I am studying the lessons on entities 1.0.0, now I am interested in the question - how to turn on/off, ISystem update?
Here is the spawn in the example
partial struct SpawnSystem : ISystem{
[BurstCompile]
public void OnCreate(ref SystemState state){
state.RequireForUpdate<SpawnDataComponent>();
}
[BurstCompile]
public void OnDestroy(ref SystemState state){}
[BurstCompile]
public void OnUpdate(ref SystemState state){
var spawnData = SystemAPI.GetSingleton<SpawnDataComponent>();
var ecbSingleton = SystemAPI.GetSingleton<BeginSimulationEntityCommandBufferSystem.Singleton>();
var ecb = ecbSingleton.CreateCommandBuffer(state.WorldUnmanaged);
var vehicles = CollectionHelper.CreateNativeArray<Entity>(spawnData.TankCount, Allocator.Temp);
ecb.Instantiate(spawnData.TankPrefab, vehicles);
state.Enabled = false;
}
}
state.Enabled = false; as I understand it, it disables the update of this system, but what if I need to create a couple of objects in the process? I am changing the data in the SpawnDataComponent and again I need to run one cycle of this system, but how do I do it?
How do I send a request to the spawner so that it creates something and turns off?
The System will run or not based on RequireForUpdate. You shouldnât need/use Enabled for this. The problem is youâre requiring for update only a singleton component which is presumably always alive due to it having the prefab reference.
The simplest solution is to require an additional tag or enableable component. I.e.
state.RequireForUpdate<SpawnDataComponent, SpawnTag>(); // â Not valid syntax but you get idea
Then add/enable SpawnTag when you want to spawn again and remove/disable spawnTag after spawning. No need for state.Enabled. This approach allows for a single spawn action per frame.
Alternatively, store the prefabs in a different singleton and spawn SpawnDataComponent entities when you need them. Then destroy those spawner entities after youâve instantiated from them. You use EntityQuery for this approach.
The system will run when said entities are present so no state.Enabled required here either. This approach allows for multiple spawn actions per frame.
You could also re-enable the system from another system but itâs a hackneyed solution versus the above data approaches.
state.WorldUnmanaged.GetExistingSystemState().Enabled = true;
Unfortunately state.RequireForUpdate doesnât check the enabled state of an IEnableableComponent (Entities 1.0.0-pre.65). From the docs of SystemState.RequireForUpdate() and SystemState.RequireForUpdate(EntityQuery)
this method ignores whether the component is enabled or not, it only checks whether it exists.```
That statement about IEnableableComponent not working with RequireForUpdate() only occurs in the remarks section for this overload: âpublic void RequireForUpdate()â. So you might think that it would still work under the overload with the query âpublic void RequireForUpdate(EntityQuery query)â. Unfortunately, I can confirm that the query overload not work either.
So this will not work as expectedâŚ
state.RequireAnyForUpdate(state.GetEntityQuery(ComponentType.ReadOnly()));