Game.SubAreaReferencesSystem
Assembly: Game
Namespace: Game.Areas
Type: class
Base: GameSystemBase
Summary: SubAreaReferencesSystem is a small ECS system that keeps per-owner SubArea buffers in sync with Area entities. It watches Area entities that have an Owner and react to Created/Deleted (and Temp) state to add or remove SubArea entries in the owner's buffer. The system uses a chunk job (Burst-compiled) to perform fast, chunked updates and uses ComponentLookup/BufferLookup to access other entity data safely on worker threads.
Fields
-
private EntityQuery m_SubAreaQuery
Used to select Area entities with Owner and either Created or Deleted tags. The query is created in OnCreate and is required for system updates (RequireForUpdate). This query drives which chunks are processed by the job scheduled in OnUpdate. -
private TypeHandle __TypeHandle
Internal struct instance that caches the EntityTypeHandle, ComponentTypeHandles and Component/Buffer lookups needed by the job. The handles are assigned during compiler-time helper OnCreateForCompiler by calling __AssignHandles on the TypeHandle.
Properties
- (none)
Constructors
public SubAreaReferencesSystem()
Default constructor. Marked with [Preserve] to avoid being stripped by code stripping. The constructor does not perform initialization; real initialization happens in OnCreate.
Methods
-
protected override void OnCreate()
Initializes the m_SubAreaQuery with an EntityQuery that selects entities with Area and Owner, and with either Created or Deleted. Calls RequireForUpdate(m_SubAreaQuery) so the system only runs when there are matching entities. -
protected override void OnUpdate()
Creates and populates an UpdateSubAreaReferencesJob (the chunk job), filling in the EntityTypeHandle, ComponentTypeHandles, ComponentLookup and BufferLookup via InternalCompilerInterface using the cached __TypeHandle. Schedules the chunk job with JobChunkExtensions.Schedule and assigns the returned JobHandle to base.Dependency to chain dependencies. -
protected override void OnCreateForCompiler()
Compiler helper invoked by generated code: calls __AssignQueries and __TypeHandle.__AssignHandles to set up query and type handles for the system during build/compilation phases. -
private void __AssignQueries(ref SystemState state)
Compiler-generated helper for assigning queries. In this class it creates an EntityQueryBuilder and disposes it; present to satisfy the generated workflow.
Nested types (important behavior):
private struct UpdateSubAreaReferencesJob : IJobChunk
Burst-compiled chunk job that implements the chunk-level logic. For each chunk of Area entities it:- Reads the Entity array and Owner components from the chunk.
- If the chunk has the Created component:
- If it also has the Temp component: for each area in the chunk, it checks if the owner entity has a Temp component and a SubArea buffer; if so, it tries to add a SubArea (unique) representing the area to the owner's SubArea buffer.
- Otherwise (Created without Temp): it adds the SubArea for each area to the owner's SubArea buffer (if present).
- If the chunk does not have Created (meaning entities are Deleted in the query conditions), it removes the SubArea value for each area from the owner's SubArea buffer.
- Uses CollectionUtils.TryAddUniqueValue and CollectionUtils.RemoveValue to maintain unique entries.
-
Fields used by the job:
- m_EntityType: EntityTypeHandle for chunk entity access.
- m_OwnerType: ComponentTypeHandle
(ReadOnly) for Owner components. - m_CreatedType: ComponentTypeHandle
(ReadOnly) to check Created presence. - m_TempType: ComponentTypeHandle
(ReadOnly) to check Temp presence on the chunk. - m_TempData: ComponentLookup
(ReadOnly) to check Temp presence on the owner entity. - m_SubAreas: BufferLookup
(read/write) to access the owner's SubArea buffer.
-
private struct TypeHandle
A helper struct that stores all the handles required by the job: - EntityTypeHandle __Unity_Entities_Entity_TypeHandle
- ComponentTypeHandle
__Game_Common_Owner_RO_ComponentTypeHandle - ComponentTypeHandle
__Game_Common_Created_RO_ComponentTypeHandle - ComponentTypeHandle
__Game_Tools_Temp_RO_ComponentTypeHandle - ComponentLookup
__Game_Tools_Temp_RO_ComponentLookup - BufferLookup
__Game_Areas_SubArea_RW_BufferLookup - Method __AssignHandles(ref SystemState state) to obtain these handles from the SystemState. This method is marked AggressiveInlining.
Notes on attributes: - UpdateSubAreaReferencesJob is marked with [BurstCompile] to enable Burst optimizations. - Several methods and the constructor are marked with [Preserve] to avoid code stripping.
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
m_SubAreaQuery = GetEntityQuery(new EntityQueryDesc
{
All = new ComponentType[2]
{
ComponentType.ReadOnly<Area>(),
ComponentType.ReadOnly<Owner>()
},
Any = new ComponentType[2]
{
ComponentType.ReadOnly<Created>(),
ComponentType.ReadOnly<Deleted>()
}
});
RequireForUpdate(m_SubAreaQuery);
}
[Preserve]
protected override void OnUpdate()
{
var jobData = new UpdateSubAreaReferencesJob
{
m_EntityType = InternalCompilerInterface.GetEntityTypeHandle(ref __TypeHandle.__Unity_Entities_Entity_TypeHandle, ref base.CheckedStateRef),
m_OwnerType = InternalCompilerInterface.GetComponentTypeHandle(ref __TypeHandle.__Game_Common_Owner_RO_ComponentTypeHandle, ref base.CheckedStateRef),
m_CreatedType = InternalCompilerInterface.GetComponentTypeHandle(ref __TypeHandle.__Game_Common_Created_RO_ComponentTypeHandle, ref base.CheckedStateRef),
m_TempType = InternalCompilerInterface.GetComponentTypeHandle(ref __TypeHandle.__Game_Tools_Temp_RO_ComponentTypeHandle, ref base.CheckedStateRef),
m_TempData = InternalCompilerInterface.GetComponentLookup(ref __TypeHandle.__Game_Tools_Temp_RO_ComponentLookup, ref base.CheckedStateRef),
m_SubAreas = InternalCompilerInterface.GetBufferLookup(ref __TypeHandle.__Game_Areas_SubArea_RW_BufferLookup, ref base.CheckedStateRef)
};
base.Dependency = JobChunkExtensions.Schedule(jobData, m_SubAreaQuery, base.Dependency);
}
Additional notes / tips: - Ensure the owner's entity has a SubArea buffer component present before relying on buffer operations. The job checks m_SubAreas.HasBuffer(owner) before attempting modifications. - The job uses CollectionUtils.TryAddUniqueValue to avoid duplicate SubArea entries. If you need other semantics (allowing duplicates, custom comparison) modify the collection helpers or buffer content accordingly. - Because this is a chunk job operating on handles/lookups, preserve correct read/write annotations: the job expects certain handles as ReadOnly and the buffer lookup as writable. - The system is compiler-generated (note [CompilerGenerated] on the class) and contains several helper methods used during AOT/IL2CPP generation; avoid manual removal or renaming of those helpers unless you fully understand the code generation pipeline.