+2 votes

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 getsimplepath 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....

in Engine by (217 points)

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.

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.

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.

hey, found the solution to this ?

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.