• 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!

mp grid instance not following path

Pfap

Member
My instance always successfully draws the path, but sometimes won't follow it. It seems that it is getting stuck somehow.

Grid creation code
Code:
global.grid_m = mp_grid_create(0, 0, room_width / 8, room_height / 8, 8, 8);


mp_grid_add_instances(global.grid_m, wall, false);
The above debugs and draws as expected.


Enemy object



Code:
if path_exists(new_path){
 
 path_delete(new_path);
 
}

 new_path = path_add();
 

 if mp_grid_path(global.grid_m,new_path,x,y,player.x,player.y,0){
 
 
 
   path_start(new_path, 1, path_action_stop, 1);
 
 
 }
 else{
 
  show_debug_message("path creation failed");
 
 }



My wall object is 8x8 pixels, while my enemy sprite is 32x32 could this be what is causing the problem?

And if that is my problem is there a way I can tell the grid to run the path check with an 8x8 object and then have my 32x32 object just follow the path anyways?

I'm thinking a solution like quidditch where I send an object with 8x8 dimensions after the player and just instruct the enemy to chase that? Or rather copy that path and have the enemy follow it.







I've been trying to get this solved for a few hours and I'm not entirely sure what is going on here.




Edit:

I tested with a sprite that is 8x8 and uploaded a video. The enemy sprites are the neon green squares. The path is always drawn and they sometimes just decide not to follow it.


 
Last edited:
I

icuurd12b42

Guest
I think you should debug draw the grid over the room with alpha of .25 to see through it, there is a function specifically for this, I forget its name, it is compatible with draw_set_alpha() so it is useful to overlay the grid with the room. I suspect the grid does not match the instances in the room, 90% of the time, with this issue, the grid is offset with the instances... and I suspect those instance are solid and you have collision event with the ai that runs on the path. So yeah a path is found, avoiding things but in the room those thing are offset relative to the grid. and so are in the way.


also you dont need to delete the path each new a* check. mp_grid_path will clear the path of it's content. basicaly create a new past in instance create and delete the path on cleanup (gms2)... or instance destroy and room end event (gms14)
 

Pfap

Member
I think you should debug draw the grid over the room with alpha of .25 to see through it, there is a function specifically for this, I forget its name, it is compatible with draw_set_alpha() so it is useful to overlay the grid with the room. I suspect the grid does not match the instances in the room, 90% of the time, with this issue, the grid is offset with the instances... and I suspect those instance are solid and you have collision event with the ai that runs on the path. So yeah a path is found, avoiding things but in the room those thing are offset relative to the grid. and so are in the way.


also you dont need to delete the path each new a* check. mp_grid_path will clear the path of it's content. basicaly create a new past in instance create and delete the path on cleanup (gms2)... or instance destroy and room end event (gms14)

Thanks, that is really useful to know especially about not needing to always delete the path. I did set the alpha to a lower value and it looks like everything lines up correctly.
Also, I am not using any solid instances and the enemies don't have any collisions yet, I still need to figure out a way to keep moving instances from potentially overlapping each other though.
I did have the enemies flagged as solid while testing early in development, but have unchecked the solid box and pressed the clean button.

Part of the reason I am using the mp_grid is so the grid can handle "collisions" by preventing the enemies from walking through stuff by blocking out cells.

My sprites have there origin set as middle center, and I am creating the path_grid object last in the room.





It works pretty well in the above video except there are times when they just stop chasing.
The 20 second and 36 second marks.

Edit:
It also seems that it only happens when the enemy instances are about to chase down the screen. They will freeze and then if I move the player up to past horizontal with them they will restart chasing.
 
Last edited:
I

icuurd12b42

Guest
That's weird. I've never seen that tbh...

try not to path_start every step... move that step code in an alarm that triggers every 1/4 of a second
 

Pfap

Member
That's weird. I've never seen that tbh...

try not to path_start every step... move that step code in an alarm that triggers every 1/4 of a second

Yea, that seems to work.

I've never had any problems when using grids with 32x32 cell size... so, maybe 8x8 in my 1024x576 room with multiple instances of enemies is just too much?


Thanks, especially for that alpha tip; some things just need to bite you before you learn them lol.
 
I

icuurd12b42

Guest
The only effect of smaller cells is the cost to do a single a*... but's since you have a path generated, as shown by the draw_path, I dont think think the problem is the cell size...

do you see that little curve, sometimes, right at the start of the path? that is from path point 0 to path point 1. path point 0 is where the instance is at... it's x,y, that automatically is always the first path point... then the second and onward points are the mapped coord of the mp_grid cells constituting the route towards the desired destination. and finally the last point on the path is the x,y coord of the destination.

upload_2018-10-21_7-17-30.png

So you look at the drawing of the path and the moving of the instance... it should really follow that initial curve, that red line path of that path example on the drawing, but it's not. it's going straight. And it sort of fails in the case of the upper left going to the bottom right, when that curve goes "inside" the instance... and it goes straight in the case of the bottom right going to the upper left... that is why I suspect doing the path_start every step is cause the issue. I'm not sure why. anyway...

I think the alarm method allows it to do that curve... also, if you are planning on having a lot of instances do a path, it will be less costly to use an alarm.... Consider also setting up the instance so they don't all calculate a path on the same game frame...
 

Pfap

Member
The only effect of smaller cells is the cost to do a single a*... but's since you have a path generated, as shown by the draw_path, I dont think think the problem is the cell size...

do you see that little curve, sometimes, right at the start of the path? that is from path point 0 to path point 1. path point 0 is where the instance is at... it's x,y, that automatically is always the first path point... then the second and onward points are the mapped coord of the mp_grid cells constituting the route towards the desired destination. and finally the last point on the path is the x,y coord of the destination.

View attachment 21251

So you look at the drawing of the path and the moving of the instance... it should really follow that initial curve, that red line path of that path example on the drawing, but it's not. it's going straight. And it sort of fails in the case of the upper left going to the bottom right, when that curve goes "inside" the instance... and it goes straight in the case of the bottom right going to the upper left... that is why I suspect doing the path_start every step is cause the issue. I'm not sure why. anyway...

I think the alarm method allows it to do that curve... also, if you are planning on having a lot of instances do a path, it will be less costly to use an alarm.... Consider also setting up the instance so they don't all calculate a path on the same game frame...

Cool, that's good to know.

I did try to have the enemy, instead of going directly to the player go to the grid snap point nearest to the player, but that seemed to get rather complex and I was struggling with it.
I figured it would be something like find out which cell the player is in and then somehow pick the nearest value that is snapped to the 8x8 mp_grid… but the alarm worked, lol.

After playing with it some more I actually settled on updating the enemy path every second; it feels more natural as the enemies take some time to react. I will have to play around with alternating frames they update there path on, but I tested with maybe 60 and it worked well on my pc.
 
Top