• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GML Grid based movement

H

hunijkah

Guest
I am creating a game that allows the player to move one grid space (32x32) and then the enemies each get a turn to move one grid space.

I want the enemies to move one space towards the hero avoiding obstacles on the map.

First Attempt: I created my own recursive functions to 'walk' around an array representation of the map and determine the shortest path to the hero but after a certain map size (which wasn't that big) it would crash GM. So after a few hours on that I gave up on it.

Second Attempt: Using mp_grids. Now I have my grid set up correctly and I can set a path to the hero and have the enemy start to move on it. I know there's the option to make diagonal movement disabled which is great. The problem is how do I get the enemy to only move ONE grid space (32x32) towards the hero on the path?

Any help is greatly appreciated!
 
D

drowned

Guest
can't you just keep track of the enemy's movement after you start the path, and as soon as they are 32 pixels away from their starting point, stop them and kill the path?
 
H

hunijkah

Guest
can't you just keep track of the enemy's movement after you start the path, and as soon as they are 32 pixels away from their starting point, stop them and kill the path?
Well that solution would be too simple wouldn't it x_x okay time for me to go to bed. Thank you very much!

Only thing I had to do was make sure after I ended the movement I checked to see if they were still properly on the grid as sometimes they would end up a few pixels off.

STEP event:
if (( abs(x - last_x) + abs( y - last_y ) ) > 32)
{
path_end();
x = round(x/32)*32;
y = round(y/32)*32;
}
 
D

drowned

Guest
Interesting, I wonder why they were ending up a few pixels off. Is it the sprite origin?
 
H

hunijkah

Guest
Interesting, I wonder why they were ending up a few pixels off. Is it the sprite origin?
I'm not sure, I have my sprite origins set to 0,0

At first, they would go off grid and follow the lines with the path so I altered the code to adjust for this
if (mp_grid_path(grid, path, x+16, y+16, obj_hero.x+16, obj_hero.y+16, false))
{
path_start(path, 2, path_action_stop, false);
} else {
show_debug_message("NO PATH");
}

Note the +16 on x and y's
That got them to follow on the grid but now when I run my code with the check as I showed in the step event without fixing their x and y, they gain 1 extra pixel every time they move a space. Now that I'm playing with it it's kind of annoying because they kinda bounce when they fix their position at the end of their move :(
 
H

hunijkah

Guest
Oh, I figured it out.... I was saying in the step event if the difference was > 32... so it wouldn't catch it until the next step event. Changed it to >= 32 and it fixed it. Thanks alot for the help! @drowned
 
Top