Hi all!
I’ve just upgraded to Entities 0.5.0-preview.17, and now I am running into a ArgumentException: The EntityArchetype was not created by this EntityManager
.
Full error:
ArgumentException: The EntityArchetype was not created by this EntityManager
EntityCommandBuffer was recorded in FiringJobSystem and played back in Unity.Entities.EndSimulationEntityCommandBufferSystem.
In the below FiringJobSystem
, I am getting a projectileRequestArchetype
, which is just an entity archetype that I have cached and am trying to reuse that signals my SpawnSystem
to create a projectile.
In the OnUpdate
, I am then trying to use commandBuffer.CreateEntity
with the archetype to generate a ProjectileRequest
entity from the archetype.
I do not understand the error – yes, the archetype was not generated by the commandBuffer
, since I cache it in another system. How can I cache the archetype, then? Do I have a fundamental misunderstanding of how archetypes or EntityManagers should be used or do I just have a silly error in the code below?
public class FiringJobSystem : JobComponentSystem
{
// EndFrameBarrier provides the CommandBuffer
EntityCommandBufferSystem barrier;
protected override void OnCreate()
{
base.OnCreate();
// Cache the EndFrameBarrier in a field, so we don't have to get it every frame.
barrier = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
}
[BurstCompile]
protected override JobHandle OnUpdate(JobHandle inputDeps)
{
var dt = Time.DeltaTime;
var projectileRequestArchetype = Archetypes.Spawn(EntityManager, typeof(ProjectileRequest));
var commandBuffer = barrier.CreateCommandBuffer().ToConcurrent();
var handle = Entities
.ForEach(
(Entity entity,
int entityInQueryIndex,
ref Weapon weapon,
ref WeaponCooldown weaponCooldown,
ref LocalToWorld ltw) =>
{
if (weaponCooldown.seconds <= 0) {
// Creates a projectile in the SpawnSystem.
var projectileRequestEntity = commandBuffer.CreateEntity(entityInQueryIndex, projectileRequestArchetype);
var translation = ltw.Position + ltw.Forward * 2f;
var target = translation + ltw.Forward;
commandBuffer.SetComponent(entityInQueryIndex, projectileRequestEntity, new ProjectileRequest {
translation = new Translation { Value = translation },
target = new Translation { Value = target },
speed = 500f,
});
}
// Reset the cooldown timer.
weaponCooldown.seconds = weapon.secondsCooldown;
}).Schedule(inputDeps);
barrier.AddJobHandleForProducer(handle);
return handle;
}
}
Many thanks in advance for any help!