Search Unity

Question: Creating Non-Generic Instances of Generic Systems

Discussion in 'Entity Component System' started by PublicEnumE, Sep 11, 2019.

  1. PublicEnumE

    PublicEnumE

    Joined:
    Feb 3, 2019
    Posts:
    729
    I have a generic System type, which I'd like to instantiate non-generic instances of, based on a few different ComponentTypes in my project.

    What's the best way to create these System instances?

    - - -

    In the past, I believed this was one potential use of ICustomBootstrap. But I've since learned that ICustomBootstrap is meant for creating your own custom Worlds.

    - - -

    I'd like to add these System instances to the default World. What would be the best tool for this job?

    Thanks for any advice.
     
    Last edited: Sep 11, 2019
  2. daschatten

    daschatten

    Joined:
    Jul 16, 2015
    Posts:
    208
  3. PublicEnumE

    PublicEnumE

    Joined:
    Feb 3, 2019
    Posts:
    729
    Thank you for the reply. But I might be missing something (or I might have explained something poorly).

    In the example, they’re deriving from a generic base type to create a non-generic type.

    That’s different from what I want to do. I’d like to instantiate a generic system type with a type argument at runtime, which would make it non-generic.

    I’d also like to know the best way to do this once, during world initialization, so that I can loop over dozens of types in my project and instantiate a system instance for each.
     
    Last edited: Sep 11, 2019
  4. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,759
    What's wrong with ICustomBootstrap?

    If anything it was designed for creating custom systems not worlds as it passes a system list you are meant to add/remove from (should totally be used for custom worlds as well though.)
     
    PublicEnumE likes this.
  5. PublicEnumE

    PublicEnumE

    Joined:
    Feb 3, 2019
    Posts:
    729
    Thanks, I was probably just confused.

    If I want to make sure that my ICustomBootstrap only adds these Systems to the default world (and not the conversion world, etc.), what's the best approach?

    I've seen you comment on a way you figured out to detect it, but I recall you saying it felt hacky.
     
  6. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,759
    Yep it's a bit annoying to need to check. I don't think ICustomBootstrap was really considered when the conversion worlds etc were added.

    Systems that appear in conversion etc have an attribute specifying that. Currently no system appears in default world and these custom worlds so you can just check if the first of the systems passed in have this attribute specifying another world, and if so, just early out.

    Code (CSharp):
    1.         public List<Type> Initialize(List<Type> systems)
    2.         {
    3.             if (systems.Count == 0)
    4.             {
    5.                 return systems;
    6.             }
    7.  
    8.             // We only want to install in default world groups, not GameObjectConversion or EntitySceneOptimizations
    9.             var attrib = systems.First().GetCustomAttribute<WorldSystemFilterAttribute>(true);
    10.             if (attrib != null && (WorldSystemFilterFlags.Default & attrib.FilterFlags) == 0)
    11.             {
    12.                 return systems;
    13.             }
    Definitely hacky but it works for now.
     
    PublicEnumE likes this.