Search Unity

Multiple components of the same type

Discussion in 'Entity Component System' started by Allan-Smith, Jul 27, 2018.

  1. Allan-Smith

    Allan-Smith

    Joined:
    Feb 7, 2012
    Posts:
    57
    Hey,

    So, I was looking for answers regarding this and got, among others, to this post:
    https://forum.unity.com/threads/support-for-multiple-components-on-same-entity.523589/

    I couldnt find any documentation into FixedArrays or FixedArrayArrays, and its still not clear for me how that would work. Someone in some other post suggested spawning other entities and putting the component in those entities, and in this component store a "reference" to the Entity that they are trying to affect, so say, you have a "Poisoned" status in your game, and you want to be able to have 2 completely different stacks of poison in your entity... one that deals 10 damage over 5 seconds and other that deals 20 over 20 seconds. In my case its a very different yet similar scenario, but I've no clue as what the best way would be to move forward.

    I really love the ECS so far, and I am basically using my own form of "hybrid" ecs... I need physics for my game so I have game objects, but all the object's data is on a pure ecs layer (not using GameObjectEntity), and being able to use the jobcomponentsystem is such a huge increase in performance that I really want to use the ECS in its full potential, but this kind of problem is kind of worrying me.

    Any more clarification how FixedArrays could possibly help with this issue?
     
  2. dartriminis

    dartriminis

    Joined:
    Feb 3, 2017
    Posts:
    157
    The problem with the fixed array is that you need to define its length up front when you create the entity, so it gets tricky if you are implementing something where you need to add/remove items from it, as I assume your are adding/removing the poison effect from an entity.

    For your poison scenario, I would recommend sticking with regular entities and components. Since your entities could be effected by multiple poison components, I would create separate entities that have the Poison component, a Cooldown component, and a Target component. A PoisonSystem could then update the cooldown on all the poison entities, and if the cooldown is at zero, it can apply damage to the target entity and reset the cooldown.

    There are probably others ways to skin this cat, but that's what comes to mind for me, at least.
     
  3. Allan-Smith

    Allan-Smith

    Joined:
    Feb 7, 2012
    Posts:
    57
    Yeah thats the suggestion I found for this issue and indeed it feels like the best solution, even though it sounds a bit hacky. It also has the problem of having to keep looking for other entities (after all I want to apply the effect somewhere else), but even with that sounds like its a decent solution... specially if the rate at which the affect is applied is cooldown based (and not per frame based)
     
  4. Allan-Smith

    Allan-Smith

    Joined:
    Feb 7, 2012
    Posts:
    57
    Hm... went on to implement something like this but indeed this is not the best scenario ever right... I mean, the problem with referencing another entity basically makes it impossible to use JobComponentSystems...

    Let me give the real example in my case here. Entities have a TemperatureComponent which has a value. They can be affected by X TemperatureModifierComponents, which grab the current temperature value and slowly modify it towards the desired temperature. So I went ahead and created a new entity with just tem TemperatureModifierComponent, and added a Entity "reference" in this component, as the TargetEntity.

    The problem is, since they will need to directly affect another entity, I need to first grab the TemperatureComponent of that entity to know which state it is in right now, then make the formula to modify the temperature, and then save that component back to that entity. This basically makes it impossible for the "TemperatureModifierSystem" to be a JobComponentSystem, cause even though I can have a EntityCommandBuffer to SET the TemperatureComponent, I have no way of GETTING the TemperatureComponent state to make the calculation in the first place (cause it depends on the current temperature) (or is there?), and well... the performance gain of running JobComponentSystems is so big for the multicore usage that I will try to go with the FixedArray, even though it does add some challenges and limitations
     
  5. julian-moschuering

    julian-moschuering

    Joined:
    Apr 15, 2014
    Posts:
    529
    ComponentDataFromEntity should be what you need. It injects all components of the requested type accessible by entity.
     
    Last edited: Jul 28, 2018
  6. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    Would you be better off with a generic ECS time delta system all it does is take a value and update it at a given rate for a set duration?

    Then it could be used for cooldowns, poison, firing rates, healing effects any affect that changes a value over time?