|Topic was automatically imported from the old Question2Answer platform.
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 the field. (Placing, checking and then removing it isn’t an option as it involve cost etc…)
Does anyone know, if it is possible to do that?
You don’t need to put the whole block instance and remove it.
However, you do need to disconnect the new coordinate where you are trying to put the block from your pathfinding system to see if the starting point is connected to the end point.
That means, you need to check the state of the pathfinding map where the new coordinate should be perceived as not passable. Then see if there’s any available path. If no, do not permit the action of putting the block. If yes, execute the action.
By saying pathfinding map, I mean anything that represent the world of available paths (only care about paths), it’s not neccesarily a map, depending on how you do the pathfinding, like Navigation2D, it can be different, but the idea is the same.
Overall, the idea is that when pathfinding, we only care if the coordinates of the world are connected (passable) without considering anything else (Monsters, combat, etc.), it should be separated from the game world. Then, before placing the block, we view where you just clicked on as a blocked path. After the action (Either executed or not), if you are keep tracking of the world available paths, simply updating it according to what action has been executed.
Hey thanks for your reply.
But there are not going to be any “click”.
Its meant to be as i’m “hovering” on top of the tile.
Any idea on how to run a simple_path + 1 tile not “busy” yet ?
quizzcode | 2020-05-11 07:47
Ok. If you’re using Control node (like Button), there’s a signal
mouse_entered. So instead of using signal
pressed, you just change it to
pressed is when mouse clicked while
mouse_entered is when mouse enters the tile (Use
mouse_exited if needed, when mouse enters but not yet exited, it’s hovering)
If you are using other nodes for detecting mouse events, there should be similar signals like
mouse_entered. If can’t find one, you can always create a child node like Button for detecting mouse inputs. (Tip: Remember to turn off the property
flat so it’ll be invisible)
The action function doesn’t need to be changed. It’s just what triggers the path checking function and the actual action function. (If you need clicking, then call the function when clicked; If hovering, then call the function when mouse starts hovering)
Also, because you’re just hovering (Not even trying to do the action of placing the blcok), you just need to call the path checking function without needing to worry about the action function. (But you may need to create a variable to keep the info. For example: A bool variable
var is_current_coordinate_valid = false)
Here’s the outline of the steps:
- When mouse starts hovering.
- Perceive the coordinate where the mouse hovers as not passable (Change the pathfinding world state)
- Check if the monster spawning point can find an available path to the target point. (Pathfinding function)
- If yes,
is_current_coordinate_valid = true.
Extra functions you may need:
When mouse exits the tile,
is_current_coordinate_valid = false.
When you actually start trying to put the block, permit the action only if
is_current_coordinate_valid == true
Sween123 | 2020-05-11 08:06