Search Unity

accessing parent and child components in a job

Discussion in 'Entity Component System' started by andynayrb, Jan 22, 2020.

  1. andynayrb

    andynayrb

    Joined:
    Feb 28, 2019
    Posts:
    25
    So I have a system that I would like to write data to a component of a parent and a different component in a child entity (it can't be SCD). Seems like it should be doable, I am just not sure how to access the child component in the job.

    I am doing it currently in two separate jobs, but it seems like it could be combined and done more efficiently. I could be wrong, if so feel free to tell me that also.

    Thanks in advance for any pointers
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,266
    You can use ComponentDataFromEntity to access the components. Parent is an ICD that contains an Entity field. Child is an IBED also containing an Entity field. So that gives you full hierarchy access.
     
  3. andynayrb

    andynayrb

    Joined:
    Feb 28, 2019
    Posts:
    25
    Two questions.

    1. I will ask the really dumb one first. What do ICD and IBED stand for?

    2. Are there any of the Unity Samples that show how to use ComponentDataFromEnitity? And also accessing the Entity hierarchy?

    The way i am understanding this is that ComponentDataFromEntity gets an array of entities that have the desired component. If I am in a IJobForEach to get the parent, we don't actually know what entity we are looking at, is this correct. So in my mind I couldn't sort through the array to find the entity's child. So I am thinking that I shouldn't use an iJobForEach for this but rather a different type of job. One that maybe just creates two arrays one for the parent and one for the child. With all that being said it seems like using a IJobForEach would in theory be better if its possible.

    Pretty much all the examples I have seen with these use inject, which I don't think is used (or is discouraged) anymore if my understanding is correct
     
  4. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,266
    1) IComponentData and IBufferElementData respectively.
    2) https://github.com/Unity-Technologi...fy/Scripts/ModifyContactJacobiansBehaviour.cs

    ComponentDataFromEntity just provides a handle into the full ECS data structure with the restriction that it can only grab the one component.

    You can get a component from the parent entity using ComponentDataFromEntity just fine. It uses the Entity as a key to look up the chunk and then the index in chunk. It's an O(1) operation.