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

Adding components in ITriggerEventsJob

Discussion in 'Physics for ECS' started by Craftdragon, Jan 15, 2020.

  1. Craftdragon

    Craftdragon

    Joined:
    Jan 2, 2019
    Posts:
    1
    Hi,
    For my game I need to add a component on collision/when a trigger is hit.
    For this I've been using the ITriggerEventsJob, which works well if there is only 1 collision event.
    But sometimes multiple events report the same entity and this seems to cause an issue when the EntityCommandBuffer is played back.
    I suspect this is because it's now trying to add the same component twice (ecs errors aren't the most descriptive).
    Does anyone have an idea on how to solve this issue?

    (Simple example of what I mean)
    Code (CSharp):
    1. struct TriggerJob : ITriggerEventsJob
    2. {
    3.     public EntityCommandBuffer commandBuffer;
    4.  
    5.     public void Execute(TriggerEvent triggerEvent)
    6.     {
    7.         var entA = triggerEvent.Entities.EntityA;
    8.         var entB = triggerEvent.Entities.EntityB;
    9.  
    10.         commandBuffer.AddComponent(entA, new IsTriggeredEvent() { data = 5 });
    11.         commandBuffer.AddComponent(entB, new IsTriggeredEvent() { data = 5 });
    12.     }
    13. }
     
  2. elora_k

    elora_k

    Unity Technologies

    Joined:
    Sep 20, 2016
    Posts:
    39
    Hi,
    Can you share the error you're seeing? Adding a component that already exists on an entity through the command buffer should result in just setting the value.

    Also, can you share which version of Entities you are using?

    Thanks!
     
    steveeHavok likes this.
  3. lclemens

    lclemens

    Joined:
    Feb 15, 2020
    Posts:
    716
    Maybe change EntityCommandBuffer to EntityCommandBuffer.ParallelWriter ?
     
  4. Tudor

    Tudor

    Joined:
    Sep 27, 2012
    Posts:
    150
    `EntityCommandBuffer.ParallelWriter` is meant for multithreaded jobs and requires a `Execute([ChunkIndexInQuery] int ciq`.

    Which you can't do in a `ITriggerEventsJob`.

    Unity in its own ECS samples repo changes components in ITriggerEventsJob's like this:

    Code (CSharp):
    1. public ComponentLookup<PhysicsGravityFactor> PhysicsGravityFactorGroup;
    2. ...
    3. var component = PhysicsGravityFactorGroup[dynamicEntity];
    4. component.Value = triggerGravityComponent.GravityFactor;
    I was also interested in how to destroy or create an entity on collision with a command buffer only once. I guess it's not possible considering you can have multiple collision events, and they already happened by the time you read them.

    Unless you use one of the ComponentLookup component value sets (which are instant) to mark this pair as "solved" so subsequent trigger calls you can return early from.
     
    Last edited: Apr 1, 2023