Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

ECS understanding

Discussion in 'Scripting' started by UltiPangol, Jun 16, 2020.

  1. UltiPangol

    UltiPangol

    Joined:
    Nov 12, 2019
    Posts:
    4
    Hello,

    I am currently studying ECS and before getting into code, I want to be sure I understand all its subtleties.

    Is it possible to store entities in a component?

    the objective being to cut the big Gameobjects into as many components as possible in order to optimize the data processing.

    But if I have a GameObject that has GameObjects that has GameObjects, for example, a tree has branches that have leaves and fruits ...

    Each one having its own components, to be treated by systems.

    How to reproduce this tree of objects? where to store references to entities for linked together in order to easily access the components of said entities?

    I watch videos, and read articles, maybe I haven't yet come across the answer to my question, but google doesn't seem to understand my question and I only find the eternal "how to get started "which only mention the basics, which I understand.

    Thank you in advance for your answers, hoping that you can light my lantern.
     
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    10,004
    You are still thinking in objects. No tree, no branches.

    Well, that's not true, but not this way you described.

    You have trees and you have branches. Many branches. Trees may only contain pointers to branches not the branches.
    Just watch the ECS presentations from Unite Copenhagen they describe the memory layout better than me.

    Put it this way:
    you have GameObjects (N amount), these GOs have position, rotation and scale components and I don't know, Color.

    In OOP, it described as a GameObject class which owns and contains the Position, Rotation, Scale and Color objects and values.
    In ECS, you have four arrays:

    Array<Position>, Array<Rotation>, Array<Scale>, Array<Color> (in real life these are float3, float3, float, float4 types)
    You have another array:
    Array<Entity>

    This Array<Entity> is an array of int (for this example, in real life it is a bit more complicated).
    Every int value in this array is an index to the other four arrays. And at the same time this int value IS your Entity.

    So the entities[2] has a value, let's go with 13 because why not. 13 is the Entity (as an ID).
    So, the Entity 13 has (or rather point to) four components:
    Positions[13] -> this is the entity's position
    Rotations[13] -> this is the entity's rotation
    Scales[13] -> this is the entity's scale
    Colors[13] -> this is the entity's color

    But, when you change/process things, like move around the entities, you don't need colors or scales. But you need positions and possibly rotations. So when you move your entities you iterate over the Positions array alongside with the Rotations array and process whatever you need. And since you don't read/touch Scales and Colors, everything will be much faster (as described in the mentioned presentations).

    Does this make a little bit more sense?

    Disclaimer: this is an attempt to simplify reality a bit, which is a little more complicated than this
     
    Last edited: Jun 16, 2020
    Yoreki likes this.
  3. UltiPangol

    UltiPangol

    Joined:
    Nov 12, 2019
    Posts:
    4
    I understand that, my question is, "how do my entity 13 know/store the link to entity 42 who is a branch?

    can I have a array of entity inside a component? do I have to make a Gameobject how store the entity composing the tree?

    I can't make a component branch, it's to heavy, My branch have to be an entity who can grow, have entity leaves, etc...

    But how do I do that in data oriented system?
     
  4. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    10,004
    You can have references to other entities. But if you want performance you need to minimize the interleaving entities as much as possible. (prefer the flat hierarchies against the deep hierarchies)

    You can find more info in the Beta/DOTS forum.
     
  5. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    Albeit I can't help you with your specific problem and I would suggest to post this in the ECS/DOTS related forum Lurking-Ninja linked, I can suggest to have a look at the free Data Oriented Design book by Richard Fabian. It explains the mindset and some strategies (fe normalization) to do the transition of OOP thinking to DOD thinking. You can also buy a printed version of the book wich contains more content, code etc. .