Suggestion: AddOrSetComponent in EntityCommandBuffer

Hey

In my small code base I already have a lot of places like this

if (hasHeat)
{
    Commands.SetComponent(_threadIndex, entity, heat);
}
else
{
    Commands.AddComponent(_threadIndex, entity, heat);
}

It would probably work fine if SetComponent would implicitly Add it, if it’s not exists.
But to stay clear about what API would actually do - I propose to add something like AddOrSetComponent

This has been discussed a few times without any official word on it I believe.

I used to be all for this and found multiple cases where I needed it. However over time with move towards chunk iteration and a better understanding of how ECS works, I’ve changed how I design systems and I’ve come to believe that AddOrSet promotes bad design.

Adding and removing components is something I believe should be avoided where possible. Constantly (every frame) moving entities between chunks is costly and really limits the potential performance for your systems.

That’s not to say that there aren’t use cases for this and that adding it wouldn’t be helpful for these, but if you’re constantly finding you need it you might want to consider refactoring your design.

2 Likes

Yep. After thinking about it and rewriting some parts, amount of usages has been reduced. (total amount of dependencies has been dropped)
At the cost of introducing new barrier…