- Home /
[ECS] Reference an entity to another entity
Hello there,
I am currently trying to learn using the ecs. For my current project i want to build a tree-like structure. In normal OOB this would be easy but slow since the tree might get quite large. So there is a 'HeadNode' and 'Node' entities. Each should be able to hold a reference to multiple 'Node' Entities.
One possiblity might be an array of entitys as a component on the "parent-entity" though i can't find the right types to introduce that as an component.
I am so far not able to find any useful information on this topic... Most posts are with a deprecated API or use some kind of ID-System as a workaround.
I'd be grateful for any information if this is even possible in ecs or if there is a method that might help me here. Let me know if you need more information on the project/problem.
Thanks in advance!
Answer by Captain_Pineapple · Jul 30, 2019 at 03:07 PM
For anyone intrested:
this is solvable by adding a DynamicBuffer
to the entity. The type of this buffer has to be a struct that implements IBufferElementData
which can have a member to hold an Entity.
basically like this:
[Serializable]
public struct ChildBuffer: IBufferElementData
{
public Entity childNode;
}
So does this component hold a single entity or a collection of entities?
basically just one, but you should be able to extend this to multiple entities. Sadly you can not use arrays in there if i remember correctly.
An IBufferElementData is an element in a DynamicBuffer. In this case the DynamicBuffer is the collection of ChildBuffer, each of which holds an Entity.
Your answer
![](https://koobas.hobune.stream/wayback/20220612205543im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to mimic Unity's Components based System 1 Answer
How do I assign a public variable inside a JobComponentSystem(ECS)? 1 Answer
[ECS] Set data to a SharedComponent of Type RenderMesh from EntityCommandBuffer 1 Answer
Accessing local system ( File Browser ) 2 Answers
Question about new Component system 2 Answers