Search Unity

ECS components not injected

Discussion in 'Entity Component System' started by Micz84, May 3, 2018.

  1. Micz84

    Micz84

    Joined:
    Jul 21, 2012
    Posts:
    451
    I have system:
    Code (CSharp):
    1. public class PlayerInputSystem : ComponentSystem
    2. {
    3.     public struct Data
    4.     {
    5.         public int Length;
    6.         public ComponentArray<NavMeshAgent> Navigation;
    7.         public ComponentArray<Player> PlayerTag;
    8.     }
    9.  
    10.     [Inject] private Data m_Data;
    11.  
    12.     protected override void OnUpdate()
    13.     {
    14.         Debug.Log(m_Data.Length);
    15.     }
    16. }
    And Component (Tag):
    Code (CSharp):
    1. namespace GameCode.Components.Player
    2. {
    3.     public class Player : MonoBehaviour
    4.     {
    5.     }
    6. }
    I have a GameObject in the scene with NavMeshAgent and Player component. Length in m_Data is always 0.
    Why does it not get injected?

    Edit:

    OK I forgot to add GameObjectEntity ;) But I do not know how to add components at runtime, so other systems will react.
     
    Last edited: May 3, 2018
  2. isbdnt

    isbdnt

    Joined:
    May 16, 2017
    Posts:
    35
    Check out the TwoStickShooter demo.
     
  3. Micz84

    Micz84

    Joined:
    Jul 21, 2012
    Posts:
    451
    I have looked at it (hybrid) and can't find example of adding and removing components from entities at run time.
     
  4. pahe

    pahe

    Joined:
    May 10, 2011
    Posts:
    543
    Here's a small piece of my code.

    Code (CSharp):
    1. var cubeEntity = cube.AddComponent<GameObjectEntity>().Entity;
    2. entityManager.AddComponentData(cubeEntity, new CubeComponent());
    Essentially you need the EntityManager, your entity and the IComponentData you want to add. Then tell the EntityManager to add the IComponentData to your Entity.

    If I remember correctly, there was a warning not to add/remove components during OnUpdate as it may stall the execution of your job due to synchronization. But take this all with care, I'm too have to wrap my head around this stuff :)
     
  5. Micz84

    Micz84

    Joined:
    Jul 21, 2012
    Posts:
    451
    I have tried this but I am using a hybrid approach and my Components are MonoBehaviours and I get a compiler error when I pass my Component to AddComponentData. It says that second argument must be non-nullable. Maybe I have to wrap my behaviors.
    EDIT:

    Ok I had to wrap my component and It works now:

    Code (CSharp):
    1. public struct Destination : IComponentData
    2.     {
    3.         public float3 position;
    4.     }
    5.  
    6.     public class DestinationWrapper : ComponentDataWrapper<Destination>
    7.     {
    8.     }
     
    Last edited: May 4, 2018
  6. siggigg

    siggigg

    Joined:
    Apr 11, 2018
    Posts:
    247
    Shouldn't those be ComponentDataArray ?
     
  7. pahe

    pahe

    Joined:
    May 10, 2011
    Posts:
    543
    Yeah... and no :) Player is a MonoBehaviour, not an IComponentData. ComponentDataArray can only take structs derived from IComponentData.
    BUT Unity want us to use IComponentData. As long as not all Unity components (Rigidbodies for example) are not fully supported in ECS, the ComponentArray will fill that gap.
     
    siggigg likes this.
  8. Micz84

    Micz84

    Joined:
    Jul 21, 2012
    Posts:
    451
    They are MonoBehaviours so it has to be ComponentArray.
     
  9. pahe

    pahe

    Joined:
    May 10, 2011
    Posts:
    543
    Ok, here I have to correct myself. The warning was for creating new Entities, not components. Haven't seen warnings about adding/removing components in the docs. I guess it may also be recommended to do so, so you can add components as filters (like a DeadComponent).

    Sorry for the confusion.
     
  10. allenmathewmd

    allenmathewmd

    Joined:
    Apr 21, 2018
    Posts:
    2
    @Micz84 Did you say you managed to add/remove Monobehaviour components live during runtime?

    I've been trying to add Monobehaviour components during runtime using gameObject.AddComponent<MyComponent> but it seems like GameObjectEntity only adds Monobehaviour components that are currently on the game object when adding GameObjectEntity.
     
  11. Micz84

    Micz84

    Joined:
    Jul 21, 2012
    Posts:
    451
    @allenmathewmd

    No, I did not manage to do that, but right now I do not need it.