Generic systems unsupported or I'm doing something wrong?

Title. I’ve been trying to create a generic system, but it simply doesn’t appear in the EntityDebugger.
Neither its created or updated, and it doesn’t produce any error.

So its unsupported? If so, will it be supported in the future?

You need to create the concrete version of the system you want to use or else it wont be automatically added.
For example:

class GenericSystem<T> : JobComponentSystem where T : struct {

}

class ConcreteSystem : GenericSystem<int> {

}

Concrete system will be added.

You can also manually instantiate those generic systems and add them to the playerloop.

1 Like

So its basically identical to inheritance implementation. Hmm. Cool, thanks :slight_smile:

Yeah basically that.

If you really want to have those systems dynamically created and not have to go through declaring them for all the generic types you need you can also do it.
I have created a World extension for doing just that.
Here it is:

 public static class WorldExt {

    public static ComponentSystemBase ForceGetOrCreateSystem(this World world, Type type) {
      var system = world.GetOrCreateSystem(type);
      if (!IsSystemAssignedToAnyGoup(world, system)) {
        var groups = type.GetCustomAttributes(typeof(UpdateInGroupAttribute), true);
        if (groups.Length == 0) {
          var simulationSystemGroup = world.GetOrCreateSystem<SimulationSystemGroup>();
          simulationSystemGroup.AddSystemToUpdateList(system);
        }

        foreach (var g in groups) {
          var group = g as UpdateInGroupAttribute;
          if (group == null)
            continue;

          if (!(typeof(ComponentSystemGroup)).IsAssignableFrom(group.GroupType)) {
            Debug.LogError($"Invalid [UpdateInGroup] attribute for {type}: {group.GroupType} must be derived from ComponentSystemGroup.");
            continue;
          }

          var groupMgr = world.GetOrCreateSystem(group.GroupType);
          if (groupMgr == null) {
            Debug.LogWarning(
                $"Skipping creation of {type} due to errors creating the group {group.GroupType}. Fix these errors before continuing.");
            continue;
          }
          var groupSys = groupMgr as ComponentSystemGroup;
          if (groupSys != null) {
            groupSys.AddSystemToUpdateList(world.GetOrCreateSystem(type) as ComponentSystemBase);
          }
        }
      }
      return system;
    }

    public static T ForceGetOrCreateSystem<T>(this World world) where T : ComponentSystemBase {
      var system = world.GetOrCreateSystem<T>();
      return ForceGetOrCreateSystem(world, typeof(T)) as T;
    }

    private static bool IsSystemAssignedToAnyGoup(World world, ComponentSystemBase system) {
      var groups = world.Systems.Where(s => s is ComponentSystemGroup).Cast<ComponentSystemGroup>();
      return groups.Any(group => group.Systems.Any(s => s == system));
    }
  }

Then you can simply do this:

World.ForceGetOrCreateSystem<GenericSystem<int>>()
1 Like