Checking path using get_simple_path in advance

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By PixelWizzard
:warning: Old Version Published before Godot 3 was released.

I am currently working on a simple 2d tower defense game, where the towers can be placed anywhere on the map. The path for each enemy from its spawn to its goal is calculated using the get_simple_path method, provided by the navigation2D node. What I would like to do now, is checking, if there is still a possible path, before I place a new tower on a certain position. In other words, I would like to know, if a tower blocks the path, before I even placed It on theh field.
Does anyone know, if it is possible to do that?
And yes, I know that A* pathfinding exists, but I never worked with it and would probably have to write the whole code for it myself…

Have you tried adding the tower, then checking if the path exists and then deleting it again if it blocks the path? I’m not sure how pathfinding is implemented but that might all work in one frame so that the player wouldn’t notice it. If it’s simpler you can also just add a simplified block to do the test so that all the rest of the tower placing logic is not executed.

umfk | 2016-03-04 16:36

Yeah, I already tried that. The interesting thing about this is that it allows me to block the path completely, but if I try to add another block after that, it returns no possible path, so I can’t place another obstacle once the path is blocked.

PixelWizzard | 2016-03-04 16:39

That sounds like pathfinding does not update during a frame. Maybe it is even done in a separate thread? You could still use this method but you have to do it across several frames. For example, as soon as the path does not exist anymore, flag the last placed tower as invalid. If the calculations are quick enough the user might never notice it. Or you place a new tower only after an invisible test block was succesfully placed for 5 frames without blocking the path.

I’m out of ideas. Let’s wait for someone with more insight into Godot’s pathfinding implementation :D.

umfk | 2016-03-04 16:51

hey, found the solution to this ?

quizzcode | 2020-05-10 23:11