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

Running code before systems start

Discussion in 'Entity Component System' started by Init33, Jun 28, 2019.

  1. Init33

    Init33

    Joined:
    Aug 30, 2017
    Posts:
    67
    Hi All,

    I have a game that I am working on and haven't touched for a couple of beta versions which I recently updated to 2019.2.0b3.

    Generally speaking, in my code I have a bunch of stuff I want to do first, then get my ComponentSystems going after. How I have been doing this for a while (which i haven't had problems with up until now) was using this:

    Code (CSharp):
    1.     [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
    2.     public static void Initialise()
    3.     {
    4.         //..do stuff
    5.     }
    6.  
    7.     [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
    8.     public static void RunGame()
    9.     {
    10.         entityManager = World.Active.EntityManager;
    11.        
    12.         MySystem.SystemStaticMethodThatSetsThingsUp();
    13.         //.. do more stuff
    14.     }
    Just tried to run my game and I'm getting the "NullReferenceException: Object reference not set to an instance of an object" error on the line
    entityManager = World.Active.EntityManager;
    .
    entityManager is declared as a static variable at the top of my this script.

    Firstly, why is this error occurring here... have they changed how entitymanager works?

    Secondly, am I going about initialising things in systems before they start by using the above methods?
     
  2. Razmot

    Razmot

    Joined:
    Apr 27, 2013
    Posts:
    346
    Here 's my take on this :

    I do all my setup in a monobehaviour Start method like that ( very useful for my hybrid system, to pass camera position, player gameobject or some other monobehaviours, or some settings that I can tune in the good old inspector)

    Code (CSharp):
    1.        world.GetOrCreateSystem<MergeNodesSystem>().Setup();
    2.         world.GetOrCreateSystem<NodeDestroyer>().Setup(MonoChunkManager);
    3.         world.GetOrCreateSystem<CheckNodesSystem>().Setup(Target);
    Example of setup method in a system :

    Code (CSharp):
    1. public NodeDestroyer Setup(MonoChunkManager achunkManager)
    2.         {
    3.             _chunkManager = achunkManager;
    4.             LookupSys = World.Active.GetOrCreateSystem<LookupSystem>();
    5.             _setupDone = true;
    6.             return this;
    7.         }
    and of course in update :

    Code (CSharp):
    1.      protected override void OnUpdate()
    2.         {
    3.             if (!_setupDone) return;
    (return inputDeps for the jobCompoSys)

    And I keep just the native allocations in OnCreate / OnDestroy
    Code (CSharp):
    1.   protected override void OnCreate()
    2.         {
    3.             _destroyed = new NativeList<Entity>(Allocator.Persistent);
    4.         }
    5.  
    6.         protected override void OnDestroy()
    7.         {
    8.             _destroyed.Dispose();
    9.         }
     
    Flurgle likes this.