Search Unity

Need some help with understanding ECS

Discussion in 'Scripting' started by EternalMe, Feb 24, 2020.

  1. EternalMe

    EternalMe

    Joined:
    Sep 12, 2014
    Posts:
    183
    Hi. I'm having problem understanding ECS when it comes to nested objects. For example if I have a Character (Entity), that holds a Gun (that also is an Entity!) and the gun has component Color (green, red, yellow, etc.). And then I want the a System to work on all Characters that have a Gun in green color. How would that work? Does the Gun act like like a Entity and Component is same time?
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    In ecs, everything is entity. Character is entity. The gun the character holding is entity. As for the fact holding, it might be represented differently.
    1. Entity and add a Holding component to it with two fields: who holds and what.
    2. A HoldedBy compoent for the gun storing the character entity id.
    3. A Holder component for the character storing gun entity id.
    4. Both 3 & 2.
    5. Parent the gun to the character (basically #2 but via transform hierarchy)
     
  3. EternalMe

    EternalMe

    Joined:
    Sep 12, 2014
    Posts:
    183
    How would this component look like? And most importantly, the assignment of this entity- when and how?
     
  4. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Code (CSharp):
    1. public struct HoldTheGun : IComponentData {
    2.     public Entity TheGun;
    3. }
    It depends on your game logic. You can add this component to the character's archetype and set it to empty when character holds no gun or you can add the component and set TheGun value to the gun entity when character draws the gun and remove the component when character hides his gun. It's up to you to decide.
     
  5. EternalMe

    EternalMe

    Joined:
    Sep 12, 2014
    Posts:
    183