Game.FindOwnersSystem
Assembly: Assembly-CSharp
Namespace: Game.Tools
Type: class
Base: GameSystemBase
Summary:
FindOwnersSystem locates and assigns the Owner component for sub-entities (nodes, edges, areas, objects) by matching their OwnerDefinition data to existing owner entities (SubNet, SubArea, SubObject). It does this by collecting owner archetype chunks, then running a Burst-compiled parallel IJobChunk (SetSubEntityOwnerJob) which scans owners and sets the m_Owner field on the Owner component for matching sub-entities. Matching is done either by exact Transform equality (position & rotation) or — for curve-based owners — by closeness on the curve (with a small Y-axis scaling for distance computations). The system removes the OwnerDefinition component from processed sub-entities using a ModificationBarrier3 command buffer.
Fields
-
private ModificationBarrier3 m_ModificationBarrier
Used to create a command buffer (parallel writer) that removes the OwnerDefinition component from sub-entities as they are processed. The barrier ensures changes are applied safely with jobs. -
private EntityQuery m_OwnersQuery
An EntityQuery that selects updated owner entities. Constructed to match entities that have Updated and any of SubNet / SubArea / SubObject, and excludes Deleted. -
private EntityQuery m_SubEntityQuery
An EntityQuery that selects sub-entities that need owner resolution. It requires OwnerDefinition and Owner components and any of Node / Edge / Area / Object. This query is required for the system to run (RequireForUpdate is called). -
private TypeHandle __TypeHandle
Internal struct that stores EntityTypeHandle and ComponentTypeHandles used by jobs (PrefabRef, Transform, Curve, OwnerDefinition, Temp, Owner). Those handles are assigned in OnCreateForCompiler.
Properties
- None.
Constructors
public FindOwnersSystem()
[Preserve] Default constructor. Standard system construction; most initialization is done in OnCreate.
Methods
protected override void OnCreate()
Initializes the system:- Retrieves the ModificationBarrier3 system from the World.
- Builds m_OwnersQuery to find owner entities: All: Updated; Any: SubNet, SubArea, SubObject; None: Deleted.
- Builds m_SubEntityQuery to find sub-entities to process: All: OwnerDefinition, Owner; Any: Node, Edge, Area, Object.
-
Calls RequireForUpdate(m_SubEntityQuery) so the system runs only when there are candidate sub-entities.
-
protected override void OnUpdate()
Main update: - Produces an asynchronous list of archetype chunks matching m_OwnersQuery (ownerChunks).
- Schedules SetSubEntityOwnerJob as a parallel job over m_SubEntityQuery. The job receives the ownerChunks list and component type handles from __TypeHandle via InternalCompilerInterface.Get*TypeHandle calls.
- Disposes ownerChunks with jobHandle dependency and registers the job with the modification barrier (AddJobHandleForProducer).
-
Updates base.Dependency with the scheduled job handle.
-
protected override void OnCreateForCompiler()
Used by the generated/compiled code path: assigns query handles and component type handles by calling __AssignQueries and __TypeHandle.__AssignHandles. -
private void __AssignQueries(ref SystemState state)
Compiler-generated stub that could populate additional queries (in this file it's essentially a placeholder and currently does nothing other than temp EntityQueryBuilder disposal).
Nested types (important runtime behavior):
public struct SetSubEntityOwnerJob : IJobChunk
(Burst compiled)- Purpose: Iterate sub-entity chunks and resolve their Owner.m_Owner by scanning provided owner archetype chunks.
- Key fields:
- [ReadOnly] NativeList
m_OwnerChunks — list of owner chunks to search. - [ReadOnly] EntityTypeHandle m_EntityType — used to retrieve entity IDs for owner chunks and sub-entities.
- [ReadOnly] ComponentTypeHandle
m_PrefabRefType — prefab id per owner. - [ReadOnly] ComponentTypeHandle
m_TransformType — transform per owner (used when present). - [ReadOnly] ComponentTypeHandle
m_CurveType — curve per owner (used when transform not present). - [ReadOnly] ComponentTypeHandle
m_OwnerDefinitionType — sub-entity desired owner info. - [ReadOnly] ComponentTypeHandle
m_TempType — used to match temp vs non-temp chunks. - ComponentTypeHandle
m_OwnerType — read/write Owner component on sub-entities. - EntityCommandBuffer.ParallelWriter m_CommandBuffer — used to remove the OwnerDefinition component.
- [ReadOnly] NativeList
- Behavior (Execute):
- For each sub-entity in the chunk:
- Reads its OwnerDefinition and Owner components.
- Removes OwnerDefinition component via command buffer.
- Iterates all ownerChunks:
- Skips owner chunk if it has a different Temp presence (so temps match).
- Obtains owner entities, PrefabRef, Transform and Curve arrays for the owner chunk.
- If Transform array length > 0: it looks for exact prefab, position and rotation equality to select owner (fast exact match).
- Else (no Transform, but Curve present): compares position and rotation differences relative to the curve endpoints; if the sub-entity already has an Owner, it requires exact zero difference; otherwise it computes squared distance with Y scaled and picks the closest owner (updates value.m_Owner accordingly).
- Writes back the possibly-updated Owner component into the sub-entity.
-
Note: The job uses Burst and is scheduled with ScheduleParallel over m_SubEntityQuery.
-
private struct TypeHandle
- Internal container of type handles required by the job and system:
- EntityTypeHandle, ComponentTypeHandle
(RO), ComponentTypeHandle (RO), ComponentTypeHandle (RO), ComponentTypeHandle (RO), ComponentTypeHandle (RO), ComponentTypeHandle (RW).
- EntityTypeHandle, ComponentTypeHandle
- __AssignHandles(ref SystemState state) fills the handles from the provided SystemState.
Usage Example
[Preserve]
protected override void OnUpdate()
{
// This mirrors how FindOwnersSystem schedules its job internally.
JobHandle outJobHandle;
var ownerChunks = m_OwnersQuery.ToArchetypeChunkListAsync(Allocator.TempJob, out outJobHandle);
var jobHandle = JobChunkExtensions.ScheduleParallel(new SetSubEntityOwnerJob
{
m_OwnerChunks = ownerChunks,
m_EntityType = InternalCompilerInterface.GetEntityTypeHandle(ref __TypeHandle.__Unity_Entities_Entity_TypeHandle, ref base.CheckedStateRef),
m_PrefabRefType = InternalCompilerInterface.GetComponentTypeHandle(ref __TypeHandle.__Game_Prefabs_PrefabRef_RO_ComponentTypeHandle, ref base.CheckedStateRef),
m_TransformType = InternalCompilerInterface.GetComponentTypeHandle(ref __TypeHandle.__Game_Objects_Transform_RO_ComponentTypeHandle, ref base.CheckedStateRef),
m_CurveType = InternalCompilerInterface.GetComponentTypeHandle(ref __TypeHandle.__Game_Net_Curve_RO_ComponentTypeHandle, ref base.CheckedStateRef),
m_OwnerDefinitionType = InternalCompilerInterface.GetComponentTypeHandle(ref __TypeHandle.__Game_Tools_OwnerDefinition_RO_ComponentTypeHandle, ref base.CheckedStateRef),
m_TempType = InternalCompilerInterface.GetComponentTypeHandle(ref __TypeHandle.__Game_Tools_Temp_RO_ComponentTypeHandle, ref base.CheckedStateRef),
m_OwnerType = InternalCompilerInterface.GetComponentTypeHandle(ref __TypeHandle.__Game_Common_Owner_RW_ComponentTypeHandle, ref base.CheckedStateRef),
m_CommandBuffer = m_ModificationBarrier.CreateCommandBuffer().AsParallelWriter()
}, m_SubEntityQuery, JobHandle.CombineDependencies(outJobHandle, base.Dependency));
ownerChunks.Dispose(jobHandle);
m_ModificationBarrier.AddJobHandleForProducer(jobHandle);
base.Dependency = jobHandle;
}
Notes and implementation details: - The system uses a modification barrier (ModificationBarrier3) to perform structural changes safely from jobs (removing OwnerDefinition). - Exact transform equality is preferred when owner Transform components exist; otherwise curve endpoints and proximity are used for matching. - The job is Burst-compiled and executed in parallel via JobChunkExtensions.ScheduleParallel for performance on many sub-entities. - The TypeHandle struct and __AssignQueries/__AssignHandles methods are present for the compiled/SystemState-driven ECS codegen pattern used in this project.