Platform Builder

Platform Builder

Not enough ratings
Creating Basic Enemy AI [Platform Builder]
By PorkChip
Want to create your own enemy that acts in a more complex way than simply harming the character by walking into them? This guide will cover the basics of enemy AI so you can populate your game with more sophisticated enemy behaviours.
   
Award
Favorite
Favorited
Unfavorite
Enemy, my friend...
Let's jump right in and create our first intelligent enemy.
Click Menu in the bottom left corner, then choose Game Setup from the menu.
Select Custom Enemies from the sub menu and then click new to make a new enemy, give it a name then click Edit.
Choose sprite appearance and find the Porcupricks enemy sprite (Got to love the names of some of these enemies!)
This is optional but will be better for testing, but uncheck the Harm Character box on the right.
We are done here for now, so use the back arrow to go back to the last menu.

Make another new enemy here and call it something like Porker Charge or something since this is going to be our attack.
Go into his editor and again find the same enemy sprite as last time.
This time on Movement Settings, set his speed to Very Fast as this will act as his charge attack.
You can leave the Harm Character checked this time as we want this sprite to act as his attack and hurt the player.
Get out of here and return to our first enemy where we will now set up our AI.
On the right hand side with the list of command functions, click Looping.
In the command prompt box, type this text:
if distance to char (then click the Managers button underneath and find your character and it will insert a number)<50

My example would read like this: if distance to char 1<50

Then underneath, type change enemy (click Managers button again to find your first enemy), (click Managers for second enemy.)

My example would read like this: change enemy 1, 2

The whole thing should read like this:
if distance to char 1< 50
change enemy 1,2

The logic of this command is that if the player is less than 50 pixels away from the enemy, the enemy sprite will change to the charging one.
Test out your game and you will see that the enemy will now move at a slower speed and then charge when it is 50 pixels or less away.
What you may wish to do now is do the reverse operation for the charging enemy so it reverts back to walking after the player has moved away.

Edit your charging enemy and input text under Looping:
if distance to char x>50
change enemy y, z (obviously substituting x, y and z with the ID numbers of your player and enemies.)

Now, if you set movement to Follow Player, you can test this out and if you move past the charging enemy it will revert back to walk speed if you move away from it a greater distance than 50 pixels rather than it just attacking the player and then charging around the level for all eternity after that.

This is just the tip of the iceberg for creating basic enemy AI. You can upload your own sprites for enemy throwing punches and melee attacks for example and have different commands for when the AI is initiated.

Experiment on your own now you know the basics and you will be surprised what you can achieve.