GMS 2 Enemy Idle... again

Discussion in 'Programming' started by CrackedGamer§, Oct 28, 2018.

Tags:
  1. CrackedGamer§

    CrackedGamer§ Member

    Joined:
    Oct 12, 2018
    Posts:
    32
    I need help with the enemy Idle script... again I got help last time but I am too new to GML to know how to implement that. I have gotten almost nowhere,
    Code:
    randomize()
    
    //In what direction are they going to move
    direction = random(360);
    
    //For how long they are going to move
    alarm[11] = random_range(15,30);
    
    That is all I got, I want the enemy to move until
    Code:
    alarm[11] == 0 || they collide with obj_solid
    
    This is all in a script called "scr_ai_wander"
    Also, the game is in a 2.5D perspective, like the pokemon games so basically topdown.
     
  2. chamaeleon

    chamaeleon Member

    Joined:
    Jun 21, 2016
    Posts:
    931
    Set speed to 0 in the alarm event and in a collision event? Of course, odds are you'll want to have it move again at some point and not idle forever, so presumably you'll want to come up with a condition that starts movement again?

    Side note, don't call randomize() all the time / every step. Once in a game start event or create event for an object that only has one instance in existence is sufficient (and better).
     
  3. CrackedGamer§

    CrackedGamer§ Member

    Joined:
    Oct 12, 2018
    Posts:
    32
    Ok thank you, I'll try to come up with something.
     
  4. samspade

    samspade Member

    Joined:
    Feb 26, 2017
    Posts:
    1,969
    This is the type of things a state machine would be good for. However, for something this straightforward it might be better to just do it simply like this:

    Code:
    
    ///step event
     if (idle == true) {
        speed = 0;
        if (alarm[0] == -1) {
            alarm[0] = idle_length;
        }
    } else {
        direction = random(360);
    }
    
    ///alarm[0]
    if (idle == true) {
        idle = false;
        speed = 5;
        alarm[0] = move_length;
    } else {
        idle = true;
        alarm[0] = idle_length;
    }
    
    
    Note that you don't actually need to set the alarm in the step event if the only way you can become idle is in the alarm event itself. However, if you allow other things to turn the instance idle, you want a fail safe. Also, I don't know what your project looks like, but having it go idle when it collides with something seems like a slightly bad idea with the way you're choosing directions. For example, if it collides with something that doesn't move, such as a wall, and you are using square masks there is a 1/4 chance that upon becoming not idle it's first move will be directly back towards the wall making it idle, and a much higher chance that it will hit the wall within a few steps. Meaning over time objects would tend to get stuck on walls.
     

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