• Hey! Guest! The 39th GMC Jam will take place between November 26th, 12:00 UTC and November 30th, 12:00 UTC. Why not join in! Click here to find out more!

GMS 2 Enemy wandering script.

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!
 

sylvain_l

Member
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
 
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
Number 3 but they also change direction after a random timer. Sorry that I wasn't clear enough in the question.
 
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
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?
 

sylvain_l

Member
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();
 
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();
I’ll use the
Code:
randomise();
new_direction = random(360);
But how do I make them move in that direction?
 

Relic

Member
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.
 

Fabseven

Member
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
  }
}
 
Top