Using Mp_grids - [closed by author]

Status
Not open for further replies.

Evanski

Raccoon Lord
Forum Staff
Moderator
GM Version: GMS2 V2.2.0.258+
Target Platform: ALL
Download: N/A
Links: N/A

[Edit] I plan on redoing this video....eventually
sometime soon

Summary:
This is my first tutorial. Hope it was easy to understand.
It goes over how to use mp_grids

Tutorial:
Its kind of bad so I dont recommend watching this anymore
its still useful but I accidentally swapped the hcells and vcells
and the audio is to low and you need to use closed captions

Code:
[CREATE EVENT]
//create the grid
var cell_width = 32;
var cell_height = 32;

var vcells = room_height div cell_width;
var hcells = room_width div cell_height;

global.grid = mp_grid_create(0, 0, hcells, vcells, cell_width, cell_height);

//add collisions
mp_grid_add_instances(global.grid, obj_wall, true);


//create path
global.pathfind = path_add();
Code:
[ROOM START]
//destroy grid
mp_grid_destroy(global.grid);
path_delete(global.pathfind);

//recreate the grid
event_perform(ev_create,0);

Code:
global.target_x = (obj_cheese.x)
global.target_y = (obj_cheese.y)


//create the path and move
if (mp_grid_path(global.grid,global.pathfind,x,y,global.target_x,global.target_y,1))
{
        path_start(global.pathfind,10,path_action_stop,false);
}
 
Last edited:
N

NeZvers

Guest
I forgot about event_perform(ev_create,0) trick. Thanks for reminding!

EDIT: Don't read it's all wrong!
/*
I think destroying grid and path should be done in room_end.
You don't need to double movement on path -
Code:
mp_grid_path(global.grid,global.pathfind,x,y,global.target_x,global.target_y,1)
//and
path_start(global.pathfind,10,path_action_stop,false);
If you do check first
Code:
if(mp_grid_get_cell(global.grid, xTo, yTo) != -1) //if destination cell is reachable
{
    mp_grid_path(global.grid, global.pathfind, xFrom, yFrom,  xTo, yTo, spd); // set movement
}
*/

I started to mess with pathfinding recently, so you can correct me.
 
Last edited:

Evanski

Raccoon Lord
Forum Staff
Moderator
I think destroying grid and path should be done in room_end.
Destroying it doesn't matter until the next room is created, The reason I put it in room_start is so its all together and creates a new grid for that room to avoid the game using one grid of one room for a completely different one.

You don't need to double movement on path -
Code:
mp_grid_path(global.grid,global.pathfind,x,y,global.target_x,global.target_y,1)
//and
path_start(global.pathfind,10,path_action_stop,false);
If you do check first
Code:
if(mp_grid_get_cell(global.grid, xTo, yTo) != -1) //if destination cell is reachable
{
mp_grid_path(global.grid, global.pathfind, xFrom, yFrom, xTo, yTo, spd); // set movement
}
mp_grid_path(global.grid, global.pathfind, xFrom, yFrom, xTo, yTo, spd); // set movement
Thats wrong, mp_grid_path is just a way to get the information what your start and goal is. It doesnt work as a path. also where you put spd is only for telling the game if the moving object can move diagonally or not, not for its speed.
 
Status
Not open for further replies.
Top