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

GML Pathfinding problem

Hey guys,

I`m trying to make a city builder and I have a problem with the pathfinding. I want to make the people walk only on roads.

For the time being I use the following setup:

1. An object that creates the grid on its Begin step and then has it destroyed on the end step. The same object defines what the no go tiles are through mp_grid_add_instances.
2. Path_start code on the object that moves (people in this case).

This works but of course the road system is not followed as the grid calculates a path that does not take this into consideration.

I tried defining empty space through mp_grid_add_instances but either it doesn`t work or I don`t know how to do this.
I thought of filling the map with obj_grass instead of tiling the map and then I can add obj_grass to mp_grid_add_instances. However I have a 6000x6000 room and filling it with 64x64 objects just for the background might not be wise for performance.

Is there a solution or am I approaching this the wrong way ?
 
K

kevins_office

Guest
Hard to guess as a lot of information is missing. Here are my thoughts reading your post.

Dont create grids every step. Have one grid control object, create the grid in the create event.
Only create one grid and let all people objects share that one grid.
Use one of the user events in that grid control object for marking the forbidden cells.
Run that user event in the create event, then only call it again if the map changes, adding / removing roads or buildings.
Then you are not doing the work every step.

It sounds like you are not using mp_grid_add_cell as you keep mentioning mp_grid_add_instances.
the mp_grid_add_cell is what tells the path finding which squares to avoid, you want to add all grid cells that are not roads, then they will only travel on roads.
For debugging you can use mp_grid_draw to see which cells can be traveled (green) and which are blocked (red).

It is your preference if you want to add instances to the grid, i personally do not think it is necessary. Side effect being people can pass through each other.
If that is something you want to avoid one method would be to use mp_potential_* to move the people from grid path node to node.

Instead of the people following a rigid path set by the grid, you use the grid path as way points. As you know grid path puts a node in each cell.
Use mp_potential_* to move from node to node (way point to way point). mp_potential_* can reference objects to avoid such as other people so they walk around each other getting to the next grid node.
This method require more code and is a little more complex.... Or just let them walk through each other.
 
Top