Search Unity

Creating Entities with Dependencies

Discussion in 'Entity Component System' started by learc83, Jan 24, 2019.

  1. learc83

    learc83

    Joined:
    Feb 28, 2018
    Posts:
    41
    I have blueprints entity that I iterate over and for every blueprint I create 3 entities A, B, and C. A stores a relationship to B and C, so B and C need to be created first in order to assign the references to A.

    I can't use EntityCommandBuffers to create them all because I won't have the references to B and C available yet.

    I can think of 2 solutions.

    1. Use a ComponentSystem instead of a job, and Iterate over the blueprints and store them all in a NativeArray then loop over that and create the the entities (for each blueprint, B, C, then A).

    2. Iterate over the blueprints and use an EntityCommandBuffer to create a B, C for each blueprint, and create the A with partial data (no references to B and C). But also create a temporary component for each entity with a shared unique identifier.

    Then another system runs after the barrier system that executes the EntityCommandBuffer and finds all of the components using the unique identifier, sets up the relationship and deletes the temporary component.

    I'm assuming someone else has done something similar. What worked for you?
     
  2. M_R

    M_R

    Joined:
    Apr 15, 2015
    Posts:
    559
    latest update (23) has ECB.CreateEntity returning a placeholder entity that you can use for subsequent commands in the same buffer.
    it should patch all Entity fields in components you add with the same buffer (quickly looked at the source, not tested)
     
    e199 likes this.
  3. learc83

    learc83

    Joined:
    Feb 28, 2018
    Posts:
    41
    Awesome! Thanks, that is exactly what I was looking for.