Search Unity

Adding newly created entity to dynamic buffer in job before playback

Discussion in 'Entity Component System' started by Init33, Sep 21, 2019.

  1. Init33

    Init33

    Joined:
    Aug 30, 2017
    Posts:
    67
    I'm trying to create an entity in a job and then add that entity to a dynamic buffer, but I suspect that this is not allowed. Currently I am getting an error "All entities created using EntityCommandBuffer.CreateEntity must be realized via playback(). One of the entities is still deferred (Index: -1)."

    Here is my buffer:
    Code (CSharp):
    1. [InternalBufferCapacity(256)]
    2. public struct ProjectileBuffer : IBufferElementData
    3. {
    4.     public static implicit operator Entity(ProjectileBuffer e) { return e.Value; }
    5.     public static implicit operator ProjectileBuffer(Entity e) { return new ProjectileBuffer { Value = e }; }
    6.  
    7.     public Entity Value;
    8. }
    In my job I'm basically trying to do this:

    Code (CSharp):
    1. ...
    2. var instance = CommandBuffer.Instantiate(index, prefab);
    3. var buffer = buffers[entity];
    4. buffer.Add(instance);
    5. ...
    Do I have to let the job complete, playback the command buffer and then add the created entity to the buffer somewhere after this? Or is my current approach sound?
     
  2. Singtaa

    Singtaa

    Joined:
    Dec 14, 2010
    Posts:
    492
    Yes. The
    instance
    entity in your above code is temporary and generally can only be used with the
    EntityCommandBuffer
    APIs. You'll need another system to work on the newly created entities after the ECB has run. I do the same. Shouldn't be too bad.
     
  3. Init33

    Init33

    Joined:
    Aug 30, 2017
    Posts:
    67
    Thanks,

    I added the entities to a buffer in another job after the first had completed and playback. Works fine.
     
    Singtaa likes this.