Game.Areas.ServiceDistrictSystem
Assembly: Game
Namespace: Game.Areas
Type: class
Base: GameSystemBase
Summary:
ServiceDistrictSystem is an ECS system that keeps ServiceDistrict dynamic buffers clean by removing entries that reference districts which have been deleted. It finds all entities marked with Deleted + District and collects those district entities, then schedules a parallel IJobChunk (RemoveServiceDistrictsJob) that iterates ServiceDistrict buffers and removes any ServiceDistrict entries whose m_District matches a deleted district. The system uses Unity's chunk/buffer type handles and asynchronous ToEntityListAsync to avoid stalls and to integrate with the Jobs system and system dependencies.
Fields
-
private EntityQuery m_DeletedDistrictQuery
m_DeletedDistrictQuery is an EntityQuery that matches entities with the Deleted and District components but excludes Temp. This query is used to collect the list of district entities that were deleted this frame. -
private EntityQuery m_ServiceDistrictQuery
m_ServiceDistrictQuery matches entities that contain a ServiceDistrict buffer and are not marked Deleted. This query is the target set for the cleanup job: each matched entity's ServiceDistrict buffer is scanned and cleaned of deleted-district references. -
private TypeHandle __TypeHandle
Internal helper struct instance used to store resolved BufferTypeHandle(and to assign it from the SystemState). Used to obtain buffer access inside the scheduled job. -
private struct RemoveServiceDistrictsJob
(nested)
Burst-compiled IJobChunk implementation that: - Reads m_DeletedDistricts (NativeList
) as a readonly list of deleted district entities. - Holds a BufferTypeHandle
for buffer access. -
Executes per chunk: iterates all ServiceDistrict dynamic buffers in the chunk and removes entries whose m_District matches any entity in the m_DeletedDistricts list. This is the core cleanup job and is scheduled in OnUpdate.
-
private struct TypeHandle
(nested)
Contains: BufferTypeHandle<ServiceDistrict> __Game_Areas_ServiceDistrict_RW_BufferTypeHandle
— the resolved buffer handle for ServiceDistrict buffers. Provides __AssignHandles(ref SystemState) to call state.GetBufferTypeHandle() when the system initializes for the compiler-generated pipeline.
Properties
- This system does not expose public properties.
Constructors
public ServiceDistrictSystem()
Default constructor. Marked with [Preserve] on the class methods; constructor itself is empty and used by the ECS/scripting runtime to create the system.
Methods
protected override void OnCreate()
Sets up the two EntityQueries:- m_DeletedDistrictQuery = entities with Deleted & District, excluding Temp.
-
m_ServiceDistrictQuery = entities with ServiceDistrict buffer, excluding Deleted. Calls RequireForUpdate on both queries so the system only updates when relevant entities exist.
-
protected override void OnUpdate()
Main runtime logic: - Calls m_DeletedDistrictQuery.ToEntityListAsync(Allocator.TempJob, out outJobHandle) to produce a NativeList
of deleted districts asynchronously. - Constructs and schedules RemoveServiceDistrictsJob in parallel over m_ServiceDistrictQuery. The job receives the deleted-district list and a BufferTypeHandle
obtained via InternalCompilerInterface.GetBufferTypeHandle using the pre-assigned __TypeHandle. - Disposes the NativeList with deletedDistricts.Dispose(jobHandle) so disposal is deferred until job completion.
-
Assigns the scheduled job to base.Dependency to propagate dependencies.
-
protected override void OnCreateForCompiler()
Compiler helper that: - Calls __AssignQueries(ref base.CheckedStateRef) — currently builds and disposes an EntityQueryBuilder (no-op placeholder for codegen).
-
Calls __TypeHandle.__AssignHandles(ref base.CheckedStateRef) to resolve buffer handles for later use by the job.
-
private void __AssignQueries(ref SystemState state)
A compiler-generated method that currently contains new EntityQueryBuilder(Allocator.Temp).Dispose(); — used by the generated boilerplate to initialize any queries if needed. -
void IJobChunk.Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
(implemented by RemoveServiceDistrictsJob)
Standard IJobChunk explicit interface forwarding to the job's Execute method (keeps Burst and IJobChunk compatibility). -
public void __AssignHandles(ref SystemState state)
(in TypeHandle)
Assigns the BufferTypeHandleusing state.GetBufferTypeHandle (). Marked with AggressiveInlining to minimize overhead.
Implementation notes:
- RemoveServiceDistrictsJob is [BurstCompile], improving performance for the buffer-scan-and-remove operation.
- The job uses a nested triple-loop: over chunks/buffers/entries and a linear search over the deleted districts list. If the deleted list is large, consider using a NativeHashSet
Usage Example
// This system runs automatically as part of the ECS world. Example usage scenario:
//
// - Some District entities get marked with the Deleted component.
// - Other entities carry a dynamic buffer ServiceDistrict which references districts by Entity.
// - ServiceDistrictSystem will automatically remove entries whose m_District points to a deleted district.
//
// Example component definition (simplified):
public struct ServiceDistrict : IBufferElementData
{
public Entity m_District;
}
// No manual invocation is required. Ensure the system is present in the world and that
// entities' Deleted + District components are set when a district is removed.
// The system schedules a burst-compiled job to remove references safely and efficiently.