1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

GMS 2 Enemy wandering script.

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

  1. CrackedGamer§

    CrackedGamer§ Member

    Joined:
    Oct 12, 2018
    Posts:
    35
    I want the enemies in my game to wander around when
    Code:
    state == "idle"
    But I don't want them to wander through solid objects, what would the best approach be to this? This is in a top-down environment!
     
  2. sylvain_l

    sylvain_l Member

    Joined:
    Sep 18, 2016
    Posts:
    705
    you'll have to refine your definition of "wander around".
    are they doing it with what kind of goal in mind?

    1) are they kinda following a patrol path to see if nothing strange happen (like a thieft player in a stealth game) -> you could use paths

    2) are they looking for something/exploring each case/room/or I don't know the unit you use of the level. ->you'll need to implement some kind of IA/pathfinding to keep track of where they went & where they need to go

    3) truly wander randomly -> everytime they bump into a wall or each other just choose a random new direction to move to
     
  3. CrackedGamer§

    CrackedGamer§ Member

    Joined:
    Oct 12, 2018
    Posts:
    35
    Number 3 but they also change direction after a random timer. Sorry that I wasn't clear enough in the question.
     
  4. CrackedGamer§

    CrackedGamer§ Member

    Joined:
    Oct 12, 2018
    Posts:
    35
    I know that I have to make them change direction every time they bump into something, but I also want it on a timer which I can just use an alarm for, But how do I make them do that?
     
  5. sylvain_l

    sylvain_l Member

    Joined:
    Sep 18, 2016
    Posts:
    705
    depends what freedom of movement they have. If they can just go 4 directions you can just use (same kind of logic with 8 directions too)
    Code:
    new_direction = choose(0, 90, 180, 270);
    theorically to be cleaner you should remove the direction they bumped into something (and any direction that's blocked); else they could just keep that direction as their new direction. (but in that case they would again bump into the wall and restart the change direction code, so your call).

    note that each time they change direction you should reset the alarm too (well, or not, you choose :p)

    same code in the alarm (except that in the alarm you don't have to remove the direction it bumped into something, as it doesn't have one - in case you do it for the collision part.)

    if their direction is free 360° you can just use
    Code:
    new_direction = random(360);
    p.s.
    by default GM use the same suit of random number, if you want to have them randomize you 'll have to call
    Code:
    randomise();
     
  6. CrackedGamer§

    CrackedGamer§ Member

    Joined:
    Oct 12, 2018
    Posts:
    35
    I’ll use the
    Code:
    randomise();
    new_direction = random(360); 
    But how do I make them move in that direction?
     
  7. Relic

    Relic Member

    Joined:
    Jun 27, 2017
    Posts:
    744
    How do you make everything else move? Your player is moving already? Do the something very similar but the direction will be based on the code above.
     
  8. chamaeleon

    chamaeleon Member

    Joined:
    Jun 21, 2016
    Posts:
    994
    Don't call randomize() all the time. One time at the start of the game is sufficient.
     
  9. Fabseven

    Fabseven Member

    Joined:
    Oct 7, 2016
    Posts:
    356
    Maybe you can do something like this,
    ( didnot try in real but it should make your obj move in a direction for a second then wait then move again then wait ...)

    Code:
    create :
    
    randomize()
    _idle = 0
    _walk = 1
    _timer = room_speed
    _wait_timer = room_speed
    
    status = _idle
    alarm[0] = _timer
    
    
    alarm[0] : set a comment "timer before changing direction"
    
    step :
    
    if( status == _idle)
    {
       if(alarm[0] <=0)
       {
            speed=1
            status = _walk
            direction= irandom_range(0,360)
           alarm[0] = _timer
       }
       else
       {
            //nothing
        }
    }
    
    if ( status == _walk)
    {
         speed = clamp( speed - 0.1 , 0 , 10) //reducing speed to 0 progresivly
        
       if(alarm[0] <= 0)
       {
       alarm[0] = wait_timer
       status = _idle
       speed=0
      }
    }
    
    
     

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