GML Animal A.I

P

_Proxy

Guest
Hey guys.

Right now I am making a survival game. I have setup all the elements such as tree cutting, mining, user interface and crafting. But the only problem is that, I have no idea how to code animal A.I. But I do know how to code enemy A.I. so I need help with the following:

Definitely:
- Animal Wandering State.
- Animal Fleeing State.
- Animal Collisions And Path Finding.
- And I would like some of the animals to stick together.

Maybe:
- Animal Breeding.
- Animal Cone Of Vision.
- I have will make it so that the animals drop food and i can cook it. (but feel free to give me other ideas).

this is a video of what my game looks like right now:
 

Kyon

Member
How I would do this is to make a variable called "state"
so something in create like state="Wandering";

Then in the step event you check what state he is in, and make a simple AI for that state.
So like for example:
Code:
if state="Wandering"{
    if dirtime>0{dirtime-=1;}else{dirtime=room_speed*2; dir=choose(1,0,0,-1);}
    if dir=1{x+=1;}
    if dir=-1{x-=1;}
}
(might want to add dir=1; and dirtime=0; to the create event)



Then you switch states when something happens, like for example the player comes too close
Code:
if distance_to_object(obj_player)<120{
   if state="Wandering"{state="Flee"; fleetime=room_speed*5;}
}
Idk, also add fleetime=0; to create event haha
Then make it go back to Wandering if the player is away after 5 sec or so:
Code:
if fleetime>0{fleetime-=1;}else{if state="Flee"{state="Wandering";}}

The Flee state should be something like, idk:
Code:
if state="Flee"{
   if obj_player.x>x{x-=2;} //if the object player is to your right run left
   if obj_player.x<x{x+=2;}
  if obj_player.y<y{y+=2;}
  //.....etc
}



But of course then it becomes more complicated because the animal has to avoid stuff,
so you could place stuff like "if !place_meeting(x,y,obj_wall)" before movement, that would help. But there are better ways for that, I'm not sure what your movement system is anyway.
This is just how I would do it.
 
P

_Proxy

Guest
How I would do this is to make a variable called "state"
so something in create like state="Wandering";

Then in the step event you check what state he is in, and make a simple AI for that state.
So like for example:
Code:
if state="Wandering"{
    if dirtime>0{dirtime-=1;}else{dirtime=room_speed*2; dir=choose(1,0,0,-1);}
    if dir=1{x+=1;}
    if dir=-1{x-=1;}
}
(might want to add dir=1; and dirtime=0; to the create event)



Then you switch states when something happens, like for example the player comes too close
Code:
if distance_to_object(obj_player)<120{
   if state="Wandering"{state="Flee"; fleetime=room_speed*5;}
}
Idk, also add fleetime=0; to create event haha
Then make it go back to Wandering if the player is away after 5 sec or so:
Code:
if fleetime>0{fleetime-=1;}else{if state="Flee"{state="Wandering";}}

The Flee state should be something like, idk:
Code:
if state="Flee"{
   if obj_player.x>x{x-=2;} //if the object player is to your right run left
   if obj_player.x<x{x+=2;}
  if obj_player.y<y{y+=2;}
  //.....etc
}



But of course then it becomes more complicated because the animal has to avoid stuff,
so you could place stuff like "if !place_meeting(x,y,obj_wall)" before movement, that would help. But there are better ways for that, I'm not sure what your movement system is anyway.
This is just how I would do it.
This is awesome! I will try this out in my game (I will also try find a way around the collisions). Thank you.
 
Top