- Home /
ECS/Dots: How do get combined data of component values/make this accessible to all entities?
I have lots of components that all have a float3 position property. I want the combined float3 of all of the components to be able to calculated, and I want the "combined positions" float3 to be accessible from all of the entities. How to do this?
Answer by andrew-lukasik · Nov 18, 2020 at 12:48 PM
Idea is to schedule jobs in such a way that you pass this data down the dependency stream:
public class CalculateAveragePositionSystem : SystemBase
{
EntityQuery _query;
NativeArray<float3> _averagePosition;
protected override void OnCreate ()
{
_query = EntityManager.CreateEntityQuery(
ComponentType.ReadOnly<AveragePositionSrc>()
, ComponentType.ReadOnly<LocalToWorld>()
);
_averagePosition = new NativeArray<float3>( 1 , Allocator.Persistent );
}
protected override void OnDestroy ()
{
_averagePosition.Dispose();
}
protected override void OnUpdate ()
{
NativeArray<LocalToWorld> ltrArray = _query.ToComponentDataArray<LocalToWorld>( Allocator.TempJob );
var averagePosition = _averagePosition;// alias
Job
.WithName("calculate_sum_job")
.WithDisposeOnCompletion( ltrArray )
.WithCode( ()=>
{
float3 vec = default(float3);
for( int i=0 ; i<ltrArray.Length ; i++ )
vec += ltrArray[i].Position;
vec /= (float)ltrArray.Length;
averagePosition[0] = vec;
} )
.WithBurst().Schedule();
Entities
.WithName("do_something_with_that_data_job")
.WithReadOnly( averagePosition )
.ForEach( ( ref AveragePositionDst averagePositionMarker ) =>
{
averagePositionMarker.Value = averagePosition[0];
} )
.WithBurst().ScheduleParallel();
}
}
public struct AveragePositionSrc : IComponentData {}
public struct AveragePositionDst : IComponentData { public float3 Value; }
Answer by PincerGame · Nov 18, 2020 at 10:24 AM
Hello @emerz210, to combine multiple queries, you can pass an array of EntityQueryDesc. Check the below code example. The following example selects any archetypes that contain a RotationQuaternion component or a RotationSpeed component (or both):
var desc1 = new EntityQueryDesc
{
All = new ComponentType[] { typeof(RotationQuaternion) }
};
var desc2 = new EntityQueryDesc
{
All = new ComponentType[] { typeof(RotationSpeed) }
};
EntityQuery query
= GetEntityQuery(new EntityQueryDesc[] { desc1, desc2 });
This is a good answer for some very different question.
He wants to sum float3
values. Not merge EntityQuery
instances.
Your answer

Follow this Question
Related Questions
Label dots in between numbers (currency formatting) 2 Answers
How can I get a component data from an entity inside a JobComponentSystem? 1 Answer
Instantiated prefab missing ECS components 1 Answer
Will Allocator.Temp erase contents of my native containers? 1 Answer
Where is com.unity.animation? 1 Answer