Pathfinding Not Working Over Long Distances

I am creating a colony builder / town simulation game, and am having issues with the pathfinding function over long distances. When the pathfinding destination is close to the sprite everything works fine. However, when the destination is a ways away, the pathfinding never initiates. Has anyone run into this issue before? Are there any fixes? I’d like to avoid having intermediate destination “check points” because that would disrupt broader game systems.

I tried to upload photos of the preview and the code, but the forum threw me an error message saying new users aren’t able to embed media. However, it is just a simple “find nearest” + “variable = true” > “move to location” command.

Thanks,

Here is the code:

Is it failing to find a path or is the path action not always being triggered. Make sure the action is being triggered and maybe check if a path is being found with the pathfinder path found condition.

Are you using obstacles?

How many stonesmith objects have a total < 8?

Currently, it’s picking a random object and then checking if it’s total < 8. If the random object’s total wasn’t < 8 then it doesn’t do anything.

If you checked the variable before picking a random object then you would pick from a list of matching objects.

Imagine if you had 3 red and 3 green balls. You could pick a ball and check if it’s red or you could pick all of the red and then randomly pick one of them. Your success rate would be 100% as long as there are red balls. The other way it could be 50-50.

Thanks for the response. It definitely appears like it is failing to find a path, as the command to change the nearest Resource_1 to Resource_2 is triggered, but then the move command for Settler_2 malfunctions. The only variable that is different is proximity. When the Resource_2 is near to the Settler_2, the pathfinding command works perfectly. When they are far away, it doesn’t.

If I change the “move to position” command from the standard pathfinding to the LinkPathFinding extension’s move command everything works normally.

Does the standard pathfinding have a distance limitation? Any ideas on how to get it to work for distant target locations?

How far is it?

I don’t know about any distance limitation with the built-in pathfinder. The Nav Mesh pathfinder behavior has settings that seem to change the area.

You can try digging into the source code including the step methods, move action and also the obstacle behavior.

https://docs.gdevelop.io/GDJS%20Runtime%20Documentation/classes/gdjs.PathfindingRuntimeBehavior.html

I decided to do a test. My results were very inconsistent.

I added 4 targets at extreme locations 10,000 plus and minus. I didn’t use any obstacles. Everytime I touched a location, it picked a random target object and tried to move to it.

So, - 10k,-10k 10k,-10k, 10k,10k, - 10k,10k

I added a mover object and a text object. I then added the distance to the destination, the pathfinder destination and then the target position. I originally used the actually x and y but decided to give each target an ID with the relative position using left, right, top, bottom.

So, anywhere the mover object is. It means that a path wasn’t found. The destination defaulted to 0,0. But there wasn’t any pattern. Sometimes it found a path. Other times it didn’t.

I circled the ones that failed.

My events.

It’s far from scientific but my events are simple enough that I don’t believe that I had bugs in my concept.

It failed to go to the left, bottom but it also succeeded where it wasn’t circled.

1 Like

I asked Gemini about the code for the pathfinder. Among other things, it said there’s a fail safe like feature that prevents too many resources from being used. Meaning locked up or I guess crashing. It only allows so many iratations before assuming a path can’t be found.

My tests didn’t use obstacles. I guess that’s not part of the method. Yes, there no need to use the pathfinder without obstacles. I was just checking for size limitations.

I wonder if my results would be different with obstacles.

How far are you trying to move your object(s)?

I guess you could breakup the path although I’m not sure what the best method would be. I guess it would depend on the scene.

If there weren’t too many obstacles then breaking up the path using a straight path split up into sections should work.

I’m just concerned that if the path was way off the direct path then it wouldn’t work. In that case, I guess waypoints would work. Even that doesn’t seem to create the shortest path. It would depend on the path.

Gemini’s response.

In the GDevelop source code (specifically in the PathSearchContext), there is a hard-coded or property-defined limit on the number of nodes it can explore.

​Iteration Limit: To prevent a infinite loop or a “Total Freeze” of your browser/phone, the search stops after a certain number of iterations (often 2,000 to 5,000 nodes).

​Grid Math: If your cell size is 20px, a 10,000px distance is 500 cells away. A search that expands 500 cells in every direction covers a massive area.

​The Result: The algorithm hits its “Search Budget,” decides the destination is unreachable within that budget, and returns false. This is why your object simply won’t move.