1. Hey! Guest! The 34th GMC Jam will take place between August 22nd, 12:00 UTC (Thursday noon) and August 26th, 12:00 UTC (Monday noon). Why not join in! Click here to find out more!
    Dismiss Notice

GMS 2 Top-Down Enemy AI

Discussion in 'Programming' started by CrackedGamer§, Dec 18, 2018.

  1. CrackedGamer§

    CrackedGamer§ Member

    Joined:
    Oct 12, 2018
    Posts:
    32
    So, I am working on a Top-Down explorer game (is that a real genre?), I need some help with my enemy AI. Every enemy has the following states:
    Idle - Wander randomly

    Attack/Hunt - Run towards player while avoiding solid objects

    Dead - Replace itself with a corpse

    Cooldown - Stand completely still for 60 frames / 1 sec

    The stuff that I have already gotten down is:

    scr_enemyAttack
    Code:
    mp_potential_step(obj_player.x, obj_player.y, huntspd, false)
    
    &

    scr_enemyDie
    Code:
    instance_create_layer(x,y,"corpses",corpsetype);
    instance_create_layer(x,y,"ui_invisible_objs",obj_enemy_loot);
    instance_destroy(self);
    
    The huntspd variable is set in the create event for the enemy, which in this case is: obj_player.movespd + 3

    The corpsetype variable is also set in the enemy's create event, which in this case is: obj_medium_corpse

    So the states that are left are:
    Idle & Cooldown

    I really have no idea how to do either of these efficiently, I could probably make a code for the Idle state with an alarm but I want to avoid that and keep everything (except setting variables, of course) in the step event so that everything can be in one script per state.

    And I also want the enemy to detect the player if they are x amount of pixels away in front of them, my suggestion would be ray-casting but if you have a better idea go for it.

    Also, I am not asking for anyone to write a script! But if you know of a function or technique that could be useful for this then please help me :)

    --- THANK YOU IN ADVANCE ---
     
    Last edited: Dec 19, 2018
  2. Shavv

    Shavv Member

    Joined:
    Aug 3, 2016
    Posts:
    53
    Why don't you show first what you came up with and ask questions about that? You are basically asking for a service other then help with your problem.
     
    Frozen Stick likes this.
  3. Frozen Stick

    Frozen Stick Member

    Joined:
    Jun 20, 2016
    Posts:
    21
    This will take a while and as @Shavv stated , you are asking for service not a question.You should write your own code and we will help you if you can't get it working.
     
  4. 3dgeminis

    3dgeminis Member

    Joined:
    Jun 21, 2016
    Posts:
    120
    For distance use point_distance or distance_to_point
    For attack/hunt, mp_potential_step
    For dead, instance_destroy and instance_create
    For cool down you can use an alrm or a variable timer=60; timer-=1; if timer=0 {........}
    Check the MANUAL for more and look tutorials of Finite State Machine to program ai
     
    Frozen Stick likes this.
  5. CrackedGamer§

    CrackedGamer§ Member

    Joined:
    Oct 12, 2018
    Posts:
    32
    Fair...
     
  6. CrackedGamer§

    CrackedGamer§ Member

    Joined:
    Oct 12, 2018
    Posts:
    32
    Thank you, this will help immensely! :)
     
  7. CrackedGamer§

    CrackedGamer§ Member

    Joined:
    Oct 12, 2018
    Posts:
    32
    That is fair... :/
     

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