Enemy Ai

R

Rosepike

Guest
Hello,
I am trying to make a platformer with every enemy having different abilities.

However, i don't know how to use AI.

I am currently working on an meele enemy but also want to create the following;
Electricity,
Brute,
Scientist (who runs away from enemy and drops key to exit on death)
An enemy that chases the player,
And an enemy that can jump on platforms.

Can anyone recommend any tutorials or pieces of code that i can use to achieve this?
I know it's complicated and requires intermediate knowledge of code, but its for a university project.
Also Is there any chance i can use DnD to achieve this?

Any help would be really appreciated!
 
You can do anything with DnD. Is it worth it? Usually not. I may be biased, but I recommend switching completely to GML as soon as you can. GML may seem overwhelming or complicated, but it's honestly way easier than DnD, for complicated things.

My favorite method is using finite state machines. This link explains them. The code examples are not in GML, but the concept follows through in any situation. Basically, you break the code apart into different sections, each that allows a different behavior. Then within each section is the conditions to change to a different chunk.
https://gamedevelopment.tutsplus.co...ines-theory-and-implementation--gamedev-11867

After you've at least skimmed that link:
In puedo-code, here is an example of what your scientist AI might look like:
Code:
if (state == wandering)
 {
 //move randomly
 if (distance to enemy < var_flee_range)
  {
  state = fleeing;
  }
 }
else if (state == fleeing)
 {
 //Run away from enemy
 if (distance to enemy > var_flee_range + 100)
  {
  state = wandering;
  }
 }
This isn't the most efficient way to do it necessary, but this way is easy to understand I think.

These lines of code are my goto for AI. Unfortunately, platformers a little more complicated than 2.5D or topdown games, in this area. (At least to me).
Code:
distance_to_point() //Caclulates distance to a point.
distance_to_object() //Calculates distance to an object
instance_nearest() //Finds the nearest object. Good for selecting an enemy to run away from for example.
mp_potential_step() //Moves towards a point while moving around solid objects. Not as great for platformers though.
Consider comparing the player's x position to the enemy's x position. If it is smaller, the enemy moves left, if it is larger, the enemy moves right.
 
R

Rosepike

Guest
You can do anything with DnD. Is it worth it? Usually not. I may be biased, but I recommend switching completely to GML as soon as you can. GML may seem overwhelming or complicated, but it's honestly way easier than DnD, for complicated things.

My favorite method is using finite state machines. This link explains them. The code examples are not in GML, but the concept follows through in any situation. Basically, you break the code apart into different sections, each that allows a different behavior. Then within each section is the conditions to change to a different chunk.
https://gamedevelopment.tutsplus.co...ines-theory-and-implementation--gamedev-11867

After you've at least skimmed that link:
In puedo-code, here is an example of what your scientist AI might look like:
Code:
if (state == wandering)
 {
 //move randomly
 if (distance to enemy < var_flee_range)
  {
  state = fleeing;
  }
 }
else if (state == fleeing)
 {
 //Run away from enemy
 if (distance to enemy > var_flee_range + 100)
  {
  state = wandering;
  }
 }
This isn't the most efficient way to do it necessary, but this way is easy to understand I think.

These lines of code are my goto for AI. Unfortunately, platformers a little more complicated than 2.5D or topdown games, in this area. (At least to me).
Code:
distance_to_point() //Caclulates distance to a point.
distance_to_object() //Calculates distance to an object
instance_nearest() //Finds the nearest object. Good for selecting an enemy to run away from for example.
mp_potential_step() //Moves towards a point while moving around solid objects. Not as great for platformers though.
Consider comparing the player's x position to the enemy's x position. If it is smaller, the enemy moves left, if it is larger, the enemy moves right.
Thank you for your help!
Are states implemented in GML or would i have to set every state as a variable?
 
The states would be a series of loops. They would do different things based on a variable.

Start of game. This sets up an enum that you use as the name for your states.
Code:
enum state
 {
 WANDERING,
 CHASING,
 FLEEING
 }
Step event:
Code:
if (STATE == state.WANDERING)
 {
 //Wander
 if (condition to start chasing)
  {
  STATE = state.CHASING;
  }
 }
Inside each loop you write the behavior for that particular state as well as the conditions for it to change states.

Here's some others. I haven't watched these videos, but these people usually have solid tutorials:
 
R

Rosepike

Guest
The states would be a series of loops. They would do different things based on a variable.

Start of game. This sets up an enum that you use as the name for your states.
Code:
enum state
 {
 WANDERING,
 CHASING,
 FLEEING
 }
Step event:
Code:
if (STATE == state.WANDERING)
 {
 //Wander
 if (condition to start chasing)
  {
  STATE = state.CHASING;
  }
 }
Inside each loop you write the behavior for that particular state as well as the conditions for it to change states.

Here's some others. I haven't watched these videos, but these people usually have solid tutorials:
Ahhh this is great! I wish i knew this ages ago! Time to completely rewrite all my code!

So I'm going to state the enum, in the creation code of the room, and then have a script for each state. Then in the step events of my enemy, I can run for example. the fleeing script when the enemy is close to the player?

Does this sound like i'm on the right track?
 
Yep, you're on the right track. The trick is changing the variable so the AI knows which section of code to run. That way you don't have it trying to do multiple things at once. The "finite" part of a finite state machine basically means it only runs one section of the code at a time. But you don't need a proper/official finite state machine to do good AI, just as long as you're on the right track.

I hope I helped you. Once you've given it a good try, feel free to ask more questions if you need.

AI programming is my favorite thing.
 
Top