Grids
Home Up Grids Paths Path Finder Demo

 

List of motion planning grid functions

mp_grid_create(xstart, ystart, hcells, vcells, cellwidth, cellheight):

This function generates the empty grid that will be used for path finding and returns its ID. You will probably create one grid for a room, not one per monster instance, so the grid might be created by a "controller" instance
xstart, ystart are the top left coordinates of the grid
hcells, vcells are the size of the grid in cells (not pixels)
cellwidth, cellheight is the size in pixels of a cell

If your maze walls are 32 by 32 blocks you might use:

grid_id=mp_grid_create(0,0,floor(room_width/32),floor(room_height/32),32,32);
mp_grid_destroy(grid_id)

Dispose of the grid, if you created the grid in the instance create event you should dispose of it in the instance destroy event

mp_grid_path(grid_id, path_id, xstart, ystart, xgoal, ygoal, allowdiag)

This is the big one, the function that actually solves mazes. It returns a boolean, true if it succeeded in finding a path. The start and end points of the path are the points you supply, and the points in between are the centres of the grid cells the path passes through
grid_id The grid to be solved
path_id The ID of a path generated by path_add (don't use a predefined path) that will be overwritten with the maze solution
xstart,ystart The location you want the path to start from, typically the location of a monster
xgoal,ygoal The location you want the path to end at, typically the location of the player
allowdiag determines if the path can contain diagonal sections, usually false

Functions to allow the grid to be manipulated directly

mp_grid_add_cell(grid_id, h, v)
mp_grid_clear_cell(grid_id, h, v)
mp_grid_get_cell(grid_id, h , v)

The above functions work in grid coordinates not room coordinates, and would be used where you have your own rules for what parts of a room are passable.

Functions to add room features to the grid

 
mp_grid_add_instances(grid_id, obj, prec)

Mark any cells covered or partially covered by instances of obj. If prec is true then the accurate collision mask will be used otherwise the bounding box will be used

mp_grid_add_rectangle(grid_id, x1, y1, x2, y2)

Mark a rectangle of cells as obstructed using room (not grid) coordinates

mp_grid_clear_rectangle(grid_id, x1, y1, x2, y2);

Mark a rectangle of cells as clear using room (not grid) coordinates

Other functions

mp_grid_clear_all(grid_id)

Mark the entire grid as clear

mp_grid_draw(grid_id)

Draws the grid on screen using red to indicate an obstructed cell and green to indicate a clear cell. The GMS manual warns that this function could be slow and should only be used for debugging.