Can we instantiate a prefab with an existing entity?

Hey all!

TL;DR:
Is there a way to Instantiate in IJobs with an already existing entity and a prefab entity?
Like, CommandBuffer.Instantiate(Entity existingEntity, Entity prefabEntity)

I currently have this spawner code from the samples:

struct SpawnJob : IJobForEachWithEntity<SpawnCommand>
    {
        public EntityCommandBuffer CommandBuffer;

        public void Execute(Entity entity, int index, [ReadOnly] ref SpawnCommand spawner)
        {
            var instance = CommandBuffer.Instantiate(spawner.prefab);        

            CommandBuffer.SetComponent(instance, new Translation { Value = spawner.position });
            CommandBuffer.SetComponent(instance, new Rotation { Value = spawner.rotation });

            CommandBuffer.RemoveComponent<SpawnCommand>(entity);
        }
    }

I was fine with it at first but I can’t work around a design flaw now. See, I rather want to create my entities with all their data and then async load the models for it.
Till now I spawned the instance and put a SpawnComplete comp on it so I can put the gameplay data on it after spawn.
I also didn’t want to write game design specific code into the spawner code. That turned into quite a mess and didn’t feel right and I quickly reverted.
So, what would be helpful is to have CommandBuffer.Instantiate(Entity existingEntity, Entity prefabEntity)

edit:
I should mention, I’m aware this could lead to entities being too complex. I was also thinking about writing some kind of link system but I’m not sure how that works in ECS and if it’s even feasible to have systems work on archetypes that just reference to another set of comps.

Anyway, any ideas are welcome!

Thanks and greetings,
Thomas

What’s stopping you from doing that? After instantiating an entity with necessary comp datas, you can have separate systems work on the entity in the subsequent frame. Is there some special requirements with “async load the models”?

I’m not sure if I get your meaning. Do you mean sth like that?

struct SpawnJob : IJobForEachWithEntity<SpawnCommand>
{
    void SetPrefab()
    {
        var instance = CommandBuffer.Instantiate(spawner.prefab);       
        CommandBuffer.SetComponent(instance, new Translation { Value = spawner.position });
        CommandBuffer.SetComponent(instance, new Rotation { Value = spawner.rotation });
    }
    public EntityCommandBuffer CommandBuffer;

    public void Execute(Entity entity, int index, [ReadOnly] ref SpawnCommand spawner)
    {
        SetPrefab();
        CommandBuffer.RemoveComponent<SpawnCommand>(entity);
    }
}

Ok, let me eleborate.
If I have a seperate system for building creation I need to create the entity there. Due to PostUpdateCommands.CreateEntity returning -1, I can only reference the entity after the commands have been done.
So I’d need a job for creation and postCreation to be able to do this. Which I don’t want just from a design perspective.

The CommandBuffer.Instantiate is creating a new entity and I can’t stop this behaviour so from a design perspective I seem to be forced to use the CommandBuffer.Instantiate instead of the CreateEntity.
Under the hood CommandBuffer.Instantiate is creating an entity and adds the necessary render components from the converted prefab. I’m not using any proxies because I don’t want to set proxy data in a system and I don’t think proxies are supposed to do that.
If I could stop CommandBuffer.Instantiate from spawning it’s own entity and provide my own to it, it would solve some issues I’m having.

BUT, maybe the SpawnJob is the design flaw and I should just scrap it and use a function like eterlan suggested.
I just liked the ECS approach of seperate closed of systems that do their thing, are handy to have and run super fast.

Adding so many components to one entity also got me thinking, so my current solution puts a PrefabModel with a reference to the instanced prefab model on the building entities.
The bad thing about it, even though it’s better structured from a data perspective, if I want the Translation/Rotation I have to call EntityManager to give me the comps of the instanced prefab model.
That could be solved with having a chunk system running and I have access to 2 different chunks but that’s also something I don’t know how it works yet.

And I’m interested how you are handling more complex entity creation in your code. Are you all having seperate create code?