- Home /
Tree Structure: update root element (reference) for all classes
Hi everyone!
I might have a trivial problem, but I'm not finding the right "wording".
Lets say I have different nodes, which are connected like this
A - > B -> C -> D
All class objects have a root node, referencing to D*, means A returns D or C returns D. Therefore adding a new element like this
A - > B -> C -> D
F - > B
F should also return D
Now here is the thing, if the tree changes to
A - > B -> C -> D -> N
F - > B
all objects should now reference to N instead of D, without looping through parent of parent to get the final root. All objects should link to a "value", that can be changed by any object (static isn't a good option, there are different trees). Probably I require something like a pointer, but C# is different.
Hope someone can point me in the right direction :) Thanks
Answer by IgnoranceIsBliss · Jun 09, 2017 at 05:51 AM
I think the best answer is "Don't".
Unless you're constantly requesting the root asset, I wouldn't bother - make it traverse the tree to find the root and you'll never be wrong. Each asset that way only holds a reference to it's immediate parent.
If all of the objects in the scene are part of the same hierarchy, then you can just use a static variable that you hold the root node in. That way when you update the static, each node knows about it. However, if you have multiple hierarchies, this will fail.
public class Node { public static Node Base = null; }
FInally, you could always create a class that holds a value...
public class RootHolder {
public RoodNode N;
}
Then you could pass the same RootHolder object (instead of a reference to the root) to each and every node. Then when you update the node, update it once and every object will change.
That's a lot of memory usage for what might be a small payoff though. As I said, unless you're requesting the root node a LOT, there should be a more efficient way around it.
Your answer