Search Unity

How to set component data of other entity inside job?

Discussion in 'Entity Component System' started by lijianfeng, Aug 1, 2019.

  1. lijianfeng

    lijianfeng

    Joined:
    Sep 8, 2015
    Posts:
    54
    I use PhysicsWorld.CastRay inside job get other rigidBody entity,and I need to set component data of this rigidBody entity,how do I accomplish this?
     
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Use EntityCommandBuffer.Concurrent, pass it to the job upon job creation.
     
  3. lijianfeng

    lijianfeng

    Joined:
    Sep 8, 2015
    Posts:
    54
    Use EntityCommandBuffer.Concurrent will take effect later,not immediately. forexample, if I will set other entity healthpoint component in this job useEntityCommandBuffer ,and following another system handle healthpoint update,it will not see the healthpoint change immediately because of commandbuffer replay happens later and it can only see the change in the next system update,this will cause one frame delay!:eek:
     
  4. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    It will. But in reality, you do not need everything to happen instantly.
     
  5. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    Use ComponentDataFromEntity

    It'll be much faster than EntityCommandBuffer just have to be single threaded job though (or split your your jobs and group entities into hashmap if you're doing more than 1 raycast)
     
  6. lijianfeng

    lijianfeng

    Joined:
    Sep 8, 2015
    Posts:
    54
    any sample code to use hashmap to do it?
     
  7. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    say you have a job where each thread does a raycast and returns an entity. instead of working on the results straight away you can store it in a native(multi)hashmap<entity, X>

    then you can use the IJobNativeMultiHashMapVisitKeyValue to process the results which would guarantee that if different queries hit the same entity they would be processed on the same thread so you can safely modify the entity within this job.

    if I remember i'll write a very short example up tomorrow
     
    GameDeveloper1111 and ADHDCoder like this.