nullable generics type function in C# Unity

128 Views Asked by At

I am working on a system for spatial partitioning in unity dots and currently.

the workin example function looks something like this

partial struct CharCopyJob : IJobEntity
    {
        public NativeList<Entity> Entities;
        public NativeList<CharacterControllerComponent> Bodies;
        public NativeList<LocalTransform> Transforms;
        public NativeList<AICharacterStateData> CharacterStates;
        public NativeList<AIInteractionSignalData> Signals;

        void Execute(Entity entity, in CharacterControllerComponent body, in LocalTransform transform, in AICharacterStateData state, in AIInteractionSignalData signal)
        {
            Entities.Add(entity);
            Bodies.Add(body);
            Transforms.Add(transform);
            CharacterStates.Add(state);
            Signals.Add(signal);
            log(Entities.Length);
        }
    }

but as you can imagine, for every kind of entity i have to rewrite this function ( i.e. char, weapons etc).

I am trying to write a more generic job for this purpose

partial struct CopyJob<A,B,C,D,E,F,G,H,I> : IJobEntity 
        where A : unmanaged, IComponentData
        where B : unmanaged, IComponentData
        where C : unmanaged, IComponentData 
        where D : unmanaged, IComponentData
        where E : unmanaged, IComponentData 
        where F : unmanaged, IComponentData
        where G : unmanaged, IComponentData
        where H : unmanaged, IComponentData
        where I : unmanaged, IComponentData
    {
        public NativeList<A> al;
        public NativeList<B> bl;
        public NativeList<C> cl;
        public NativeList<D> dl;
        public NativeList<E> el;
        public NativeList<F> fl;
        public NativeList<G> gl;
        public NativeList<H> hl;
        public NativeList<I> il;

        void Execute(Entity entity, A a, B b, C c, D d, E e, F f, G g, H h, I i)
        {
            if(typeof(A) == typeof(Null))
                al.Add(a);
            if(typeof(B) == typeof(Null))
                bl.Add(b);
            if(typeof(C) == typeof(Null))
                cl.Add(c);
            if(typeof(D) == typeof(Null))
                dl.Add(d);
            if(typeof(E) == typeof(Null))
                el.Add(e);
            if(typeof(F) == typeof(Null))
                fl.Add(f);
            if(typeof(G) == typeof(Null))
                gl.Add(g);
            if(typeof(H) == typeof(Null))
                hl.Add(h);
            if(typeof(I) == typeof(Null))
                il.Add(i);
        }
    }

this is what i came up with but it is not working as NULL can not be unmanaged.

Any suggestions would be appreciated.

2

There are 2 best solutions below

0
On BEST ANSWER

I ended up creating multiple systems for every use. It also helps in simplifying different systems.

0
On

You do not need to implement what is there already.

Allocator allocator = Allocator.Temp;

var query = EntityManager.CreateEntityQuery(
        ComponentType.ReadOnly<CharacterControllerComponent>()
    ,   ComponentType.ReadOnly<LocalTransform>()
    ,   ComponentType.ReadOnly<AICharacterStateData>()
    ,   ComponentType.ReadOnly<AIInteractionSignalData>()
);    
var entities = query.ToEntityListAsync( allocator , out var h0 );
var bodies = query.ToComponentDataListAsync<CharacterControllerComponent>( allocator , out var h1 );
var transforms = query.ToComponentDataListAsync<LocalTransform>( allocator , out var h2 );
var characterStates = query.ToComponentDataListAsync<AICharacterStateData>( allocator , out var h3 );
var signals = query.ToComponentDataListAsync<AIInteractionSignalData>( allocator , out var h4 );
        
JobHandle copyJobHandle = JobHandle.CombineDependencies(
    new NativeList<JobHandle>(Allocator.Temp){ h0 , h1 , h2 , h3 , h4 }
);