GML Animal A.I

Discussion in 'Programming' started by _Proxy, Jul 12, 2018.

  1. _Proxy

    _Proxy Member

    Joined:
    Jun 22, 2018
    Posts:
    5
    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:
     
  2. Kyon

    Kyon Member

    Joined:
    Jun 27, 2016
    Posts:
    382
    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.
     
  3. _Proxy

    _Proxy Member

    Joined:
    Jun 22, 2018
    Posts:
    5
    This is awesome! I will try this out in my game (I will also try find a way around the collisions). Thank you.
     
    Kyon likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice