GML [SOLVED] Could this cause a memory leak? (mp_grid_add_instance in Step event)

Dr_Nomz

Member
So recently my NPCs were acting stupid by clearly ignoring an open door and trying to walk around it, or worse yet, walking THROUGH a closed door, since the above code was in the create event of the grid object.

I fixed this just fine, but I want to ask this to make sure this won't cause problems down the road.

Create event for all enemies:
Code:
var cellh = 50;
var cellw = 50;
var horzcells = room_width div cellw
var vertcells = room_height div cellh
global.grid = mp_grid_create(0,0,horzcells,vertcells,cellw,cellh);
Step event for enemy:
Code:
if (mp_grid_path(global.grid,path,x,y,obj_Character.x,obj_Character.y,true)){
  path_start(path,4,path_action_stop,false);
  mp_grid_add_instances(global.grid,obj_Wall,true);
}
And don't worry, there grid object has an end game event that deletes the grid as well. That said, is it an issue, or should it be fine?
 

Dr_Nomz

Member
Still relevant, but just asking about this now:
mp_grid_add_instances(global.grid,obj_Wall,true);

I don't use most of the other code so much, but I'm still putting that^ in the step event. (Well, END step event at least.) Is that gonna cause a memory issue or is it fine?
 

rytan451

Member
I've identified the memory leak. Every time a new enemy is created, you're creating a new mp_grid, overriding global.grid. When that happens, the old grid is not deleted, thus causing the memory leak.

Instead, you might check if global.grid exists before creating it, or even only creating it in the create event of the grid object. (If you've already made a dedicated manager for the movement planning grid, why wouldn't you make it work for the creation and deletion of it?)

The reason sometimes you go through walls is in your step event. I'll add comments in the code to explain exactly what is happening.

Code:
///@desc Step event

if (mp_grid_path(global.grid,path,x,y,obj_Character.x,obj_Character.y,true)) { // Create the path through the grid. If it succeeds:
  path_start(path,4,path_action_stop,false); // Start the path
  mp_grid_add_instances(global.grid,obj_Wall,true); // Add the obstructions to the grid (this does not retroactively update paths)
}
Also, your step event has the enemy recompute the path. This means that every step, it's running the entire A* algorithm again, which is slow in larger grids. What you could be doing instead is to only change the path only when the path becomes obstructed (or on some other trigger).

Finally, to reply to your other comment, what that does is it adds all the wall objects to the grid. It does not cause a memory issue, but there are more efficient ways of doing things. Instead of adding all the walls to the grid every step, perhaps you could make it such that each wall adds itself to the grid when it is created?
 

Dr_Nomz

Member
Alright thanks for clearing that up. Gonna open up a new thread for a question about pathfinding now, so hopefully I can get this working better.
 
Top