Game Maker: use of parents and AI

I would like to talk about a solution about AI that I used many times. Problem: we want to make intelligent enemies that move like the player (i.e. they can walk, shoot, run and jump). To make things simpler, the idea is to put the code that takes care of the movement (common to player and enemies) in a parent object.

So we make a parent object, and we define some control variables in the Create event. Example:

In the Step event, instead of checking the keyboard, we move the object on the basis of these control variables. Example:

We made an object that potentially can move, but so far it can’t, because control variables are not set. So we make two child objects, player and enemy, in which we write some code that controls the object using the control variables. Note: we could also define some condition variables that the childs can check to understand for example if the object hit a wall (cnd_wall).

Let’s look at the possible code of the childs. Player:

Enemy:

With a few rows of code we made a very simple but perfectly working artificial intelligence.