GML A wander state for an enemy

W

White_heart_of_Blood

Guest
I need help making a wander state for my enemy. I have been having trouble with this. Here is my code
Code:
/// sc_enemy_wander_state
randomize();
var x_wand, y_wand;
x_wand = x;
y_wand = y;

if (distance_to_point(x_wand, y_wand) == 0) {
    do {
        x_wand = random(room_width);
        y_wand = random(room_height);
    } until (!place_meeting(x_wand, y_wand, ob_wall));
}

mp_potential_step(x_wand, y_wand, 5, false);
This is all in a script that is being called in my step event.
My problem is that he wont move at all.
It could also help if I know how to stop the wander state to go into a choose state
 

Gamebot

Member
Oops i see. Not what i thought. Your distance to point is with itself. You need to set a random point for your instance to go to. In a variable that changes would be the easiest. I rarely use alarms myself but it might not be a bad idea just to get things rolling.
 
Last edited:
B

baconlovar

Guest
It might be because it is in a step event, and it is constantly generating new cords for the AI to wonder to. Try adding a timer maybe? To run the code only once after a time period instead of 60times a second

also, I find it easier to add randomize(); in a game_start event :D
 
T

tomsaram

Guest
There is no need to test if (distance_to_point(x_wand, y_wand) == 0) if you would set their value right before.
 
W

White_heart_of_Blood

Guest
It works now but it is moving wearily here is the code:
Code:
/// sc_enemy_wander_state
var x_wand, y_wand;
alarm[0] = 0;
x_wand = random(room_width);
y_wand = random(room_height);
if (alarm[0] = 0) {
    mp_potential_step(x_wand, y_wand, 5, ob_wall);
    if (x = x_wand && y = y_wand) {
        state = sc_choose_enemy_state;
    }
}
 
B

baconlovar

Guest
I know with mp_step stuff there is cell sizes.

For example, if you have cell sizes of 32x32 the cords might not be divisible by the cell size so it might be struggling between 2 points or something
 
Top