Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Thursday, June 8, for a Q&A with Unity's Content Pipeline group here on the forum, and on the Unity Discord, and discuss topics around Content Build, Import Workflows, Asset Database, and Addressables!
    Dismiss Notice

Question How To Generic ISystem<Component>?

Discussion in 'Entity Component System' started by andreyakladov, Apr 2, 2023.

  1. andreyakladov

    andreyakladov

    Joined:
    Nov 11, 2016
    Posts:
    29
    How does one overcome this error:

    Code (CSharp):
    1. ArgumentException: Unknown Type:`ECS.Systems.EffectTriggerSystem`1[ECS.Components.Effects.EffectSpeed]` All ComponentType must be known at compile time. For generic components, each concrete type must be registered with [RegisterGenericComponentType].
    in:
    Code (CSharp):
    1. fixedStepSimulation.AddSystemToUpdateList(_world.CreateSystem<EffectTriggerSystem<EffectSpeed>>());
    where system:
    Code (CSharp):
    1. public partial struct EffectTriggerSystem<T> : ISystem where T: unmanaged, IEffectTimed
    where component:
    Code (CSharp):
    1. public struct EffectSpeed : IEffectTimed
    where interface:
    Code (CSharp):
    1. public interface IEffect : IComponentData, IEnableableComponent
    If I use this generic in EntityMamanger, like em.GetComponentData<T>(entity) - it works just fine, but throws when I try to use generic ISystem.
    Code (CSharp):
    1. [assembly: RegisterGenericComponentType(typeof(EffectTriggerSystem<EffectSpeed>))]
    has no effect.
     
    Last edited: Apr 2, 2023
  2. elliotc-unity

    elliotc-unity

    Unity Technologies

    Joined:
    Nov 5, 2015
    Posts:
    203
    Generic isystems don’t work right now; it’s on the todo.
     
  3. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    897
    You can try a generic SystemBase for now. It worked in 0.50. Not sure if it works on 1.0. I'm still in the process of upgrading my project.
     
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,300
    Atm. one of work around, but far from ideal as I have experienced, is to tap into Systems tate of ISystem.
     
  5. FaithlessOne

    FaithlessOne

    Joined:
    Jun 19, 2017
    Posts:
    158
    Edit: Sorry, code posted was not really generic. Removed.
     
    Last edited: Apr 10, 2023
  6. lclemens

    lclemens

    Joined:
    Feb 15, 2020
    Posts:
    621
    Does SystemBase work?
     
  7. Enzi

    Enzi

    Joined:
    Jan 28, 2013
    Posts:
    879
    Yes :)
     
    lclemens likes this.
  8. elliotc-unity

    elliotc-unity

    Unity Technologies

    Joined:
    Nov 5, 2015
    Posts:
    203
    I made generic isystems work in 1.0.8, so enjoy! (Edit: you have to say [assembly: RegisterGenericSystemType(typeof(YourSystem<YourParticularType>))], just like for generic components.)
     
    Last edited: May 19, 2023
  9. Sylmerria

    Sylmerria

    Joined:
    Jul 2, 2012
    Posts:
    356
    Generic IJobEntity soon so ? :D
     
  10. elliotc-unity

    elliotc-unity

    Unity Technologies

    Joined:
    Nov 5, 2015
    Posts:
    203
    Mmm that's harder. I'm skeptical personally, but you never know. The people who could do that (i.e. the sourcegenerator people) are currently hard at work trying to bring iteration time down, which I suspect you might prefer...?
     
    Occuros, Sylmerria and jasonboukheir3 like this.
  11. elliotc-unity

    elliotc-unity

    Unity Technologies

    Joined:
    Nov 5, 2015
    Posts:
    203
    Um, due to internal process snafus, this apparently did not make it into 1.0.8. Sorry for the confusion! I'm trying to figure out what release it will end up in. :(
     
  12. farlenkov

    farlenkov

    Joined:
    Mar 28, 2014
    Posts:
    17
    In 1.0.10 I'm trying to use RegisterGenericSystemType like this

    Code (CSharp):
    1. [assembly: RegisterGenericSystemType(typeof(MySystem))]
    And this gives me an error:

    Code (CSharp):
    1. Unity.Entities.CodeGen.EntitiesILPostProcessors: (0,0): error error DC3002: MySystem: [RegisterGenericJobType] requires an instance of a generic value type
    But I don't have any generic job types
     
  13. Enzi

    Enzi

    Joined:
    Jan 28, 2013
    Posts:
    879
    Is your system generic? Like MySystem<T>? Then you also need to declare that in the typeof.
    If not, you just need to register the generic job and not the system.
     
  14. farlenkov

    farlenkov

    Joined:
    Mar 28, 2014
    Posts:
    17
    I have this classes:
    Code (CSharp):
    1. public abstract partial MyBaseSystem<T> : SystemBase
    2.  
    3. public partial MyGameSystem : MyBaseSystem<GameState>
    And I have tried both

    Code (CSharp):
    1. [assembly: RegisterGenericSystemType(typeof(MyBaseSystem<GameState>))]
    2. [assembly: RegisterGenericSystemType(typeof(MyGameSystem))]
    I don't have any generic jobs
     
  15. Enzi

    Enzi

    Joined:
    Jan 28, 2013
    Posts:
    879
    RegisterGenericSystemType is only needed for ISystem.
    SystemBase just needs an implementation and will work.
     
  16. farlenkov

    farlenkov

    Joined:
    Mar 28, 2014
    Posts:
    17
    but if I remove RegisterGenericSystemType I get another error when calling AddSystemManaged:

    Code (CSharp):
    1. ArgumentException: Unknown Type:`MyGameSystem` All ComponentType must be known at compile time. For generic components, each concrete type must be registered with [RegisterGenericComponentType].
     
  17. Enzi

    Enzi

    Joined:
    Jan 28, 2013
    Posts:
    879
    Hm, that's odd that this is thrown.
    I have [DisableAutoCreation] on the base class. When implemented, the system is automatically added so I never need to call AddSystemManaged.