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

Pathfinding and roaming states

M

Monster25

Guest
I've been using pathfinding for a while now in my game (top down shooter). The enemies are supposed to have 2 different states whenever they are not engaged, idle and roam. While in idle they stay still and while they are roaming they pick a random direction and move there until an alarm makes them pick again between idle and roam just to make them act a bit more natural. I've succesfuly implemented the pathfinding using motion planning for when the enemies chase you or when they are startled and look for you in your last position but for some reason when I try to use the same code but calculate the end coordinates for the roaming path they won't budge at all.
This is the code found in their roaming state:

scr_enemy_movement();
//Pathfinding
var cx = (lengthdir_x(1,enemy_angle)/32)*32+16;
var cy = (lengthdir_y(1,enemy_angle)/32)*32+16;
if (mp_grid_path(global.grid,path,x,y,cx,cy,1))
{
path_start(path, movespeed,path_action_stop, false);
}

the enemy movement script calculates their movespeed and for some reason if I try and use lengthdir_x and lengthdir_y to calculate the end of their path they won't move at all but if I place the player's coordinates or any other hard coordinate instead, they will move accordingly and correctly.

I'm not sure what's happening and why this is not working, is the pathfinding not able to use lengthdir calculations?
 
M

Monster25

Guest
Yes they do receive a path, while in any other state they do, if it involves hard coordinates they do but if I calculate them with lengthdir_x they suddenly don't
 
M

Monster25

Guest
I don't understand why it works with the volatile coordinates of the player which keep changing but it won't work with the lengthdirs since they get their coordinates the same volatile way.
 
M

Monster25

Guest
I've tried creating a separate object like a flag where the enemy is supposed to move and use its coordinates instead but it still won't work, it also clutters the game a lot.
 
A

anomalous

Guest
Always output your expected values if you run into an issue. It will usually give you some clues as to what's going on.
Code:
scr_enemy_movement();
//Pathfinding
var cx = (lengthdir_x(1,enemy_angle)/32)*32+16;
var cy = (lengthdir_y(1,enemy_angle)/32)*32+16;
show_debug_message("cx = " + string(cx);
show_debug_message("cy = " + string(cy);
if (mp_grid_path(global.grid,path,x,y,cx,cy,1))
{
show_debug_message("path found");
path_start(path, movespeed,path_action_stop, false);
}
 
M

Monster25

Guest
Allright I did as you suggested, the cx and cy are being calculated correctly but the path won't start at all.
 
M

Monster25

Guest
I've tracked the problem down to the statement
if (mp_grid_path(global.grid,path,x,y,cx,cy,1))
the result of this is always 0. If I switch cx and cy with the player's x and y it suddenly works, the same happens for any constant coordinates.
 
A

anomalous

Guest
I still think your cx/cy are incorrect.
var cy = (lengthdir_y(1,enemy_angle)/32)*32+16;

That's your destination y? lengthdir 1 will be between -1 and 1? Thats what, the upper right of your room?
Then /32*32..that's not snapping to grid, you truncate for that like div 32 *32. +16 is moving to the center of the tile, that's the only thing that looks correct to me.

Normally I expect to see an origination x/y ADDED to the lengthdirx/y values.
so var cx = (x) + lengthdir_x...

In any case, if that's not it, I suggest you draw a circle at x/y and cx/cy, and use draw mp grid (the debug where it shows you the grids green/red collision), and you will see the issue.
 
M

Monster25

Guest
Oh it seems you are right, the lengthdirs are missing the origination x/y but how can I make them become negative if they need to be? I've added them both but the enemy seems to move to the south east only like he's adding x and y.
 
M

Monster25

Guest
this is the updated code
var cx = x+(lengthdir_x(1,enemy_angle)/32)*32+16;
var cy = y+(lengthdir_y(1,enemy_angle)/32)*32+16;
show_debug_message("cx = " + string(cx));
show_debug_message("cy = " + string(cy));
if (mp_grid_path(global.grid,roam,x,y,cx,cy,1))
{
show_debug_message("path found");
path_start(roam, movespeed,path_action_stop, false);
}
else
show_debug_message("path not found");

I've drawn the circles as well and it seems he always goes to the south east.
 
Top