以下エラーが出た時の解決方法について
InvalidOperationException: The previously scheduled job TriggerVolumePortalSystem:TriggerVolumePortalJob writes to the ComponentLookup TriggerVolumePortalJob.JobData.LocalTransformLookup. You must call JobHandle.Complete() on the job TriggerVolumePortalSystem:TriggerVolumePortalJob, before you can read from the ComponentLookup safely.
エラーの説明の通り、JobHandle.Complete()する必要があるようですが、
解決に手間取ったため、メモを残します。
私の場合、以下2行実施時にエラーが発生しました。
var transformLookup = SystemAPI.GetComponentLookup<LocalTransform>();
var startPosition = transformLookup[_playerEntity].Position;
どうやら、.PositionでEntityの情報にアクセスするのと、既存のJOBの動作が干渉してしまっているようです。
解決としては、
state.Dependency = new TriggerVolumePortalJob()
{
LocalTransformLookup = SystemAPI.GetComponentLookup<LocalTransform>(),
}.Schedule(state.Dependency);
[BurstCompile]
partial struct TriggerVolumePortalJob : IJobEntity
{
public ComponentLookup<LocalTransform> LocalTransformLookup;
LocalTransformLookup[portalEntity].Position);
のように記述する。
state.Dependency が肝のようです。海外のUNITYマニュアルとか、Discordの技術トピックとか色々調査しましたが、コードの意味は完全にはわかりませんでした。とりあえずはおまじないだと思って注意していくしかなさそうです。
もう一つ別パターンで、以下エラーが出た時の対処
ArgumentException: The previously scheduled job DamageCalculateJob writes to the Unity.Entities.EntityCommandBuffer DamageCalculateJob.JobData.ECB2. You must call JobHandle.Complete() on the job DamageCalculateJob, before you can write to the Unity.Entities.EntityCommandBuffer safely.
以下コードの追加により、エラーが回避された。(なお理由は不明...)
[RequireMatchingQueriesForUpdate]
[UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
[UpdateAfter(typeof(PhysicsSystemGroup))]
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<TriggerDamage>();
state.RequireForUpdate<SimulationSingleton>();
state.RequireForUpdate<EndSimulationEntityCommandBufferSystem.Singleton>();
}
今後勉強が必要だが、一旦メモとして残す。