Path Finding

M

MattD227

Guest
hey guys, so what im needing help with is that i want my player(s) on screen to move where i click, however i want the game to find the shortest distance to where i clicked while avoiding obstacles in the way.

For example, lets say i have a building and a doorway to go through, i want to be able to click in the building and they will find there way to the doorway and go inside. right now i have code that is allowing my player to move where i click but do not have anything set up for path finding. I can post this code for you guys if it is needed.

thank you guys in advance for helping me out
 

rIKmAN

Member
hey guys, so what im needing help with is that i want my player(s) on screen to move where i click, however i want the game to find the shortest distance to where i clicked while avoiding obstacles in the way.

For example, lets say i have a building and a doorway to go through, i want to be able to click in the building and they will find there way to the doorway and go inside. right now i have code that is allowing my player to move where i click but do not have anything set up for path finding. I can post this code for you guys if it is needed.

thank you guys in advance for helping me out
Take a look at this tutorial and follow it through, it will give you an understanding of how to use mp_grids for pathing.
https://www.yoyogames.com/blog/459/dynamic-mp-grids

After doing that it will be simple to add a mouse click to select target location for the path.
 
Last edited:
M

MattD227

Guest
Take a look at this tutorial and follow it through, it will give you an understanding of how to use mp_grids for pathing.
https://www.yoyogames.com/blog/459/dynamic-mp-grids

After doing that it will be simple to add a mouse click to select target location for the path.
so i have been following the tutorial and i think ive got an understanding on how it is working, but i do not know how to transfer it over to my game. ive tried replacing in the code where it would flag the obj_wall with my obj_land but it will not flag them as occupied space. i also cant find out how to get the path to be my mouse click if you would be able to help with that.
 

rIKmAN

Member
so i have been following the tutorial and i think ive got an understanding on how it is working, but i do not know how to transfer it over to my game. ive tried replacing in the code where it would flag the obj_wall with my obj_land but it will not flag them as occupied space. i also cant find out how to get the path to be my mouse click if you would be able to help with that.
It's hard to help without seeing your code, but have you tried drawing the grid using mp_grid_draw()and seeing what is happening with regards to which cells are marked as free / occupied.

For setting the path to end at the position of the mouse when you click, you would use the mouse coordinates as the
xgoal and ygoal arguments in the mp_grid_path() function when creating the new path.

Have a read of those linked pages as it explains everything in more detail.
 
L

LWDIV

Guest
Hello MattD227,

just create a new project with two objects (obj_player, obj_wall).

obj_player -> create event:
size = 16;
grid = mp_grid_create(0,0,ceil(room_width/size), ceil(room_height/size),size,size);
mp_grid_add_instances(grid,obj_wall,1); //add impassable objects
path = path_add();

obj_player -> draw event:
draw_path(path,x,y,true); //draw path
draw_self();

obj_player -> any click event you want:
//create path between player and mouseClick
mp_grid_path(grid,path,x,y,mouse_x,mouse_y,1);
//start new path
path_start(path,2,path_action_stop,true);

The obj_wall object dont need any code. Create for each object a simple srpite (just a square + a color (32x32)).
Place the player-object somewhere in the room and place walls. Click some and watch the magic :).
 
M

MattD227

Guest
Hello MattD227,

just create a new project with two objects (obj_player, obj_wall).

obj_player -> create event:
size = 16;
grid = mp_grid_create(0,0,ceil(room_width/size), ceil(room_height/size),size,size);
mp_grid_add_instances(grid,obj_wall,1); //add impassable objects
path = path_add();

obj_player -> draw event:
draw_path(path,x,y,true); //draw path
draw_self();

obj_player -> any click event you want:
//create path between player and mouseClick
mp_grid_path(grid,path,x,y,mouse_x,mouse_y,1);
//start new path
path_start(path,2,path_action_stop,true);

The obj_wall object dont need any code. Create for each object a simple srpite (just a square + a color (32x32)).
Place the player-object somewhere in the room and place walls. Click some and watch the magic :).
awesome! this worked for me, thank you
 
L

LWDIV

Guest
Great :). If u have any other question about the "path"-stuff just ask me.. i did spend alot time on it.
 
Top