GameMaker Parenting and Inheritance

GameMaker Parenting and Inheritance

Hey guys! In this GameMaker basics tutorial, we will learn about parenting and inheritance. A parent object is able to pass its code down to child objects, which then run the parent code as if it were their own. You can save a lot of time by creating a parent enemy object, which has common variables and code that all of your enemies may need, and passing it down. This is exactly what we will be doing in this example.

Parent set up

The first thing we need to do is create the actual parent object. This object will not need to exist in the actual game itself, because it will only be passing its code to objects that will exist in the game. Create a new object and name it parentEnemy. This object does not need to have a sprite. Add the create event and step event, and let’s add some code.

parentEnemy create event

life = 5;
dead = false;

parentEnemy step event

if(life <= 0){
dead = true;
}
if(dead){
instance_destroy();
}

In the create event we are initializing two variables. Life is the amount of health/hp our enemy will have, and dead is a boolean that lets us define what happens when the enemy’s life reaches zero. In this case, we are just going to destroy the object.

Now we need to create an enemy object to pass this code to. Create a sprite of some kind for your new enemy, it doesn’t matter what it looks like, and then create a new enemy object. Assign a sprite to this new enemy object, and set the parent to parentEnemy.

Finally, add the create event, step event, and draw event.

Child set up

Now we are ready to pass our code from the parent to the new object, which will be referred to as the child object from here on out.

Child object create event

event_inherited();

Child object step event

event_inherited();
if(mouse_check_button_pressed(mb_left)){
life --;
}

Child object draw event

draw_self();
draw_text(x,y - sprite_height, life);

By calling the event_inherited function, the code from the parent object is passed down. It is important to call this function in each event the child object shares with the parent object. So, since we have a create event, and step event in the parent and child, we have to call event_inherited in the child in both events. If our child object did not have one of the same events as the parent, let’s say step event for example, the child will automatically inherit the step event code from the parent. Just something to keep in mind.

The last bit of code up there for the draw event is mostly for debug purposes. We are drawing the life variable to the screen, so we can see it deplete when we click the mouse button. If you run the game now, and click the mouse, your enemy life should deplete to zero and then be deleted.

Overwriting inherited code

There may be times when your child objects do not need to inherit all of the parents code, or you need to make changes to the code that is being inherited. Let’s say you want one of your enemies to have ten life, instead of five that it is inheriting. To overwrite the inherited variable, all you need to do is re-initialize that variable after you call event_inherited.

Child object create event

event_inherited();
life = 10;

Each enemy has a different life variable, and is still a child of the parentEnemy object.

For step events, and code that runs every frame, it can be a little different. For example, if we had this code in both the parent and the child…

if(mouse_check_button_pressed(mb_left)){
life --;
}

… and the child was inheriting from the parent, then every time we clicked the mouse the life variable would be reduced by two, rather than one. It’s best to avoid any duplicate code in the parent and child object. But, what do you do if you don’t want the child to inherit part of the parent code? It’s as easy as not calling event_inherited in the child event. The best practice here is to try to give the parent code that you are absolutely sure the child will need.

Conclusion

We hope you enjoyed this tutorial. See you next time!

Leave a Reply

Your email address will not be published. Required fields are marked *