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

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;

Entities.ForEach((ref Translation translation, ref Target target) =>
    {
        manager = World.Active.EntityManager;

        if (target.InRange == true )
        {
             Entity beam = manager.Instantiate(beamEntityPrefab);
        }

    }

And the beamEntityPrefab is something like this;

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;

manager.AddComponent(entity, typeof(Entity));
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?

I use

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.

Using new Entities 0.2.0:

Create your Beam prefab like this:

[GenerateAuthoringComponent]
public struct BeamPrefab : IComponentData {
  public Entity Prefab;
}

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:

public class SpawnPrefabSystem : JobComponentSystem {

  BeginInitializationEntityCommandBufferSystem m_entityCommandBufferSystem;

  protected override void OnCreate() {
    base.OnCreate();
    m_entityCommandBufferSystem = World.GetOrCreateSystem<BeginInitializationEntityCommandBufferSystem>();
  }

  [BurstCompile]
  struct InstantiateJob : IJobForEachWithEntity<BeamPrefab> {
    public EntityCommandBuffer.Concurrent CmdBuffer;

    public void Execute(Entity entity, int index, [ReadOnly]ref BeamPrefab c0) {
      CmdBuffer.Instantiate(index, c0.Prefab);
    }
  }

  protected override JobHandle OnUpdate(JobHandle inputDeps) {
    if (Input.GetKeyDown(KeyCode.H))
      inputDeps = new InstantiateJob {
        CmdBuffer = m_entityCommandBufferSystem.CreateCommandBuffer().ToConcurrent()
      }.Schedule(this, inputDeps);
    m_entityCommandBufferSystem.AddJobHandleForProducer(inputDeps);
    return inputDeps;
  }
}

Heck EntityCommandBuffer is even burstable now :slight_smile:

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!

1 Like

Thanks, okay, I can now reference; like this

beamEntityPrefab = GameObjectConversionUtility.ConvertGameObjectHierarchy(beamPrefab, World.Active);
manager.AddComponent(miner, typeof(BeamPrefab));
manager.SetComponentData(miner, new BeamPrefab { Prefab = beamEntityPrefab });

And then instantiate like this

Entity beam = manager.Instantiate(beamPrefab.Prefab);
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?

The GameObjectConversionUtility doesn’t work anymore like it did. I am back to figuring out how to do basic conversions.

Why don’t you look at existing examples?
https://github.com/Unity-Technologies/EntityComponentSystemSamples/search?q=GameObjectConversionUtility&unscoped_q=GameObjectConversionUtility

1 Like

Well, first I couldn’t find em. Then I couldn’t understand them, but now I am trying them.
Thanks for the pointer.

edit: Hmm, World.DefaultGameObjectInjectionWorld is somewhat dodgy, but I’ll ignor that bit :slight_smile:

Okay. That works. Nice.

1 Like

In a new post, to make searching for future people TM easier, the follow up issues of parenting and rotating: Children relative to parent, not quite working

Thanks for the help so far.