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!
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:
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.
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”.
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.
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).
Yep it’s what I’m currently doing with a NativeHashMap, I’ll stick to this solution for now! Thanks guys!
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?
No. Cos until ECB playback, structural changes not applies.
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