0

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.

4
  • in general packing structs into an interface is kinda hacky ... what speaks against the upper stricter typed job? sounds like it makes sense anyway if the signature and outcome is of certain types anyway ... otherwise I would probably rather go with individual jobs for each individual list .. those again can be strict typed then Commented Jun 12, 2023 at 6:30
  • also what exactly are those checks supposed to do if(typeof(A) == typeof(Null))? Commented Jun 12, 2023 at 6:33
  • 1
    You cannot check if a type is null, that just does not make any sense, null is not a type. You can check if values are null, just do if(a == null). But a large number of generic type arguments is a bit of a code smell, so you might want to consider if this is the best design. Commented Jun 12, 2023 at 6:55
  • yes, you are right this is horrible code and I should have created different maps from the beginning. Commented Jul 29, 2023 at 15:12

2 Answers 2

0

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 }
);
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.