 This video was brought to you by my patrons. Thank you so much for your support Hello there Following the series where I'm studying object orientation design in the past video We talked about aggregation in goto engine and in this video will talk about composition. So let's get started So as we saw in the previous video Aggregation is a kind of relationship between two objects a relationship of ownership between two objects Where a class doesn't depend on the other to exist So we saw that a texture doesn't depend on a sprite to exist We can delete the sprite and the texture will still exist in memory and the logic of the texture Doesn't depend on the code of the sprite and vice versa. So this configurates an aggregation to independent classes being used it together Here in composition will see that it's quite the opposite Because composition is a relationship of ownership between two classes where the child class Depends on the parent class to exist. So the logic of the child class is highly dependent on the logic of the parent class But as will nations pointed out in the previous video comments In goto engine using gd script is very hard if not impossible to achieve a composition relationship because to achieve that we would need a Private field such as a private constructor and we can do that with gd script So we can actually do that But we'll see here that we can fake it just so we can understand and try to recreate this behavior Because Lars also pointed out that if we are not so hard on the interpretation of this This relationship of the composition relationship We can create objects that depend on the parent class and this would also Configurate a composition and let's see how we can achieve that using gd script So as I said composition implies a relationship of ownership between two objects Where the child object cannot exist independent of the parent? Okay, so this is what configurate a composition I know that this example will not be a composition per se But it's just so that we can understand how this works So let's go here the node creation and I will create a collision shape And you see that it throws a warning Telling that collision shape to D only serves to provide a collision shape to a collision to D Derivate node which is a static body a rigid body a kinematic body, etc So to achieve its behavior We actually need a kinematic body to D and you can see that the kinematic body to D Also warns you that this node has no children shapes. So we can't interact with the space so we add a Collision shape here and now you see that the collision shape is still has a warning Which is the shape must be provided for collision shape to function So we go here in the shape and we create a new shape and now we have the full composition of this Kinematic body class we don't have any warnings here But will nation pointed that this is not a composition per se all of these classes can still Exist in the game without the the need of the others It won't throw an error it will be possible for them to still exist without this parent class So let's try to recreate an actual composition now I will delete these nodes and what we will do here is that I will create a Let's say yeah, I sprite so sprite and here in this sprite I will add a let's say an animation player, but here in the animation player I will add a script so it will be a sprite component Create and here in the ready call back We will try to prevent this node to exist if its parent is not a Sprite, okay So there are two ways to achieve that and we can talk about those two methods in further videos So let me know if you actually want to hear about this in the comments But we can achieve that in an aggressive way or in a defensive way The defensive way is that we won't throw any arrows, but we will do this silently So the game will still run, but it won't have this animation player in the same tree Okay, so the way we can do that is to in the ready call back. We'll see if get parent Is a sprite. It's not a sprite, right? If not get parent is a sprite, we will queue it free and by doing so I will go in composition Sprite, okay, and by doing so if you run this We'll go here in the remote sin tree and we can see that we have a animation player here But if we try to change the type of this parent here So I will change this to a simple note and if we test this We will go here in the remote sin tree and we don't have any child here in this note So the animation player was freed from memory. So it cannot exist if its parent is not a sprite, right? And the aggressive way to achieve that so this is the defensive way We do stuff silently so the game will run, but we will not have what we intended to have But the aggressive way to achieve that is trying to use an assert so we will try to assert if the parent is a Sprite Okay, so this will this will throw an error and this will prevent the game from running So if it's not a sprite, we will have an error telling that this a specific Animation player cannot exist if its parent is not a sprite node. So if we try to test this We now have an error which tells that the assertion failed Because this is not a sprite node. So if we change this to a sprite node change type to sprite And try to run this again We will go here in the remote sin tree and now we have the animation player that is a child of the sprite So now we faked I think that we faked a composition But I think that this is actually a good way because this is more I think that this is more didactic You know trying to work with private fields. It's not very understandable for anyone. That's not a Software engineer or a computer scientist or anything like that. So for me as a designer This works better. This is more understandable I would understand that this is a component using this instead of using a private field and that kind of stuff So this is a way that you can use to tell other people your team members For instance that this node that you just made so you can still add more behaviors You can make it more complex But if you actually need it to be a child of a sprite node So its logic depends that the parent is a sprite you can try to make it a component So that everyone that uses it we understand Okay, so I have to make this a child of a sprite in order to achieve its behavior and For this video, this is it Let me know in the comments if you have some doubts if you have some questions if I say something wrong Please correct me leave a thumbs up if you like this kind of content I'm really enjoying to make this and I will really appreciate if you tell me that you also like this kind of content Subscribe if you're not subscribed yet because I'm planning doing more of this kind of stuff like design patterns and Exploring more object oriented design and the best way you can tell me that you really Appreciate my work that you want to get more content from you is becoming my patreon By the way, let's welcome my newest patron tryptic. Thank you so much for joining us So this is it. I hope you enjoy it. Thank you so much for watching keep developing and to the next time