I’m using an EntityQuery to fetch component data from entities, in order to use this data in a job.
However, in my application, the component data fetched this way rarely changes.
It would be more efficient to only fetch the component data when the results of the entity query have actually changed (instead of every fame in the OnUpdate() method).
Is there a way to check if the results of the entity query have changed?
(one way to do this would be to check query.CalculateEntityCount(), but this would not work when adding and removing an entity that conforms to the query in a single frame…)
Current code example:
protected override void OnUpdate()
{
var query = GetEntityQuery(ComponentType.ReadOnly<MyComponentData>());
var data = query.ToComponentDataArray<MyComponentData>(Allocator.TempJob);
// .. do stuff with data, dispose data after having used it
}
Desired code example:
private NativeArray<MyComponentData> data;
protected override void OnUpdate()
{
var query = GetEntityQuery(ComponentType.ReadOnly<MyComponentData>());
if(query.HasChanged()) //<<< does something like this exist?
{
data.Dispose();
data = query.ToComponentDataArray<MyComponentData>(Allocator.Persistent);
}
// .. do stuff with data, dispose data on system disposal
}