Help with Adding Instances to MP Grid

Hello,

I'm working on a simple tower defense styled project. In the room_create event, I establish a default grid for enemies to use, and add some basic objects to be avoided to it:

GML:
global.nme_grid_ground = mp_grid_create(0, 0, room_width div 32, room_height div 32, 32, 32);
mp_grid_add_instances(global.nme_grid_ground, obj_building, false);
mp_grid_add_instances(global.nme_grid_ground, obj_solid, false);
mp_grid_add_instances(global.nme_grid_ground, obj_slow, false);
mp_grid_add_instances(global.nme_grid_ground, obj_water, false);
Enemies start on this path by default, towards the user's base unless there is a player-controlled unit that is closer:

Code:
    if mp_grid_path(global.nme_grid_ground, nme_path[w], x, y, target.x, target.y-8, 0) {
        path_start(nme_path[w],move,path_action_stop,0)
    }
The issue I'm running into is that when I build walls, which I want enemies to avoid, during gameplay (using the obj_building object type) the enemies ignore and just pass right through instead of going around. My assumption is that this happens because the new instance of obj_building has been added after the grid was created.

I would greatly appreciate any guidance on how to update the grid/enemy path, if thats not possible, a clean way to "reboot" the grid/enemy path each time I add a new building to the field.


Thanks,
 

EvanSki

Raccoon Jam Host
you need to update the grid when you place something down
then update each enemy's path to the new grid

just delete the old one
then recreate it
then have the enemy's restart their paths
 
I wouldn't clear the grid when it has to be updated, as I don't think placing instances is a particularly cheap process. If that's true, then maybe look at having the walls add themselves in as part of their create event - since your grid is global, and I believe the function accepts id

GML:
mp_grid_add_instances(global.nme_grid_ground, id, false);
If it doesn't accept id then mp_grid_add_rectangle is cheaper in my experience anyway, it just requires a bit more effort on your part to implement.

This way you don't have to replace everything. In the create event of the wall object it could then tell patrolling enemies to make a new path, and it would run that code right after it's added itself into the grid so you know their pathing will reflect the changes.
 
Top