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;
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?
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.
[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
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!
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?