How to use the Alternative Path Modifier?

I’m making a Tower Defense Game. It will have a “dynamic placement grid”, meaning that the game won’t allow the player build a tower if it blocks the path of the AI (how it happens in games like Sanctum or Tower Wars).
I tried using Penalty Areas, moving the penalty area when the mouse was over a square. When scanning, the node on the square became unwalkalbe so now we make a seeker search for a path, if it can’t make one, then the player can’t put a tower. Everything OK until a new AI spawns at this time, it can’t make a path either.
Instead of making validations on my AIs, or making a “stop” between waves (like in Sanctum, actually I want to make it like in Tower Wars), I wanna try this: make two paths to follow, the shortest one from point A to B, and another longer. So, if there are two paths, it means that the tower that is gonna be build in that place is not blocking the path for the AIs, but if there’s only one path, then the game won’t allow to build there.
So I wonder, how does the Alternative Path Modifier works? Can it help me with my little problem? Is there an example about how to use it?

Hi

I got good news for you. This is already a built in function.
See http://arongranberg.com/astar/docs/class_pathfinding_1_1_graph_update_utilities.php#a5cca2a0658c53d66d366a2ba495498f1
You can pass a GraphUpdateObject to that method an also two nodes (which would be the start and end point of the TD). It will only apply the GraphUpdateObject if it did not block the path between the start and end nodes.
There is also another overload which takes an array of nodes. You can use that to pass the start and end nodes and the nearest node to all your AIs, that will prevent AIs to be closed in by towers blocking the path to the target.

Um… Well, that GraphUpdateObject… How do you expect it to know what to update if you are not giving it that information. new Bounds() will just give it an empty area, so it will not update anything.

Have you read this page: http://arongranberg.com/astar/docs/graph-updates.php

Also, you will probably not want to include the “tower node” (node2) in the call since that node will be made unwalkable if the nodes around the placed tower are updated to be unwalkable.

Perfect! I’ll check it on this weekend.

OK, I have here a simple case of when the game should not allow the player to build a tower:

Now I have this “test code”:

`
// Initial and Final Points positions
Vector3 initio = GameObject.FindWithTag (“PuntoInicio”).transform.position;
Vector3 finito = GameObject.FindWithTag (“PuntoFinal”).transform.position;

// Nearest Nodes to Initial/Final Points and the place on the grid selected by
// the player (On this case, the space crossed by the solitary line next to the
// first tower in the pic)
Node node1 = AstarPath.active.GetNearest (initio).node;
Node node2 = AstarPath.active.GetNearest (placeSelected.transform.position).node;
Node node3 = AstarPath.active.GetNearest (finito).node;

List nodes = new List ();
nodes.Add (node1);
nodes.Add (node2);
nodes.Add (node3);

GraphUpdateObject guo = new GraphUpdateObject (new Bounds ());

bool result = GraphUpdateUtilities.UpdateGraphsNoBlock (guo, nodes, true);

// Always printing “true”
print (result);
`

Now, every time the player clicks on the space this code is executed, and always results true. If I make the “node2” unwalkable (node2.walkable = false;) I’ll always get false, even when there is no other tower placed.

So, Is there another to say “this node is banned, use another” using this code above?
Basically, Am I still able to develop the initial idea whit this code?
If not, What am I doing wrong?

By the way, about the “Bounds” class. I used some Vector3 and part of the code of GraphUpdateShape class (public Bounds GetBounds ()) trying to get a different result… unsuccessful.

1 Like

The little green squares in the middle of the space next to the first tower is the area created the by the points of a GraphUpdateScene component that I used the first time for the validate of the tower placement:

"I tried using Penalty Areas, moving the penalty area when the mouse was over a square. When scanning, the node on the square became unwalkalbe so now we make a seeker search for a path, if it can't make one, then the player can't put a tower."
And for the Bouns thing:
"By the way, about the "Bounds" class. I used some Vector3 and part of the code of GraphUpdateShape class (public Bounds GetBounds ()) trying to get a different result... unsuccessful."
I used all the area where the withe squares are.

I don’t have a lot of time for this during the week, but I’ll keep trying until it is done.

Done! I’ve finished this. Just how you said, the bounds were the problem and I remove the “node2” from the list.

Bounds bounds = new Bounds (placeSelected.transform.position, new Vector3 (1,0,1));

I was a little confused by that class. First I thought it wasn’t part of Unity, so I was searching in the C# documentation and also in A*Pathfiniding Project’s.

Anyway, thank you very much for your help.

By the way, here is the final code, for those who are checking this page:

`// Initial and Final Points positions
Vector3 initio = GameObject.FindWithTag (“PuntoInicio”).transform.position;
Vector3 finito = GameObject.FindWithTag (“PuntoFinal”).transform.position;

// Nearest Nodes to Initial/Final Points
Node node1 = AstarPath.active.GetNearest (initio).node;
Node node2 = AstarPath.active.GetNearest (finito).node;

// We use the transform of the place selected
GraphUpdateObject guo = new GraphUpdateObject (new Bounds (transform.position, new Vector3 (1,0,1)));
guo.modifyWalkability = true;
guo.setWalkability = false;

bool result = GraphUpdateUtilities.UpdateGraphsNoBlock (guo, node1, node2, true);

// Do whatever you want to do with this variable
print (result);`

As simple as that.