The Family Tree with Zombies

The Family Tree with Zombies

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.

BobBob is your loyal zombie worker, but Bob isn’t too interesting by himself so you decide to give his clones make overs. You try painting each of them different colors.

Inheritance_1

 

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…

Inheritance_2

… 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!

Inheritance_3

 

 

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.

InheritanceDialog

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.

Override

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.

CallParent

Clicking that will give you a call parent function node that you can add anywhere in that node’s execution path.

Parent2

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”.

GetAllActors
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.

 


3 thoughts on “The Family Tree with Zombies”

  • 1
    Anonymous on April 16, 2016 Reply

    goodj job ty

  • 2
    Kat Skinner on April 21, 2016 Reply

    Excellent! This is one of the best examples of inheritance that I have seen! Thank you very much for taking the time to do the graphics and tutorial.

  • 3

    Very good explanation for beginners!

Comment on this post!