Search Unity

Create Entity, from an Entity; how do I reference?

Discussion in 'Entity Component System' started by Reloque, Nov 28, 2019.

  1. Reloque

    Reloque

    Joined:
    Apr 28, 2015
    Posts:
    207
    TL;DR: How do create a bullet as an Entity from an Entity?

    Long version:
    Maybe I am missing something, or just not searching the right terms, but here is the thing.

    I have a spaceship, as an Entity.
    I have a beam bullet prefab. That I can convert to an entity.

    I want, under certain conditions, to have that spaceship fire that bullet. So, I take care of that in a job. Basically, the job is something like this;

    Code (CSharp):
    1. Entities.ForEach((ref Translation translation, ref Target target) =>
    2.     {
    3.         manager = World.Active.EntityManager;
    4.  
    5.         if (target.InRange == true )
    6.         {
    7.              Entity beam = manager.Instantiate(beamEntityPrefab);
    8.         }
    9.  
    10.     }
    And the beamEntityPrefab is something like this;

    Code (csharp):
    1. beamEntityPrefab= GameObjectConversionUtility.ConvertGameObjectHierarchy(beamPrefab, World.Active);
    So, what I can't wrap my head around is how to get that beamEntityPrefab into the job. I've thought about doing it like a component during conversion;
    Code (csharp):
    1. manager.AddComponent(entity, typeof(Entity));
    2. manager.SetComponentData(entity, new Entity { ??? = beamPrefabEntity };
    But that doesn't really add up, there is not a prefab reference to be assigned there. I am half convinced I am just approaching this all wrong, but can't seem to break out my mistake. Anyone have a nudge in the right direction?
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,778
    I use
    Code (CSharp):
    1. public void Convert ( ... )
    Haven't been using GameObjectConversionUtility.
    But does't it return beamEntityPrefab entity?
    If so, you simply put that entity into struct and access via a job.
    I have struct with stored prefabs.
    You could also add tag, to identify specific entity prefab and access it that way.
     
  3. GilCat

    GilCat

    Joined:
    Sep 21, 2013
    Posts:
    676
    Using new Entities 0.2.0:

    Create your Beam prefab like this:
    Code (CSharp):
    1. [GenerateAuthoringComponent]
    2. public struct BeamPrefab : IComponentData {
    3.   public Entity Prefab;
    4. }
    Then you can have a system spawning that prefab in any fashion you might need.
    Here is an example of spawning it by pressing a key for the sake of simplicity:
    Code (CSharp):
    1. public class SpawnPrefabSystem : JobComponentSystem {
    2.  
    3.   BeginInitializationEntityCommandBufferSystem m_entityCommandBufferSystem;
    4.  
    5.   protected override void OnCreate() {
    6.     base.OnCreate();
    7.     m_entityCommandBufferSystem = World.GetOrCreateSystem<BeginInitializationEntityCommandBufferSystem>();
    8.   }
    9.  
    10.   [BurstCompile]
    11.   struct InstantiateJob : IJobForEachWithEntity<BeamPrefab> {
    12.     public EntityCommandBuffer.Concurrent CmdBuffer;
    13.  
    14.     public void Execute(Entity entity, int index, [ReadOnly]ref BeamPrefab c0) {
    15.       CmdBuffer.Instantiate(index, c0.Prefab);
    16.     }
    17.   }
    18.  
    19.   protected override JobHandle OnUpdate(JobHandle inputDeps) {
    20.     if (Input.GetKeyDown(KeyCode.H))
    21.       inputDeps = new InstantiateJob {
    22.         CmdBuffer = m_entityCommandBufferSystem.CreateCommandBuffer().ToConcurrent()
    23.       }.Schedule(this, inputDeps);
    24.     m_entityCommandBufferSystem.AddJobHandleForProducer(inputDeps);
    25.     return inputDeps;
    26.   }
    27. }
    Heck EntityCommandBuffer is even burstable now :)

    Remember create a gameobject with the BeamPrefab on it and add convertToEntity (or put it in a subscene) and reference your prefab to be spawned and it's up and running. As simple as that!
     
    florianhanke likes this.
  4. Reloque

    Reloque

    Joined:
    Apr 28, 2015
    Posts:
    207
    Thanks, okay, I can now reference; like this
    Code (CSharp):
    1. beamEntityPrefab = GameObjectConversionUtility.ConvertGameObjectHierarchy(beamPrefab, World.Active);
    2. manager.AddComponent(miner, typeof(BeamPrefab));
    3. manager.SetComponentData(miner, new BeamPrefab { Prefab = beamEntityPrefab });
    And then instantiate like this

    Code (CSharp):
    1. Entity beam = manager.Instantiate(beamPrefab.Prefab);
    2. manager.SetComponentData(beam, new Translation { Value = new float3(10,0,0) });
    However, the entity spawns at 0,0,0 and not 10,0,0. It seems to ignore the component data set instruction. It spawns at whatever coordinates the prefab itself is set to at design time. Any idea why?
     
  5. Reloque

    Reloque

    Joined:
    Apr 28, 2015
    Posts:
    207
    The GameObjectConversionUtility doesn't work anymore like it did. I am back to figuring out how to do basic conversions.
     
  6. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,778
  7. Reloque

    Reloque

    Joined:
    Apr 28, 2015
    Posts:
    207
  8. Reloque

    Reloque

    Joined:
    Apr 28, 2015
    Posts:
    207
    Okay. That works. Nice.
     
    Antypodish likes this.
  9. Reloque

    Reloque

    Joined:
    Apr 28, 2015
    Posts:
    207