Search Unity

Support for abstract Systems

Discussion in 'Entity Component System' started by Daxten, Jul 21, 2021.

  1. Daxten

    Daxten

    Joined:
    Sep 20, 2013
    Posts:
    30
    Hi,

    I can totaly understand that polymorphism for example is a hard topic for Dots. Still I think that abstract systems should be easily supportable, are those planned?

    Currently I see that big games might take exponentially more code with Dots, just because we can't use any form of "modern" model designs / language features.

    Example would be:

    Code (CSharp):
    1. using Unity.Entities;
    2. using Unity.VisualScripting;
    3. using UnityEngine;
    4.  
    5. namespace Systems.Chunks
    6. {
    7.     public struct SpecificParticle : IComponentData, ParticleData
    8.     {
    9.         public int x;
    10.         public string someString;
    11.  
    12.         public int genericData => x;
    13.     }
    14.  
    15.     public interface ParticleData
    16.     {
    17.         public int genericData { get; }
    18.     }
    19.  
    20.     public abstract class AbstractTest<T> : SystemBase where T : IComponentData, ParticleData
    21.     {
    22.         protected abstract string somethingSpecific(T data);
    23.  
    24.  
    25.         protected override void OnUpdate()
    26.         {
    27.             Entities.ForEach((Entity e, in T data) =>
    28.             {
    29.                 Debug.Log($"Got generic data ${data.genericData} but also specific ${somethingSpecific(data)}");
    30.             }).Run();
    31.         }
    32.     }
    33.  
    34.     public class SpecificSystem : AbstractTest<SpecificParticle>
    35.     {
    36.         protected override void OnCreate()
    37.         {
    38.             var entity = EntityManager.CreateEntity();
    39.             EntityManager.AddComponentData(entity, new SpecificParticle()
    40.             {
    41.                 x = 10,
    42.                 someString = "Hello World"
    43.             });
    44.         }
    45.  
    46.         protected override string somethingSpecific(SpecificParticle data)
    47.         {
    48.             return data.someString;
    49.         }
    50.     }
    51. }
    Since all Types are known at compile time, this should be not too hard to support as far as I can see
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,264
    Lambda jobs might be supported once C# source generators arrive. Otherwise, abstract and generic systems are supported as long as you write jobs manually using IJob and IJobEntityBatch instead of lambdas.