Inheritance in Unreal Engine for
Dummies Mad Scientists
So I got some questions recently on one of my other posts about my use of inheritance so I figured this might be a good topic for its own post. This one might not be very useful to people with programming backgrounds but Unreal attracts a lot of people who don’t have strong programming backgrounds and this stuff can be useful to them. So let’s start with what is inheritance.
Let’s say you have this really cool magic cloning machine. You can clone anything you put in it. You decide to start with Bob.
Bob in different colors is great. For all intents and purposes he’s the same Bob you’ve grown to love but now in new and interesting colors. But I mentioned earlier that this cloner isn’t just any old matter replicator. This cloner is magic, and what makes it magic is that the pretty new Bob clones aren’t just snapshots of Bob at the moment of his cloning, the Bob clones are actively tied to Bob. So to illustrate that point let’s give Bob a pretty new hat…
… And Presto! All of Bob’s clones have Bob’s pretty new hat! This isn’t just restricted to clones of Bob. Let’s Say you made clones of green paint Bob. The Children of Green Paint Bob would inherit Bob’s new hat too!
So that’s great and all but what does this have to do with Unreal?
Inheritance in Unreal works a lot like the magic cloner we used on the Bob zombies in the story above, except instead of cloning Bobs we’ll be using it on blueprint classes. If you’ve ever created a blueprint class you’ve used inheritance already and you may not have even realized it. No blueprint in Unreal exists without inheriting from something else. The first thing you see when creating a blueprint is a dialog asking what you’d like to inherit from.
Classes inherit all of the functions and variables of their parent. Sometimes it’s useful to make a vague parent class that you know won’t ever be directly used in-game but will hold a lot of the shared functionality of its children. For instance in a first person shooter game it may be useful to create a blueprint for “Gun”. You know you’ll never have just a generic “Gun” in-game but you will have “Shotgun” and “Machine Gun” that can have shared functionality. If you keep that functionality on the gun class you’ll keep your code cleaner and only need to make changes in once place.
Sometimes you’ll find that your shared functionality no longer suits the child class you are working on. You want to, for instance, change the default way that “Shotgun” handles firing. Instead of shooting one bullet you want “Shotgun” to fire several pellets. What you need to do is override the parents functionality on the child. Unreal makes this really easy to do in blueprints. Next to the Add Function button you should see a drop down called Override. Just select the function on the parent you’d like to override and Unreal will spin up the function in the child class for you to manipulate as you like.
Be careful overriding function on your parent class because it means that by default anything you wrote on the parent’s function will no longer get hit. If you want to piggy back on the parent’s function without preventing it from executing you can overwrite the function and then call it on the parent anyway. This can be useful when you want to add functionality instead of replace it and it’s relatively easy to do. Just right-click on the event node that was created by overriding the parent function and you should see a call parent function option.
Clicking that will give you a call parent function node that you can add anywhere in that node’s execution path.
One last thing of note, a class that inherits from another class technically is both the new class and the old class. This can be useful to know when using the “get all actors of class” node. Let’s say I want to get all the guns in my map right now. If I made all guns inherit from a “Gun” class this becomes very easy. I just call get all actors of class “Gun”.
Even though I may have “Machine Gun” or “Shotgun” actually in the map because they both inherit from “Gun” the above call will return them all. I can then sort through them to figure out which is which.
Anyway I hope that helps somebody. If you have any questions or corrections for me just leave a comment below. Also if you have any topics you’d like me to cover feel free to leave a comment. I could always use some help coming up with new topics.