Search Unity

Feedback Behaviour of AddComponent for IEnableable Components

Discussion in 'Entity Component System' started by kruskal21, Dec 18, 2022.

  1. kruskal21

    kruskal21

    Joined:
    May 17, 2022
    Posts:
    68
    I'm writing this to highlight one aspect of the semantics for IEnableable components which is a bit odd currently. Specifically that EntityManager.AddComponentData<T>(Entity, T) silently does nothing if the entity already has a disabled component. While this is clearly documented, the behaviour is still rather odd.

    One of my current usecase for IEnableable components is tweening. There are several entities in the scene which will have their scale tweened when the player hovers over them. To facilitate this, an IEnableable ScaleTween component is added to entities when hovered, and a ScaleTweenSystem queries for entities with this component, and updates ScaleTween progress and the entities' scale accordingly. Once tweening is finished for an entity, the system disables the component.

    The querying and disabling aspects of this are perfect thanks to IEnableable semantics. However the adding of the ScaleTween component is currently not. The system responsible for detecting hovering and setting up the tweening does not care whether the entity has a ScaleTween component, it just wants to set new ScaleTween values, and add the component if it doesn't exist yet. Ideally this can just be done with one line of AddComponent, however since the component is enableable, several checks are currently needed:
    Code (CSharp):
    1.     if (!HasComponent<ScaleTween>(entity))
    2.     {
    3.         // If component doesn't exist, then add it normally.
    4.         em.AddComponentData(entity, scaleTween);
    5.     }
    6.     else
    7.     {
    8.         // If component exists already, adding it does nothing.
    9.         // We must set the component then enable it.
    10.         SetComponent(entity, scaleTween);
    11.  
    12.         SetComponentEnabled<ScaleTween>(entity, true);
    13.     }
    Not terrible, but I would love to be able to just write this:
    Code (CSharp):
    1.     // Add the component if it doesn't exist, else update the values
    2.     em.AddComponentData(entity, scaleTween);
    Which is the same behaviour as regular non-enableable components.

    I'd love to hear the team's thoughts on this. The entities package has been a joy to use so far, and this is one tiny area where I think the ergonomics could be improved just a bit further. Thanks for reading!
     
    Last edited: Dec 18, 2022
    JesOb and Greexonn like this.