[Solved] Moving armies in a grand strategy game [pathfinding]

I’ve made a few other posts about my grand strategy game pretty recently. I’ve worked on the game quite a bit constantly, and I present my next issue:

For those who don’t know what a grand strategy is/what my game is: Here is an image and an explanation.

It is a game where you select a country (I selected Castile in this image) and do stuff. Currently there’s barely anything to do.

(The map is very unfinished, the whole world will be made eventually but this is what I have made so far.)

A VERY important thing about this game:
The map is divided into “provinces” which you can see in the image. Each province has a variable on what country it belongs to, and they are colored accordingly. Each province is a DIFFERENT OBJECT in the same group, not different instances of the same object.

Now, objects that ARE created with code are the armies (the little numbers.)
You only see the ones of the country you selected.
Now, each province also has a variable on how many armies are created there at the start of the game. This variable is only useful right when the game loads and becomes obsolete after.

The armies are text objects, and they are instances of the same object. They all have the armyTroops variable which indicates how many troops are in them, and that number is displayed on them.

Now, army movement. I created a simple movement thing before this but realized the armies could move anywhere on your country instantly so yeah, it got deleted.

How I want armies to move is you left click on one to select it, and right click where you want it to move.

There are two ways this can go:

  1. Armies move from province to province, their movement is defined by provinces.
  2. (This might be easier to do) Armies move freely and can go anywhere, not fixed position by provinces.

For both of these, they shouldn’t be able to go in water. One of the downsides to #2 is that it’ll probably be harder to enforce the water rule for it.

Here is all the code for the armies (trust me, there isn’t any other code which is useful for this issue. I barely have 3 screens worth of code.)

If you really want more of my code, I’ll post more of my code in a reply, but I don’t think it’ll be necessary.

1 more thing, time elapses in this game, and you can pause and unpause it with SPACE, there is a variable TimeSpeed which can be 0.1 or 0, when it is 0.1 time elapses, when 0 it doesn’t. Armies should freeze what they’re doing when TimeSpeed is 0.

Anyway, I think that is all I wanted to say, I hope I didn’t forget something important.

2 Likes

The events you shared got nothing to do with moving the army but creating them?
Do you have any problem with that?

If you want to move the army using pathfinding and you don’t want them to go in to water, the most simple way to go about this is to cover the waters with some invisible pathfinding obstacle so the pathfinding going to avoid this areas. To be more realistic, could also just simply give more cost to the water areas so the pathfinding going to prefer to find a path on land but if there is no path available then going to go through water, but it is going to slow the army down. You can set this up in the pathfinding properties.

If you want the army to move center point to center point of each province and not freely, the most simple way to go about this is to have each province as an individual object and when the player click it, literally use the center point of the object/province as the destination for the army. Otherwise you want to store the center point of each area in a structure variable maybe but then you still need to know which province is it the player clicked, so I would go with having each province as an individual object.

If you don’t know how to use pathfinding, there are some examples included with gdevelop, search for “pathfinding”.

1 Like

To freeze the army when the TimeSpeed is 0, you can reduce the speed of the army on the path to 0 to stop them using the action to change speed on path and then increase their speed on the path once the TimeSpeed is 0.1

1 Like

Thanks for everything! A few things to note:

No code showed because I literally haven’t written any code with pathfinding
Yes, I will make water impassable, yes, I need to learn pathfinding, will do.

Thanks, I just wanted to generally see how to do it :smiley:

How do you make all the provinces not controlled by yourself obstacles?

Like, you can’t move your troops through other countries’ territory.

Remember, it’s dynamic what provinces are controlled by you.
You can gain or lose provinces. Who owns provinces at the start will change.
Keeping in mind that, how do I make enemy provinces obstacles?

If each province is an individual object as I was suggesting, you can add the PathfindingObstacle behavior to each province/object and enable/disable the behavior. So if you control the province, pathfinding obstacle is disabled so troops can move freely, if you don’t control the province then enable the behavior and you won’t be able to move troops in to that area using pathfinding.

An other way is simply use an object variable to flag if you control the province so when you click the province to move troops to that location, you could check the value of this variable and set the destination for the troops to go there only if the value of this variable indicate that you control the province. If you don’t control it then just do nothing, don’t set the destination and the troops won’t move…

Ok thanks :smiley: :smiley: :smiley: :smiley:

How do I make the water stop you? I made an object that is just the border from water to land all the way around what I’ve made so far of the map (Iberia + a third of France.) Problem is, how do I make the collision mask just the border? Or just the water? It would take SO long to make a custom collision mask, and even if I did, GDevelop just gives me the “polygon is not convex.” Is there any way around this?

One way to go about this is to use an object dedicated to be an obstacle and use this single object everywhere like a wall instead of editing the collision shape of each piece manually.
However, you may need to link these “wall” objects to each province they belong to so when you click the province and it is controlled by you, then you can disable the walls and if you don’t control it then you can make sure you enable the walls. Linking objects can be a complicated topic for beginners so I don’t attempt to explain it here. There are examples included with GDevelop have a look.

You can add multiple collision masks to one object to work around this error but if you going to have hundreds of objects it can hit performance and slow your game down if each objects has multiple very complex collision masks, I would not recommend it.

In general the less complex the collision mask is the better, don’t try to be very accurate because it is going to slow your game down. Is it really necessary to be so accurate to follow the exact shape of the borders and lands? I mean, you should ask this question to your self. If all you do is more troops around the map then I would think it is okay to be a little less accurate and don’t follow the exact shape of the land and borders and make the collision mask maybe a bit bigger or smaller to avoid very ugly situations but it is your game, just sharing my thoughts.

1 Like

I already made the provinces be obstacles when they’re not controlled by you.
For the water, yeah I’ll go with your suggestion. IK it doesn’t have to be 100% accurate around the border, I’ll just add many instances of a water block (I will probably need like 30-50 instances) which is an obstacle. Thanks for your help :smiley: