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

Pathing for an RTS

S

squirrelah

Guest
Hello, im trying to make some pathing for an RTS and from what ive gathered if i want to use what gamemaker already has, and not make my own algorithm which seems like it would take forever, i have more or less two options. I've never used either of these before, and i was just wondering what the pros and cons are of using one versus the other.

for those who aren't familiar RTS's typically have the following features in regards to pathing.
1. A unit will move in as straight a line as possible.
2. You can que movement(and other commands in conjunction with movement), which means you can tell it to move to position x1, y1 and then after it does this action to the best of its ability move to x2,y2.
3. There are multiple units doing all of the above simultaneously.
4. There are buildings that can be added to the map that these units cannot move through.


option 1:
mp_potential_path seems like the more basic of the two as all i have to do is set up the pathing correctly, and then mark everything that i want the pathing to try and move around as solid. I
1. Seems like it will be easily achievable with this
2. Not really sure where to even start with this
3. Supposedly this takes up less computing power, but im worried about the movement being extremely clunky because of units bumping into each other.
4. Also doesn't seem to particularly be an issue.


mp_grid_path. This seems like what i should be using,
1. I've had some issues in testing where pathing doesn't move in as straight of a line, as possible but it moves diagonally at a 1 to 1 ratio and then moves in a straight line once its at the same x or y coordinate.
2. Also not sure how to do this
3. I dont think theres really going to be an issue here either unless i mess something up.
4. I think the mp_add_rectangle where a building is created and then mp_clear_rectangle when it gets destroyed would cover this but im worried that movement that started before a building was created/destroyed wouldn't register as the path was made before this happened.

TLDR; I have never used paths before, if someone who has used them can share some insight i'd be very appreciative. Also, how could you que movements?
 
A

anomalous

Guest
Pathing is complex, it involves collisions systems and movement/steering, and has to look good, and often has to work with multiple actors all doing this some sort of harmony.

You can start here:
https://help.yoyogames.com/hc/en-us/articles/216755858--GMS-S-Using-Mp-grids-To-Create-AI

The basic idea is you create a path using mp_grid, but then you move towards the path points, rather than "follow" the path.
You may still have issues with units overlapping into a single unit (if they don't collide with one another), or congo-line (lots following precisely the same path)

mp_potential is tricky to get looking right, but it's worth trying out.
If you're gonna try to make a game, IMO you'd want to fully explore them both anyway just to start learning GMS and what your options are. potential is trivial to set up usually. mp_grid will take a bit more, but is more likely going to give you consistent results.
 

Spam1985

Member
I'd say give grids a go. Have one object run something like this when the room is created.
Code:
 //makes a grid of your room of 16x16 tiles. Change cell_width and cell_height
to whatever best suits your game!
var cell_width = 16; var cell_height = 16;
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 obstacles to your grid. For an RTS this could be terrain objects or buildings. You may want to update this grid as and when buildings are placed or destroyed
mp_grid_add_instances(global.grid, obj_solid, false);
Now it's important to delete this grid when you're done with it.
So you might want to slap this room end or game end event.
Code:
 //delete me or suffer a memory leak!
mp_grid_destroy(global.grid)
For the units create event:
Code:
 my_path = path_add();
Then to get them to move along the path, you probably want a global mouse right pressed event:
Code:
 //if unit is selected we can move
if selected = true then
{    var my_speed = 3;  //movement speed
     //sets target position to mouse snapped to grid
     var goal_x = (mouse_x div 16) * 16 + 8;
     var goal_y = (mouse_y div 16) * 16 + 8;

     //sends the unit to the target
     if (mp_grid_path(global.grid, my_path, x, y, goal_x, goal_y, 1)) {
     path start(my_path, my_speed, path_action_stop, false);
     }
}
Have a play with it. I don't know if it matters but you might (or might not) need to set a unit to delete his path when destroyed.
destroy event:
Code:
 path_delete(my_path);
And to play it safe have all units be destroyed at the end of the game or room.
 
Last edited:
T

Taddio

Guest
Correct me if I'm wrong (could very well be!!) but if you want a nice and smooth flocking behavior (which RTSs require to feel good, imo) I think you WILL need to code your own algorithm, as I doubt GM has that with it's built-in functions.
 

Spam1985

Member
Oh yes it's a bloody nightmare. The example I gave should send all units to the exact same position and they will certainly overlap.

They'd each need some way to calculate a unique goal position to reach based on some kind of formula that I can't fathom. And other measures would need to be taken to stop them overlapping and passing through each other.

Also as a general rule you should always have YouTube playing Frank Klepacki music when testing the game.

Edit: Found a video that might be helpful to you. I haven't watched it (it's looooong) but maybe it'll work for you.
 
Last edited:
The best resource I've come across for pathfinding is: https://www.redblobgames.com/. GM's inbuilt pathfinding is cool for simple stuff but once you want anything more flexible you have to build your own. When you're building your first pathfinding algo, it'll probably take you at least a few days to really come to grips with what is going on, but after you start to really grasp the ideas in your bones, it's not terribly hard (not that it'll become super easy, but you'll figure out ways to accomplish what you want). Combine pathfinding with state machines (for instance, I often have a state where the next cell is picked, then a state for movement to it, then a state once the cell has been reached, breaking it up like that lets me run various checks and do calculations based on exactly what is going on at the moment) and you'll have yourself a nifty, flexible tool for sending things to places.
 
T

Taddio

Guest
@squirrelah http://harry.me/blog/2011/02/17/neat-algorithms-flocking/
This is an example of a flocking algorithm. If reading it scares you, you may want to shed a couple bucks in the marketplace to check out how the others have done it, and play with it until you get the feeling you either 1) understand and are able to adapt it to your game, or 2) write your own thing

I didnt try the Complete RTS engine, but the dollar I spent on the "how did they do that - RTS" asset was, I feel, well invested (if only to make me realise: make a RTS? No thanks, I could make 4 arcade games for the same amount of time spent, but thays just me :D)
 
Top