I’m using a custom node graph, where sets of nodes are created by “rooms” and the nodes at the edges of the rooms then get connected with the adjacent rooms.
This happens at runtime, and the rooms can change (e.g. in size) in which case I have to recreate the nodes of the room. Now my problem is that if I just add the new nodes to the connections of the rooms around it, the old nodes remain and are still referenced.
Normally I’d just check every old node for its connections and remove it from those, but some rooms have one-way connections that I can’t easily trace back this way.
What would be a good (cheap) way to find all references to nodes that I want to be discarded?
Um… Unfortunately, there isn’t really one. One solution would be to set node.walkable = false and then you could loop through all nodes regularly (but not every time) and remove invalid nodes (you could use some kind of marker on the nodes to mark that they are invalid, e.g Bit15. Node node = blah; node.walkable = false; node.Bit15 = true; // Mark for removal
This seems to work. I’ve now given all nodes a “destroy” flag. Units now check if the node they’re going to is “destroyed”. If so, they check if the node they came from is also destroyed.
If both are destroyed, they teleport to the closest valid node (it’s likely they’re in a deleted room now).
If only the next is destroyed, they search a new path.
Every now and then the game runs through all rooms and cleans up connections to destroyed nodes. I can probably make this happen over multiple frames later, for even less performance impact.
Oh, also. Since this cleanup modifies the graph, you might want to safely update it (at least if you are using multithreading) since otherwise, strange errors could occur rarely.
See RegisterSafeUpdate. Pass a reference to your cleanup function there. One positive thing is that since you (probably) does not use any Unity specific API stuff inside that function, you could pass threadSafe as false to that function, and then if you are using multithreading, the cleanup will be done asynchronously in another thread. Neat, huh : )