Enemy stops pathfinding if player too close to wall

P

Psytrese

Guest
I'm using a grid-based pathfinding system which for the most part works great but I've been having issues with pathfinding where the enemy will stop advancing if I'm too close to a wall.

I don't know if it's my movement script that's the problem (apologies, I don't know how to tag code):

goal_x = argument0;
goal_y = argument1;
pathspeed = argument2;
global.move_to_path = path_add();
grid_size = 32
grid_set = mp_grid_create(0,0,room_width/grid_size,room_height/grid_size,grid_size,grid_size);
mp_grid_add_instances(grid_set,obj_wall,false);
failed_bool = mp_grid_path(grid_set,global.move_to_path,x,y,goal_x,goal_y,true);
path_start(global.move_to_path,pathspeed,false,false);
return failed_bool;

It's so easy to use this limitation to your advantage as my enemies have an aggro range and won't attack if you're out of range, so you can walk up to a wall, the enemy will stop advancing, then you can move out of range completely bypassing the fight.

Is there anything I can do to prevent this from happening?
 
S

SyntaxError

Guest
I haven't worked much with path finding but I think the problem lies with the grid and it having different cell sizes compared to your room/level. I suspect that the walls are not aligned with the grid and you can move to and touch the wall but the grid has space between a blocked cell and the solid wall. I not sure I'm explaining that well. There is a debug function (mp_grid_draw) that will draw the grid so you can see where the problem might be.

Also, from the return; function, I presume this is happening in a script. You can cause problems if you call this script multiple times as creating the same grid over and over will cause memory leaks.
 
P

Psytrese

Guest
It's happening in a script, yes. I'm still in a testing room at the moment so no memory leaks currently but I can see this being an issue in the future for sure. I'll probably move that line to a control object and set it at room start! Thanks for the fresh eyes.

The mp_grid_draw is something I didn't know about and you're correct; my walls are somehow misaligned from the grid but no idea how as everything in my game currently is 32x32 (no deviations whatsoever), the walls are all snapped to a 32x32 grid, and the room size is 1024x768 which is perfectly divisible.

Any ideas?

Edit: Got it. I just noticed that the 32x32 grid snaps it to the wrong point. It's not centred? The walls are actually filling into 4 separate grid points. So annoying. Can I get it to snap to a 32x32 but in the centre of the grid point?
 
S

SyntaxError

Guest
They will be snapping to the origin of the sprite, which you probably have centered. Easies fix is to probably change the snap grid to 16x16 and move them all. Or change the origin to 0,0. Depends on factors related to how you use those objects.
 
P

Psytrese

Guest
I'm actually going to do both just to be safe. A 16x16 grid seems safer but having the origin at 0,0 means I can place it easier and shouldn't affect any of my current code.

Thanks a lot you're a lifesaver! This was driving me crazy.

Edit: If anyone else finds the same issue and has checked everything and the pathfinding still has issues (which after all this mine still did) then note that I stupidly had the origin of my player object below the objects collision mask so the game was counting me on the grid spot BELOW my actual location. All 100% functional now though! :)
 
Last edited by a moderator:
Top