Transcript for:
Inheritance vs Composition in Programming

inheritance and composition are two tactics used in programming to reuse your code as much as possible inheritance is very widespread but I think there's a lot of times where composition would fit a little bit better let's say we have this set up we have a player and this player can attack has health and a hitbox alongside that we can accept user input and similarly we have an enemy this enemy can attack as health and also a hitbox as well as some enemy specific stuff like AI in the world of inheritance we would take the attack health and hitbox and drag them out to their own individual class we'll just call this class entity and the player and enemy will extend from this class this moves all our code into one easy to change spot so if we ever have to modify the code it'll affect both the player and the Enemy at the same time we don't have to go digging through every class that has the ability to take damage and if that were the end of the story inheritance would be the perfect solution I mean right now this is really easy to comprehend but let's bring in another object let's say we have a tree and similar to the player and enemy the tree can take damage so we can extend entity but this causes a small problem while we do have the health and the hitbox now we also have the ability to attack which we probably don't want from a tree there's also a more potent underlying issue because we're using Godot both the player and the enemy Extend character body 2D whereas the tree extends down on e2d in the world of inheritance at least in Godot you can't extend from multiple classes since player and enemy both need their own character body to decode entity must also Extend character body 2D however since tree also extends from this entity class that means it must also be a character body 2D there's a few ways around this and the most straightforward approach is to change tree from a static body to a character body this isn't ideal though because this gives the tree the ability to move alternatively you could extract The Entity class out into a static entity and a character entity and decide which classes need to inherit from where but as you can see this is already getting a little bit complicated and all we did was add a tree there's got to be something better let's rewind a bit instead of using inheritance we'll approach this with composition instead of making one superclass you instead make multiple components so for example we would have an attack component a health component and of course a hitbox then the player and enemy would both just have their own list of components that they need in this case we have three let's bring back in the tree from the earlier example because components don't care what class you're extending from we can simply attach a health component and a hitbox component to the tree and we're done that's all the hierarchy we gotta worry with you can see in this case this is a lot more preferable to inheritance and there may be a few odd things like telling the hitbox component where your health component is so it can send information about taking damage but I think that's a valid trade-off in Godot each of these components would actually be a child of the player enemy or tree node and you can arbitrarily attach them to any node that you feel needs these components so for example let's say we bring in a bullet well for the bullet we want an attack component but we also want a hitbox component it doesn't matter how much HP a bullet has if it hits something it's going to destroy itself so we don't need a health this is the basic idea behind composition let's go ahead and jump into Godot and we'll build through a project where we use composition alright back on the project from the last video I've gone ahead and removed any reference to the damage functions from before I want to build back the damage functions from before but using composition as a base first off I'll add a new node 2D to the enemy and call it health component I'll also add a new script to this starting off with the script we'll add a new health variable a Max health and inside of the ready function I will set Health to Max Health then I'll add a damage function and take an attack from last video and subtract that damage from the health then if the health ever drops below zero the parent will free itself back on the enemy We'll add a new area to D object and call it hitbox component you may notice a warning about the shape but we'll get back to that later we'll attach a new script to it we'll start by exporting a health component variable then add a damage function also passing in an attack like before and then we'll check if the health component exists if it does we will call Dot damage on it and the health component variable can be set in the inspector like so one thing I don't really like about this is we can attach any node 2D to this health component variable and for example if I attach the Sprite 2D this would crash because Sprite doesn't have a damage function so instead one thing we can do is inside of the health component we can declare a class name health component then instead of just using any node 2D we can specify that we want specific typically a health component then inside of the inspector you can see that it only highlights things that classify as health components this is also nice because this gives us intellisense so if I do control space inside of this function we can see what it expects or if I control click damage it will take us to the Declaration let's go ahead and save these off to their own branches as health components seen and hitbox component scene one thing you may run into is if you previously assign the health component and you go into the hitbox component scene you'll see that it's already pointing towards a health component you can leave this if you want but it may cause issues in the future so I usually just clear this and assign it for every enemy that I create alright let's talk about this warning now so a hitbox needs a shape and your first instinct might be to put it inside of the actual scene and while that would work this means that every enemy and every object that uses hitbox component would have the exact same hitbox shape instead what we'll do in the enemy scene will actually attach a collision shape as a child with a hitbox component here this allows us to use unique hitboxes for every single object inside of the scene you can also set up Collision through here or you can do it in the anime so for example an enemy and a player would want different collisions so you can set it in their respective scenes and the last thing I'll do is set up my weapon to attack a hitbox so for this I'll check whenever we collide with an area we'll check if it has the function damage and if it does we'll construct a new attack like before and call the damage function passing in the attack we can also do something neat with this code if I head back to the hitbox component we can also declare class name hitbox component here then instead of the weapon instead of checking whether it has the method we can see if it's a type of hitbox component if it is we'll pull it out into a variable and call Dot damage on that this has the same benefits as before or we can do control and space to see what the function wants or we can control click it to see where the function is declared and that's actually all we need to do for composition if I walked up to this cow I can hit it I'll also throw together the tree in the crate just to show you how easy it is once you've got these nodes set up I'll start by dragging in a health component and then a hitbox component and inside of the health component I will set the health to 30 and inside of the hitbox I will drag in the health component then of course I'll add a shape to the hitbox give it a capsule shape and and fit it to the tree then of course for the crate we can drag in a health component and a hitbox component I'll attach the health component and set the HP to 20. then I'll add a new Collision shape of a rectangle and just like that if we go in the game it takes three hits to kill a tree two hits to kill a crate hopefully this shows you how powerful composition can be of course it isn't one size fits all sometimes inheritance is better sometimes composition is better but of course that's how everything is in game development hopefully this makes game development easier for at least someone out there and as always have a good one [Music] foreign