Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

DOTS and Zenject

Discussion in 'Entity Component System' started by demozbox, Nov 16, 2020.

  1. demozbox

    demozbox

    Joined:
    Nov 26, 2014
    Posts:
    83
    Hello. Trying to use DI from Zenject. I created scene context and binging script.
    On the object with class that I need attached zengect binding component.
    When I test the injection in test class, everything fine
    Code (CSharp):
    1. using UnityEngine;
    2. using Zenject;
    3.  
    4. public class Test : MonoBehaviour
    5. {
    6.     [Inject]
    7.     CameraBeh _cameraBeh;
    8.     void Start()
    9.     {
    10.         Debug.Log(_cameraBeh);
    11.     }
    12. }
    Debug log "CameraBeh" so it was injected.
    But when I try the same way with ComponentSystem the Debug log is "Null" and null ref exception
    Code (CSharp):
    1. using Unity.Entities;
    2. using Unity.Transforms;
    3. using Zenject;
    4. using UnityEngine;
    5.  
    6. public class InCameraViewPortSystem : ComponentSystem
    7. {
    8.     CameraBeh _cameraBeh;
    9.     [Inject]
    10.     private void Construct(CameraBeh camera)
    11.     {
    12.         _cameraBeh = camera;
    13.     }
    14.  
    15.     protected override void OnStartRunning()
    16.     {
    17.         Debug.LogWarning(_cameraBeh);
    18.     }
    19.     protected override void OnUpdate()
    20.     {
    21.         Entities.WithAll<ViewPortAttend>().ForEach((ref Translation tran) =>
    22.             {
    23.                 _cameraBeh.CheckForViewPort(tran.Value);
    24.             });
    25.     }
    26. }
    27.  
    Is there special way to inject components in ComponentSystem/SystemBase?
     
  2. Micz84

    Micz84

    Joined:
    Jul 21, 2012
    Posts:
    448
    Have you tried to add Log in the constructor? Systems are created automatically and added to the world. You probably need to disable auto-creation of your systems. You get null because the default creation system creates InCameraViewPortSystem and it knows nothing about Zenject and CameraBeh.
     
  3. demozbox

    demozbox

    Joined:
    Nov 26, 2014
    Posts:
    83
    I've put Zenject Constructor with debug into SystemBase and it didn't write the message so I conclude it is not propper way to inject into SystemBase. Have no idea how to do it yet.
     
  4. WAYNGames

    WAYNGames

    Joined:
    Mar 16, 2019
    Posts:
    988
    I don't see the use of zenject in a dots project.
    The OnCreate method of system base should be enougth to :
    1. Get other dependant system (typically ecb)
    2. Get a singleton entity to have the data you want (tippically configuration for the system)
    3. Create a new instance of a struct that defines the behavioral methods you need in your job. (Utility method shared by multiple jobs or to encapsulate in explicit naming for self documentation purpose)
     
  5. demozbox

    demozbox

    Joined:
    Nov 26, 2014
    Posts:
    83
    Googled what is ECB and didnt find anythig. Could you please clarify?
    Singletones works well with ComponentSystem with my class but now I try to move to SystemBase and can not do it with singletones.
    Im so new in DOTS and can not understend an explanation without example yet :)
     
  6. Sarkahn

    Sarkahn

    Joined:
    Jan 9, 2013
    Posts:
    440
    The second and third pinned messages in this forum point to up to date samples and documentation that will give you a good head start.
     
  7. Micz84

    Micz84

    Joined:
    Jul 21, 2012
    Posts:
    448
    Generally, ECB means EntityCommandBuffer.He meant getting other systems typically EntityCommandBufferSystem like BeginSimulationEntityCommandBufferSystem. They are used to get ECB from them. The goal of those systems is to play ECBs at specific points in the frame for example a the beginning of simulation phase (group) or at the end of the simulation phase. You want to avoid to many structural changes (creating/destroying entities, adding/removing components) during frame because it causes hard sync point and all jobs have to be completed before any structural change can be done.
     
  8. WAYNGames

    WAYNGames

    Joined:
    Mar 16, 2019
    Posts:
    988
    Yeah, ECB is for EntityCommandBuffer, it's used to delay all structural change at a point in the player loop to do the changes in the most optimal way possible.

    For Singleton, I was refering to singleton entities, somes ressources on the subject (please be aware of the version of the entity package you use, things cahnges rather quickly in terms of syntax, not so much in terms of concept though) :
    https://gametorrahod.com/ecs-singleton-data/
    https://forum.unity.com/threads/singleton-components.535331/#post-4028170
    https://docs.unity3d.com/Packages/c...emBaseManagedComponentExtensions.html#methods