Construct 2 RPG series #4: Basic AI

Before I go on about making a simple AI, let’s take a look at how the character attacks. If you look at it closely when the player press K repeatedly (or attack button on the gamepad), the character will attack without stop. I want to prevent this because attacking without stopping will make the gameplay becoma a button mashing type. Add an instance variable called attackCount (a number) to the Friendly family, so that this limitation will also effect the other party members.  And then at the event where charaAttacks function is called (where button K is pressed for example), add 1 to the attackCount of the character. Maybe you’ll ask: why not add the attackCount inside charaAttacks function? Because doing that will make attackCount of other party members add up when one character attacks. To avoid that I increased attackCount of attacking character in the place that I’m sure it’s the controlled character who attacks. We’re gonna have to implement something like this at the event when other party members attack.

add attack count

After that we will add a Timer behaviour to family Friendly, this to make a little delay after the character finishes their attack according to their attackCount. This delay needs to be named for us to use it, I will simply name it “rest”, because the player is resting after delivering a series of attacks. Make a string constant variable with the value “rest”. Then add an instance variable named state to family Friendly, this is where we will give the value of “rest” to mark that this character just attacked consecutively.

Make two events that handles when player enter “rest” state and when that state ends. The character will enter “rest” when their attackCount reach 3 (or whatever long you want the combo to be).  Here we start the timer with the tag “doneRest” for 1 second (or however long you want it to be), after the set time the Timer will trigger an OnTimer event, which can be recognized by the tag that we gave it. We will end the “rest” state in this OnTimer event.

timer events

Don’t forget to change the attackCount back to 0 in the event Friendly attackCount >=3, if not then Timer will not trigger an OnTimer event. This is because the value of attackCount every loop will be equal or bigger than 3, and we will start the timer every loop, this will result in the timer unable to count to the time that we set and then the state value will never become empty. Lastly, don’t forget to change the event where our character attacks a little bit, so that the character will only attack when he is not in rest condition.

Only attack while not rest

Next I’m gonna add a simple AI to our enemy, for now the AI will only moves the monster closer when it see our character (it won’t attack for now). First add a line of sight behaviour to family Enemies, this behaviour will determine whether the character is close enough to the enemy or not. Then add an instance variable called radius to family Enemies, enemy will attack the character when  the character is in range of this radius, for beginning the radius value is 60 (in pixels), I have also changed the line of sight’s range to 150.

Here, I write two new events. First is when the enemy sees that a character is near, second when it doesn’t see. When the enemy sees the character, it will move closer to its radius, when it doesn’t it will only stand still (for now, at least). This AI also change the enemy’s walking animation.

simple enemy AI

For now the enemy will only follow the character like a good dog, but later I want the enemy to be able to attack and use skills. And we’ll need a more complex AI for the boss, and the AI system will have to be easily customizable so that it will make our job easier when making another enemy. I will write about it on my next tutorial.