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

[solved] return mp_grid coords after one step?

L

locklock

Guest
hello! I'm trying to do turn-based pathfinding and hit some trouble:



I'm using a mp_grid, and would like to supply a starting grid_x/grid_y, a target grid_x/grid_y, and return the grid position one step towards the goal.

however, all the mp_grid (and mp_potential_step, which looked promising) functions seem to only return booleans. Am I missing an obvious way to get these coords?

Code:
// where I'm starting from:

GRID = mp_grid_create(0, 0, 10, 10, 10);
PATH = path_add();
mp_grid_path(GRID, PATH, starting_x, starting_y, target_x, target_y, 0) ;


thanks for any help!
 
Last edited by a moderator:
L

locklock

Guest
no answers on this one? Is my question confusing, or is this a more difficult problem then it looks?

the image the mp_grid_path help files might be better then my diagram. Using the red line as reference:


xstart = 0;
ystart = 0;
xgoal = 8;
ygoal = 6;
mp_grid_path(mp_grid, path, xstart, ystart, xgoal, ygoal, false);
// ~ mystery code ~
and it would return (in this case) mp_grid[# 0,1]
 
C

CedSharp

Guest
You can get the x,y position that an instance would be at a specific position on the path using path_get_x and path_get_y.
Note that the position you give to this function is in percent, so from 0 to 1.

I would loop this using a small increment like 0.01 or even smaller, and convert the x and y position to a grid x,y position.
Then, the first values that do not match the start grid position is the first step :)

Regards ~
CedSharp
 
L

locklock

Guest
thank you ced, I'll try that out!

seems rather like reverse engineering though, as the path must have an array of grid coordinates at some point.
 
You're going to want to look at using 'path_get_point_x' and 'path_get_point_y'. Those will give you the actual coordinates of each point on the path.
 

Jakylgamer

Member
there possibly could be a better way of doing this but this is how i did it
Code:
mp_grid_path(grid,path,x,y,obj_player.x,obj_player.y,0); //grab the players position

var xx=path_get_point_x(path,1);//grab first point in path
var yy=path_get_point_y(path,1);//grab first point in path

mp_grid_path(grid,path,x,y,xx ,yy,0);//recalculate path to only the first point in path towards player
path_start(grid,2,path_action_stop,0)//then start the path
 
L

locklock

Guest
hm, so the first point in the path is the first grid position along the path (in room x,y)? That seems like it'd work pretty well.

I ended up just writing my own pathfinding code though. Certainly gives me control over what I want to do :p
and finding this amazing tutorial explaining pathfinding was good motivation. Here's the most basic example (Breadth First Search) translated into GMS from python:

Code:
frontier = ds_queue_create();
ds_queue_enqueue(frontier, grid[# grid_x,grid_y]);
came_from = ds_map_create();
came_from[? grid[# grid_x, grid_y]] = noone;

while ds_queue_size(frontier) != 0 {
    current = ds_queue_dequeue(frontier);
    for (i=0;i<ds_list_size(current.neighbors);i++) {
        if !ds_map_exists(came_from, current.neighbors[| i]) {
            ds_queue_enqueue(frontier, current.neighbors[| i]);
            ds_map_add(came_from, current.neighbors[| i], current);
            current.neighbors[| i].origin = came_from[? current.neighbors[| i]];
        }
    }
}

current = grid[# goal_x, goal_y];
path = ds_list_create();
ds_list_add(path, current);
while current != grid[# grid_x, grid_y] {
    current = came_from[? current];
    ds_list_add(path, current);
}
mostly for posterity, because I'm going to mark this thread solved!
 
Top