Game Maker Object Inheritance

Game Make is a very easy to use application which gives you the ability to create decent games (or applications) in a matter of minutes, but to create awesome games, you going to have to learn GML and other programatic structures like object orientation parent classes and inheritance.

The idea of parents and inheritance is pretty simple. Lets set an example using a car as our ‘object’. Our car will be a Trebant. In Game Maker, you would create an Object in the usual way and call it for example ‘obj_trebant‘. You would then set code and actions for obj_trebant.

Now, lets say your creating a game with a number of different cars, so you go ahead and create a bunch of objects to represent different cars, applying Events and Actions to each. This could turn into a long process depending on how many car objects are in your game. You could just duplicate the first car object and change Actions for each new object you create, that will save you a bit of time. But what happens if for example you need to change a variable or action for each car object further down the line. You have to edit each object in turn, manually changing Actions and Variables.

There has to be a better way, and there is. Using parent classes and inheritance…

In order to use parent classes and inheritance to good effect, your going to have to work out what each of your cars have in common. Using our car example above lets say that all cars have headlights. You might have a piece of GML or some other Action in your obj_trebant object that defines headlights. As we want all cars to have headlights, it would make sense to create a parent object and add the headlight code into that, lets create a new object and call it ‘obj_car‘. Now, we have two objects: obj_car and obj_trebant. In your trebant object set its parent object to obj_car. Jolly good, you have now created an object with a parent.

If you test your code, you will probebly find that any code in your parent class isnt being executed, why is this? This is down to object inheritance. If you have a Create and Step event in obj_trebant, it will by default override the Create and Step events in obj_car. To combat this you need to ensure that you add the ‘call event’ action from the control menu to each Event that you want to inherit code or Actions from. If you add Action > Event Call to your Create Event of obj_trebant it will execute the code in the Create Event of obj_car, then execute the code in obj_trebant.

principles like this can be very handy in games where you create a number of objects (cars for example) that you want both the player and AI to use. Think Grand THeft Auto, where the player can get in any car, the cars are also AI controlled. So by using parents and inheritance, you dont have to create player fixed object. Your player can simply be an object that can inherit its visible object from something else obj_trebant for example. This is an easy action in Game Maker with the GML function:

object_set_parent('object to change','object to change to');

Lets say we create our player object: ‘obj_player‘. By default, lets say hes a Subaru (create an object called obj_subaru) with a parent of obj_car. If we wanted to change the type of car the player is, add a code event with the following code:

object_set_parent(obj_player,obj_subaru);

I hope the above goes a little way towards explaining how parent classes work in Game Maker and helps you to create more interesting and efficient games.