I am new to DOTs and wondering how to implement Graph and Tree traversals. All of the documentation talks about iterating through entities in memory order or chunk-by-chunk order. I think this would not work for graph or tree algorithms where the Entities that correspond to the nodes of the graph/tree need to be visited in a specific order.
I looked at the code for the Unity.Transforms LocalToParentSystem.cs to see how the hierarchy tree is traversed. The System uses IJobChunk to visit the Entities block-by-block, in the order that they appear in the block. I don’t understand how this can work. How does the system ensure that a parent’s LocalToWorld is calculated before a child’s LocalToWorld? What if the parent’s LocalToWorld is in another chunk that is being executed by another job?
What am I missing here?
Here it the IJobChunk Execute method that visits each entity in the chunk, then updates the children of that entity. How does it ensure that the parent’s are always visited before the children? What if a child appears before its parent in chunkLocalToWorld, then it would update its children’s LocalToWorld before its own LocalToWorld was updated.
LocalToParentSystem.cs
public void Execute(ArchetypeChunk chunk, int index, int entityOffset)
{
bool updateChildrenTransform =
chunk.DidChange<LocalToWorld>(LocalToWorldType, LastSystemVersion) ||
chunk.DidChange<Child>(ChildType, LastSystemVersion);
var chunkLocalToWorld = chunk.GetNativeArray(LocalToWorldType);
var chunkChildren = chunk.GetBufferAccessor(ChildType);
for (int i = 0; i < chunk.Count; i++)
{
var localToWorldMatrix = chunkLocalToWorld[i].Value;
var children = chunkChildren[i];
for (int j = 0; j < children.Length; j++)
{
ChildLocalToWorld(localToWorldMatrix, children[j].Value, updateChildrenTransform);
}
}
}