Trouble working out WillBlockPath - Solved

I’m building a TD game. I have pathing working well, I can place towers and enemies path around them. I need to implement checks for path blocking and I’m having trouble wrapping my head around it.

I found mention of WillBlockPath in the old forums, but I’m a bit too new to this to figure it out. I’m wondering if someone could give me a short code example of how to use WillBlockPath. I’m using Javascript, but I can convert C#.

Cheers

I worked it out using the newer UpdateGraphsNoBlock which doesn’t appear to have any documentation.

Here’s the function I had to dig out of the Pathfinding code to work it out.
public static bool UpdateGraphsNoBlock (GraphUpdateObject guo, Node node1, Node node2, bool alwaysRevert = false)

and the messy bit of code I used to implement it.
`var GUO : GraphUpdateObject;
var n1 : Node;
var n2 : Node;
var nEnemies : GameObject[];
nEnemies = GameObject.FindGameObjectsWithTag(“Enemy”);
GUO = GraphUpdateObject(newObject.collider.bounds);
n2 = AstarPath.active.GetNearest(enemyGoal);

for (var ni : int; ni < nEnemies.Length; ni++)
{
n1 = AstarPath.active.GetNearest(nEnemies[ni].transform.position);
if (!GraphUpdateUtilities.UpdateGraphsNoBlock(GUO, n1, n2, true))
{
placeable = false;
}
}`

I also do one more check from a dummy object I have sitting in the enemy spawn point to make sure the new object doesn’t block the path from the spawn, otherwise it only checks from where the enemies currently are.

Hope that helps someone.

Hi

Weird, not sure why that class didn’t get any documentation generated, I think there is in-code documentation for it… perhaps something was malformated for doxygen.

I am not sure if this overload exists in your version (might be added for 3.2), but I think the overload exists so that the UpdateGraphsNoBlock takes a list of nodes instead of just two nodes. That would make your code easier and a lot faster.

Here is the documentation for that overload in the development version:
`/** Updates graphs and checks if all nodes are still reachable from each other.

  • Graphs are updated, then a check is made to see if the nodes are still reachable from each other.
  • If they are not, the graphs are reverted to before the update and \a false is returned.
  • This is slower than a normal graph update.
  • All queued graph updates and thread safe callbacks will be flushed during this function.

  • ote This might return true for small areas even if there is no possible path if AstarPath.minAreaSize is greater than zero (0).
  • So when using this, it is recommended to set AstarPath.minAreaSize to 0. (A* Inspector -> Settings -> Pathfinding)
  • \param guo The GraphUpdateObject to update the graphs with
  • \param nodes Nodes which should have valid paths between them. All nodes should be walkable or \a false will be returned.
  • \param alwaysRevert If true, reverts the graphs to the old state even if no blocking ocurred
    */
    public static bool UpdateGraphsNoBlock (GraphUpdateObject guo, List nodes, bool alwaysRevert = false);`

Any idea how I can run that from Javascript? I’m not having any joy passing an array of Node[] from javascript to that C# function, it wants a list.

– Edit
I’ve got it.
var nodes : List.<Node> = new List.<Node>();