Grid Enemy movement

How would I make my enemy follow the player once the player is in a certain range but make the enemy move on a 16x16 grid. I already know how to make it so the enemy will move towards the player but I want the enemy to move in a grid style to the player.
 

Slyddar

Member
Depends how perfect you need the collisions on the enemy. Since you know how to make them move towards the player, you could just add a draw event with this and draw them at their closest 16 grid position.
Code:
//draw event
draw_sprite(sprite_index, image_index, round(x/16) * 16, round(y/16) * 16)
This doesn't move the mask though. If you needed the mask to move as well, you could store the x and y position, apply the grid code, draw it, then restore the x and y position, as below:
Code:
//draw event
var x_temp = x;
x = round(x/16) * 16;
var y_temp = y;
y = round(y/16) * 16;
draw_sprite(sprite_index, image_index, x, y);

//at the end of the draw event
x = x_temp;
y = y_temp;
 

Slyddar

Member
is there a different way instead of using a drawing event??
Is there a reason you don't want to use the draw event?

You might be able to do the 2nd method using end step to do the rounding and storing of the original locations into instance variables, then restore them in the begin step.
 

Bentley

Member
How would I make my enemy follow the player once the player is in a certain range but make the enemy move on a 16x16 grid. I already know how to make it so the enemy will move towards the player but I want the enemy to move in a grid style to the player.
I haven't worked with grid AI in a while, but this should work. Just some things to keep in mind:
-I assumed you didn't want diagonal movement.
-move speed is a multiple of TILE_SIZE so the enemy will arrive exactly at the destination.
-I chose to move the enemy based on which distance was further (x to dest_x or y to dest_y). You can easily change that from > to < if you want the reverse. That decision is made at the start to avoid zig-zagging.

Create event
Code:
#macro TILE_SIZE 16
move_speed = 4;
dest_x = x;
dest_y = y;
move_horz = false;
Step event
Code:
// Test
if (mouse_check_button_pressed(mb_left))
{
    // This would be the player's snapped x/y when the AI is ready to chase
    dest_x = mouse_x div TILE_SIZE * TILE_SIZE;
    dest_y = mouse_y div TILE_SIZE * TILE_SIZE;
 
    if (abs(x - dest_x) > abs(y - dest_y))
    {
        move_horz = true;
    }
    else
    {
        move_horz = false;
    }
}

// Move to destination
if (x != dest_x || y != dest_y)
{
    if (move_horz)
    {
        x += sign(dest_x - x) * move_speed;
 
        if (x == dest_x)
        {
            move_horz = false;
        }
    }
    else
    {
        y += sign(dest_y - y) * move_speed;
 
        if (y == dest_y)
        {
            move_horz = true;
        }
    }
}
else
{
    show_debug_message("arrived");
}
 
Last edited:
this is what i have so far:

create event
Code:
randomize();
AlarmSpeed = 15;
alarm[0] = 3;
step event
Code:
randomize();
RandVar = irandom_range(1,8);//create a random number for 4 directions
//AlarmSpeed = irandom_range(30,120);
//alarm[0] =3;
alarm event
Code:
if (RandVar == 1) {

    self.y -= 16;
    alarm[0] = AlarmSpeed;

};

if (RandVar == 2) {

    self.x += 16
    alarm[0] = AlarmSpeed;
    
};

if (RandVar == 3) {

    self.y += 16;
    alarm[0] = AlarmSpeed;

};

if (RandVar == 4) {

    self.y += 16
    alarm[0] = AlarmSpeed;
    
};

if (RandVar == 5) {

    self.y -= 16;
    alarm[0] = AlarmSpeed;

};

if (RandVar == 6) {

    self.x += 16
    alarm[0] = AlarmSpeed;
    
};

if (RandVar == 7) {

    self.y += 16;
    alarm[0] = AlarmSpeed;

};

if (RandVar == 8) {

    self.y += 16
    alarm[0] = AlarmSpeed;
    
};


I would like to do something in the step event so that when the player is in a certain range the enemy will heads toward him. something like a path finder code or something is what i was thinking. I have been using gml for a long time but this is my first project using just gml and not the drag and drop system. I am also mostly self taught and sadly not the smartest when it comes to coding
 
Top