Search Unity

EntityCommandBuffer.Concurrent CreateEntity question

Discussion in 'Entity Component System' started by slages, May 5, 2019.

  1. slages

    slages

    Joined:
    May 16, 2017
    Posts:
    9
    Hi all,

    When using CreateEntity from an EntityCommandBuffer.Concurrent, it returns an temporary Entity if I understand it correctly, which can be used to add components or buffers. But as soon as the playback is done, this Entity can't be used anymore, it doesn't refer to anything. So is there a way to translate this temporary entity to the real one after the playback?

    Thanks!
     
  2. June1111

    June1111

    Joined:
    Aug 11, 2017
    Posts:
    33
    After a ECB plays back it 'stitches' the entities to reference the actual created entities (think of inside of the ecb as a number of what entity was created (0, 1, 2, 3, 4) and it updates all references to point to the real one (id 134884, version 3)
    the placeholder is just entity(5, 0) which doesn't point to a real component.
    two ways to go around keeping a reference is:
    one, the ECB will patch any ComponentData reference to it, this means you can update yourself to have a reference to the new entity
    ForEach(Spawner)
    {
    temp ent = Spawn new entity
    Spawner.Entity = temp ent
    }
    since we update ourself it will contain a reference to the new entity

    or two use a tag component and run right after the ecb plays back and convert all tag components, removing the tag in the process.

    on some main thread foreach's I tend to construct a ECB, run a foreach that stores changes into that ecb, then right after the foreach, ecb.playback(entitymanger) to apply the changes right away. then run another query right afterwards to grab the tagged components and store the references. this will work for jobs as well, but you might need to complete the job, or run a secondary system afterwards to update the refs.
     
    SergeyRomanko and cort_of_unity like this.