GMS 2 Squad Pathing

xorphinindi

Member
I'm working on a game that allows the player to control multiple units, but I'm having some difficulty with the pathing. Right now, I've got very basic movement working for a single unit. It follows a path set to it by a mouse click, avoids walls, and stays on the global grid. When I add a second unit and give them an endpoint, predictably, they move to the same point. However, I would like for each unit to recognize the others and automatically decide on a new endpoint for itself based on a formation or pattern and available space. One solution I can imagine is creating a new path for each unit individually, however, I am hoping to move up to 100 separate units eventually and am not sure if creating 100 unique paths at each mouse click will cause any problems. Certainly, at minimum, it doesn't sound very efficient code-wise so I'm hoping there is a simpler solution. I appreciate any tips you can offer. I'll provide my pathing code if that helps (I think I got much of this from a tutorial a while ago. I just started working on an old project).
Code:
/// unit create event
path = path_add();
=====================================================================
/// unit global left pressed event
var mx = (mouse_x div 32) * 32 + 16;
var my = (mouse_y div 32) * 32 + 16;

if(mp_grid_path(global.grid, path, x, y, mx, my, 0)){
    path_start(path, 4, path_action_stop, false);
}
=====================================================================
/// obj_grid create event
var cell_width = 32;
var cell_height = 32;

var hCells = room_width div cell_width;
var vCells = room_height div cell_height;

global.grid = mp_grid_create(0, 0, hCells, vCells, cell_width, cell_height);

// Add the walls
mp_grid_add_instances(global.grid, obj_wall, false);
 

NightFrost

Member
Creating a hundred paths, especially long ones across terrain that is challenging to A* pathfinding, can be very expensive. Pathfinding is a huge subject. Look into steering behaviors (flocking and separation especially) to get units to move in formation.
 

zATARA_0

Member
You can try having one unit act as the leader, have them follow a path, and have everyone else just folow that leader, keeping basically the same relative position to the leader. Also steering behaviors can defenatly work, they are a bit complicated tho.
 
Top