Search Unity

Accessing another ComponentSystem's fields and methods.

Discussion in 'Entity Component System' started by RecursiveEclipse, Jan 15, 2019.

  1. RecursiveEclipse

    RecursiveEclipse

    Joined:
    Sep 6, 2018
    Posts:
    298
    I'm working with a hierarchy of entities and would like to access TransformSystem's
    ParentToChildTree HashMap. I was thinking that instead of writing my own methods to manage it and copying TransformSystem, use reflection instead, but reflection is foreign to me so I'm assuming I need the instance of TransformSystem? If so, where would I get it? Or is this a terrible idea and I should just re-implement it?
     
  2. orionburcham

    orionburcham

    Joined:
    Jan 31, 2010
    Posts:
    488
    It may be simpler than you're thinking:

    Code (CSharp):
    1. public class MySystem : ComponentSystem
    2. {
    3.     TransformSystem transformSystem;
    4.  
    5.     protected override void OnCreateManager()
    6.     {
    7.         transformSystem = World.Active.GetOrCreateManager<TransformSystem>(); // There's also a GetExistingManager() method if you're sure it's already be created.
    8.     }
    9.  
    10.     protected override void OnUpdate()
    11.     {
    12.         // access TransformSystem members
    13.     }
    14. }
    If that's missing your point, please say so. :)
     
  3. RecursiveEclipse

    RecursiveEclipse

    Joined:
    Sep 6, 2018
    Posts:
    298
    Ouch, I knew that was a thing and for some reason completely forgot, thank you. ParentToChildTree is private though so I'll still need reflection in some sense and I'll see how that goes. GetOrCreateManager will save me the trouble/code at least.
     
  4. RecursiveEclipse

    RecursiveEclipse

    Joined:
    Sep 6, 2018
    Posts:
    298
    Okay so doing just what you showed isn't working for me(does it work for you?). I get these 2 errors:

    GetExistingManager always returns null, I recall reading that systems are initialized in alphabetical order(Or something to that effect?) so I tried renaming to 'ZSystem' but no effect.

    What does work, is having an isInitialized check in OnUpdate() and using GetExistingManager, gross solution though. Using Inject errors out as well.
     
    Last edited: Jan 15, 2019
  5. Floofloof

    Floofloof

    Joined:
    Nov 21, 2016
    Posts:
    41
    For me i found the best way to do this is by creating my system and insert anything else i need into the constructor.

    Code (CSharp):
    1. public class GridCreateSystem : JobComponentSystem
    2. {
    3.     private int matrixSize = 32;
    4.     private int diffculty = 4;
    5.  
    6.     public GridCreateSystem(GameConfig gameConfig)
    7.     {
    8.         matrixSize = gameConfig.matrixSize;
    9.         diffculty =gameConfig.diffculty;
    10.     }
    11.  
    12.     protected override void OnCreateManager()
    13.     {
    14.         base.OnCreateManager();
    15.     }
    16. }
    and then something like

    Code (CSharp):
    1. gridCreateSystem = ECSWorld.CreateManager<GridCreateSystem>(gameConfig);
    Im manually updating my systems so i kinda need to manually create my systems as well. When you create your custom system just make sure to pass in ref to the TransformSystem you need.

    Also it might not be the best idea to use World.Active. Its possible that the world has changed. It might be better to just create your own world. Populate it with the systems required and store the worlds ref.
     
  6. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,759
    Use GetOrCreateManager not GetExisting then order does not matter.

    And do it in OnCreateManager() not in a constructor.
     
  7. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,683
    Also use World property from system and not World.Active if you do it in system.
     
    tertle likes this.