GameMaker Enemy Idle... again

C

CrackedGamer§

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

chamaeleon

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

CrackedGamer§

Guest
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).
Ok thank you, I'll try to come up with something.
 

samspade

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