So sorry for this post, I’m clearly not the first one who’s struggled with this but I’ve tried endless permutations that I’ve found by Googling and searching this forum but nothing seems to be working for me.
What I’m trying to do: I have a 2D game that uses a Point Graph to control NPC agent movement. This Point Graph needs to get updated every time a new building is placed. (Essentially, NPCs can only ever move on these buildings: you’re building a tower, NPCs walk around on the tower.) Currently when I add a building, the pathfinding does not update until I activate a scan from AStarPathfinding. The problem is, it causes a noticeable stutter, and the player will be placing a lot of buildings. I’d like to use UpdateGraphs instead.
What I’ve tried: I’ve updated to the Pro version of A* Pathfinding. I’ve tried calling UpdateGraphs(Bounds b) directly. I’ve tried using the GraphUpdateScene component. And I’ve tried using active.data.pointGraph.AddNode(pos) directly as well. Nothing I do seems to make any difference. The gameobjects get added to the scene with the proper tag, they seem to get recognized by the AStarPathfinding object (I can toggle them on and off with the “Show Graphs” checkbox), but the pathfinding doesn’t use them until I initiate a scan.
Any help would be appreciated, and any more info you need I can provide.
Thanks so much, I’m otherwise very much appreciating this asset, it’s a huge help!
Unfortunately, discovering which GameObject’s have been added/removed is an operation that cannot be done efficiently in Unity. It requires iterating over all GameObjects.
Therefore, the point graph will not try to detect new/removed GameObjects when doing an update. A scan is required to detect this.
To make sure performance is good, make sure optimizeForSparseGraph is enabled in the point graph inspector.
However, unless you really need a point graph, I would recommend using a grid or recast graph instead, as they can be updated in a more performant manner.
Makes sense, I did try playing with the Sparse Graph setting and it did make it better.
The game is a top-down 2D game, I actually picked the point graph because I figured it would be more performant and I didn’t need anything fancier. Are you saying that a grid graph is more performant?
Yes, unless you spread out your nodes a lot using the point graph, the grid graph will most likely be faster.
I’m not sure how you are positioning your nodes, but if you are positioning the point nodes in a grid anyway then you definitely should use a grid graph. A recast graph can also be a good alternative in the beta version where 2D recast graphs are supported.
Hmm, I’m actually not sure which graph would be better for my situation. As far as I can tell, the grid graph is more optimized if you define one large grid at the start, then update sections of it as you place obstacles. I could modify the game to work like that, but the way it currently works is that agents can only navigate on buildings already placed. So essentially every time you place a building, you add a node, not an obstacle, and the grid just keeps getting bigger.
Can I just make each building its own 1x1 grid graph? Will agents succesfully navigate between different graphs?
If not I think a sparse point graph will be easier to work with after all.
Anyway thanks for your help.
Do you have a screenshot of your game?
Not sure how well it’ll show it off, it’s super basic. But sure.
You build a tower up as far as you can go. Each “building” placed becomes part of the tower. Each building also adds a node to the grid, so the agents can travel further and further up (bringing resources higher as they go, of course).
Here I zoomed into the Editor Scene view. You can see the point graph nodes, an example NPC and a magenta hexagon as a temp navigation target. I manually set the max horizontal distance to be longer than the vertical, so they can only go upwards if they find stairs.
As I’m learning your code I’m noticing something called a NodeLink, that might be useful to me in this situation no? It’s trivial to get each building’s neighbor, and I could just manually update the graph as I go. Is it meant for that?
Sorry for the noobish questions, like I said I’m learning this as I go. I’ll figure it out eventually!
Ah, it’s a 2D game like that.
In that case you probably want to keep your point graph. This type of side-view 2D is not supported with any graph type other than the point graph.
That looks like very few nodes, so I can’t imagine a sparse point graph would take very long to calculate at all.