I’ve made simple player control system code for my game.
public class PlayerControlSystem : JobComponentSystem
{
[BurstCompile]
[RequireComponentTag(typeof(PlayerTagComponent))]
struct PlayerControlSystemJob : IJobForEachWithEntity<FURigidInfoComponent>
{
[ReadOnly] public float3 dir;
[ReadOnly] public ComponentDataFromEntity<FUApplyForceComponent> checkComp;
public EntityCommandBuffer.Concurrent ecb;
//[ReadOnly] public ComponentDataFromEntity<>
public void Execute(Entity entity, int index, ref FURigidInfoComponent applyForceComp)
{
if(checkComp.HasComponent(entity) == false)
{
ecb.AddComponent<FUApplyForceComponent>(index, entity, new FUApplyForceComponent()
{
forceValue = dir
});
}else
{
ecb.SetComponent<FUApplyForceComponent>(index, entity, new FUApplyForceComponent()
{
forceValue = dir
});
}
}
}
EndSimulationEntityCommandBufferSystem endSimCB;
protected override void OnCreate()
{
base.OnCreate();
endSimCB = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
}
protected override JobHandle OnUpdate(JobHandle inputDependencies)
{
Debug.LogFormat("WTF?");
float3 dir = new float3(0,0,0);
if (Input.GetKey(KeyCode.W) == true)
{
dir += new float3(0, 0, 1);
Debug.LogFormat("W={0}", dir);
}
if (Input.GetKey(KeyCode.S) == true)
{
dir += new float3(0, 0, -1);
Debug.LogFormat("S={0}", dir);
}
if (Input.GetKey(KeyCode.A) == true)
{
dir += new float3(-1, 0, 0);
Debug.LogFormat("A={0}", dir);
}
if (Input.GetKey(KeyCode.D) == true)
{
dir += new float3(1, 0, 0);
Debug.LogFormat("D={0}", dir);
}
if (math.length(dir) <= 0)
return inputDependencies;
dir = math.normalize(dir);
Debug.LogFormat("No={0}", dir);
var job = new PlayerControlSystemJob()
{
dir = dir,
checkComp = GetComponentDataFromEntity<FUApplyForceComponent>(true),
ecb = endSimCB.CreateCommandBuffer().ToConcurrent()
};
var handle = job.Schedule(this, inputDependencies);
endSimCB.AddJobHandleForProducer(handle);
//return handle;
return handle;
}
}
When this code is run, it prints lots of “WTF” string on console.
But when I press input key “WASD”, then suddenly OnUpdate loop doesn’t get called!
I dont know why this happens. No error messages.
In Entity Debugger, PlayerControlSystem suddenly becomes “not run”
What did I do wrong? T_T