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

EntityCommandBuffer AddComponent question

Discussion in 'Entity Component System' started by slages, May 2, 2019.

  1. slages

    slages

    Joined:
    May 16, 2017
    Posts:
    9
    Hi all,

    Is there a way to prevent multiple systems to add the same component to an entity using the EntityCommandBuffer? A sort of TryAdd instead of Add to check if someone is already trying to add this component type to the entity? And without having to complete all jobs to flush the command buffer ideally.

    Thanks!
     
  2. Singtaa

    Singtaa

    Joined:
    Dec 14, 2010
    Posts:
    485
    I haven't tested it out yet myself. But depending on your use case, the new change in the latest version may be of use:

     
  3. slages

    slages

    Joined:
    May 16, 2017
    Posts:
    9
    Thanks, I just tested with the updated version but still having the same error:

    ArgumentException: The component of type:CellReservedData has already been added to the entity.
    EntityCommandBuffer was recorded in WalkToSystem and played back in Unity.Entities.EndSimulationEntityCommandBufferSystem.​
     
  4. Singtaa

    Singtaa

    Joined:
    Dec 14, 2010
    Posts:
    485
    After taking a look at the source code, I think IgnoreDuplicateAdd only applies to ZeroSize components. I'm guessing that's what it means by "in the cases where no data would be overwritten".
     
  5. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    Ya because how could they even tell if a component is equal to another if it has any data.

    TryAdd would not be deterministic in parallel jobs, you could have two threads adding the same component with different values, and you have no idea which one would win. Tag components are really an edge case here, as a general rule you want to be using a single writer design.
     
  6. Singtaa

    Singtaa

    Joined:
    Dec 14, 2010
    Posts:
    485
    Though on the flip side, TryAdd/TryRemove can bring in a lot of convenience for cases where job ordering is fixed or for those who don't need determinism. But yeah, I think determinism should be a big push by Unity, so the best solution for people who needs TryAdd is probably to roll your own Sync point/Buffer playback (it's simpler than it sounds).
     
  7. slages

    slages

    Joined:
    May 16, 2017
    Posts:
    9
    Yep it's what I'm currently doing with a NativeHashMap, I'll stick to this solution for now! Thanks guys!
     
  8. Ahlundra

    Ahlundra

    Joined:
    Apr 13, 2017
    Posts:
    47
    wasnt there a way to check if the entity already has the component?
    you could always filter another group of entities with that specific component and check if the entity you're messing with already exist inside that group, but how slow would that be?
     
  9. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,655
    No. Cos until ECB playback, structural changes not applies.
     
  10. Ahlundra

    Ahlundra

    Joined:
    Apr 13, 2017
    Posts:
    47
    I tought the new versions made a sync point after the job was done, but yeah... if the add need to wait until frame end them hashmap would be the only way to go

    unless you make a system just for adding the component and them turn a bool to true/false inside your other systems
    but that doesnt seem like a better idea than what you're doing already =x