Hi there, I’m trying to understand how SystemAPI.Query works in certain scenarios. Maybe you know the answer and can lend me a hand ![]()
Context:
- I’m mostly using
Systemsin its main-thread form (noJobs) - I run queries using
SystemAPI.Querywithidiomatic foreach
Scenario 1: SystemAPI.Query & structural-changes
When inside the idiomatic foreach we trigger a structural change that will affect the Entities that fall in the Query.
public struct MyComponent : IComponentData {
public float MyFloat;
}
public struct MyTag : IComponentData, IEnableableComponent {}
// Inside an IService's OnUpdate method
foreach (var (myComponent, entity) in
SystemAPI.Query<RefRW<MyComponent>>()
.WithPresent<MyTag>()
.WithEntityAccess() {
myComponent.ValueRW.MyFloat += 1;
state.EntityManager.RemoveComponent<MyTag>(entity);
}
Questions:
- What is the impact of the structural change in this context?
- Does it affect the Query?
- Should I use an
Entity Command Bufferinstead in this scenario?
Scenario 2: SystemAPI.Query & Enableable Components
Following a similar structure to scenario 1. But instead of causing a structural change, we modify the state on an IEnableableComponent that is part of the query.
public struct MyComponent : IComponentData {
public float Value;
}
public struct MyTag : IComponentData, IEnableableComponent {}
// Inside an IService's OnUpdate method
foreach (var (myTag) in
SystemAPI.Query<EnabledRefRW<MyTag>>
.WithAny<MyTag>() {
myTag.ValueRW = false;
}
Questions:
While changing the value of an IEnableable is not a structural change, we are filtering components with a MyTag and enabled: true
- It’s OK to change the
valueof theIEnableableinside theforeach? - How does this affect the Query?
- Should I use an
Entity Command Bufferinstead in this scenario? - Is the
.WithAny<MyTag>redundant in this example? I.e. DoesEnabledRefRWonly selectscomponentsthat haveenabled: true?
Thank you,
And as always, live long and prosper ![]()