How to use Mp_grids -Evaccoon

Discussion in 'Tutorials' started by Evan Malinowski, Nov 13, 2018.

  1. Evan Malinowski

    Evan Malinowski Member

    Joined:
    Apr 17, 2018
    Posts:
    233
    GM Version: GMS2 V2.2.0.258
    Target Platform: ALL
    Download: N/A
    Links: N/A

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

    Tutorial:


    Code:
    [CREATE EVENT]
    //create the grid
    var cell_width = 32;
    var cell_height = 32;
    
    var vcells = room_width div cell_width;
    var hcells = room_height 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 div 32)*32+16;
    global.target_y = (obj_cheese.y div 32)*32+16;
    
    
    //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: Nov 13, 2018
  2. NeZvers

    NeZvers Member

    Joined:
    Mar 24, 2018
    Posts:
    194
    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: Nov 15, 2018
  3. Evan Malinowski

    Evan Malinowski Member

    Joined:
    Apr 17, 2018
    Posts:
    233
    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.

    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.
     
  4. NeZvers

    NeZvers Member

    Joined:
    Mar 24, 2018
    Posts:
    194
    You are right, just checked my project.
     
    Evan Malinowski likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice