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. Dismiss Notice

Getting Component Data from another Entity (Jobified Lambda in SystemBase)

Discussion in 'Entity Component System' started by zardini123, Jun 17, 2020.

  1. zardini123

    zardini123

    Joined:
    Jan 5, 2013
    Posts:
    68
    I have entities that have a component that points to another entity by storing an Entity variable. I need to get component data from that associated entity to do work. An example of this is a camera system that follows the velocity of the object it's following (target stored as Entity, its PhysicsVelocity component data needed).

    In ComponentSystem (main thread), one would use the Entity Manager, and just use EntityManager.GetComponentData to get the associated data.
    In JobComponentSystem (multi threaded), one would use the GetComponentDataFromEntity container, and just plug the associated entity as an index to the collection (i.e. theData[theOtherEntity]).

    How would one go about this in a Entities.ForEach(...).Schedule() or ScheduleParallel() (jobified) lambda in SystemBase?
     
  2. shmafoo

    shmafoo

    Joined:
    Nov 22, 2016
    Posts:
    24
    You should be able top just also use GetComponentDataFromEntity inside your Entities.Foreach. Any issues trying to do so?

    /edit: Check Enzis post below for correct answer
     
    Last edited: Jun 18, 2020
    vectorized-runner likes this.
  3. Enzi

    Enzi

    Joined:
    Jan 28, 2013
    Posts:
    908
    Actually, GetComponentDataFromEntity is used outside the ForEach and it gets captured in a display class in SystemBase.
    You can also use GetComponent inside ForEach which gets translated into GetComponentDataFromEntity outside the ForEach when you look at the DOTS compiler.
     
    shmafoo likes this.
  4. zardini123

    zardini123

    Joined:
    Jan 5, 2013
    Posts:
    68
    @Enzi Do you know of a GetComponent-like shortcut for SystemBase ForEach but for DynamicBuffers?
     
  5. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,695
    are you looking for this GetBufferFromEntity<>
     
  6. zardini123

    zardini123

    Joined:
    Jan 5, 2013
    Posts:
    68
    Ahh yes that's exactly right. Turns out Entities 0.11 doesn't have it, so I gotta update to 0.13
     
  7. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,653
    Small note - it will be GetBuffer<T>(Entity) if you looking for syntax sugar of SystemBase codegen.
    GetBufferFromEntity lookup is similar to GetComponentFromEntity which should be captured outside of ForEach lambda. GetBuffer hides from you all boilerplate of GetBufferFromEntity under codegen. And yes it was added in 0.12.