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

HTML5 mp_grid_get_cell continuously fails

J

jaber

Guest
Hi all,

for my HTML top down shooter I am trying to let a second AI character (lets call it follower) to follow the main charachter and for that I have created a grid

Code:
/// grid creation code
global.grid = mp_grid_create(0,0,room_width/32, room_height/32, 32,32 );
mp_grid_add_instances(global.grid,obj_box_collider,0);
and I have added the code to the step event of the follower
Code:
//step event
targetX = (obj_Player.x div 32)*32 + 32; 
targetY = (obj_Player.y div 32)*32 + 32; 


//find path
if path_exists(path) path_delete(path);
path = path_add();
if (mp_grid_get_cell(global.grid, targetX, targetY) == 0)
{
    if mp_grid_path(global.grid, path, x,  y, targetX, targetX, 1){
    path_start(path,4,path_action_stop,0);
        }
}
else path_delete(path);
for debugging purposes, I also printed out my Grid and the outcome of mp_grid_get_cell; I also printed a circle in the exact position where the function is checking

draw event of folower
Code:
draw_circle_colour(targetX, targetY, 100, c_white, c_black, false);
draw_self();
draw_set_alpha(0.3);
mp_grid_draw(global.grid);
draw_set_alpha(1);
draw gui
Code:
draw_text(902, 440, "get cell  " + string(mp_grid_get_cell(global.grid, targetX, targetY)));

as you see from my test video I have recorded, the mp_grid_get_cell is throwing -1 no matter where the charachter steps!

am I skipping something here? I would appreciate any advice or help
thnx
J
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
It should be checking cell coordinates, not room coordinates, so just don't multiply the DIV values by 32 again. Also, you would be far more efficient to create the path once in the create event then simply re-use it in the step event than have it potentially creating and destroying the path every step. Also, also, you would be even better farming off the grid path functions into an alarm and only checking it every few steps rather than every step, as you'll still get errors doing it this way, as under some circumstances the instance will actually path through walls due to the path being recalculated every single step.
 
J

jaber

Guest
It should be checking cell coordinates, not room coordinates, so just don't multiply the DIV values by 32 again. Also, you would be far more efficient to create the path once in the create event then simply re-use it in the step event than have it potentially creating and destroying the path every step. Also, also, you would be even better farming off the grid path functions into an alarm and only checking it every few steps rather than every step, as you'll still get errors doing it this way, as under some circumstances the instance will actually path through walls due to the path being recalculated every single step.
Thanks So much for the Goldworth advices ..U nailed it... issues are solved.. cheers
 
Top