Game.Areas.CurrentDistrictSystem
Assembly: Assembly-CSharp.dll
Namespace: Game.Areas
Type: class
Base: GameSystemBase
Summary: CurrentDistrictSystem is a Unity ECS game system used by Cities: Skylines 2 to keep CurrentDistrict and BorderDistrict components up-to-date for world entities (objects and net elements). It listens to district updates reported by the UpdateCollectSystem and/or to entities that have the Updated tag and either CurrentDistrict or BorderDistrict components. The system uses spatial quad-tree search structures (area, object and net search trees) to find affected entities and schedules a set of Burst-compiled jobs to compute and write the correct district entities. When border/district changes may affect pathfinding (sub-lanes like car/pedestrian/parking), the system marks sub-lanes with a PathfindUpdated component via the ModificationBarrier's command buffer.
Fields
-
private UpdateCollectSystem m_UpdateCollectSystem
Used to query whether districts have been updated and to get the list (bounds) of updated districts. This system triggers the parallel search when district bounds change. -
private SearchSystem m_AreaSearchSystem
Reference to the Area SearchSystem used to look up which district (area) contains a point by iterating area search tree items. -
private Game.Objects.SearchSystem m_ObjectSearchSystem
Reference to the object search tree for locating object entities that might need district updates. -
private Game.Net.SearchSystem m_NetSearchSystem
Reference to the net search tree for locating net edge entities (borders) that might need district updates. -
private ModificationBarrier5 m_ModificationBarrier
Barrier used to produce entity command buffers when scheduling parallel jobs that will add components (e.g., PathfindUpdated) on sub-lane entities. Ensures safe structural changes. -
private EntityQuery m_CurrentDistrictQuery
EntityQuery used to find chunks that have the Updated tag and either CurrentDistrict or BorderDistrict components. Used to run a chunk job that refreshes districts for those entities. -
private TypeHandle __TypeHandle
Internal struct holding component lookups and component type handles used across jobs and chunk processing. It is initialized in OnCreate/OnCreateForCompiler via __AssignHandles.
Properties
- None (no public properties on this system).
Internally the system uses component lookups and type handles stored in the TypeHandle field.
Constructors
public CurrentDistrictSystem()
Default constructor. The system initializes and will set up its dependencies and queries in OnCreate. No custom construction logic beyond base initialization.
Methods
-
protected override void OnCreate()
: System.Void
Initializes references to dependent systems (UpdateCollectSystem, area/object/net SearchSystems, ModificationBarrier5) and builds the EntityQuery (m_CurrentDistrictQuery) that selects Updated entities that have CurrentDistrict or BorderDistrict components. Also called by Unity/ECS when the system is created. -
protected override void OnUpdate()
: System.Void
Main update logic. Two primary flows: - If UpdateCollectSystem reports districtsUpdated: gather updated district bounds and perform:
- FindUpdatedDistrictItemsJob (IJobParallelForDefer) — iterates object and net quad-trees to collect entities inside updated bounds that have CurrentDistrict/BorderDistrict and enqueues them.
- CollectUpdatedDistrictItemsJob (IJob) — drains and sorts the native queue into a deduplicated NativeList
. - FindDistrictParallelJob (IJobParallelForDefer) — for each entity in the update list, uses the area search tree to determine the new CurrentDistrict or BorderDistrict values, writes them back, and if needed marks sub-lanes with PathfindUpdated via the command buffer.
- Sets up dependencies and readers on the various search systems and modification barrier so jobs run safely and structural changes are applied after jobs complete.
- Independently, if there are any entities matched by m_CurrentDistrictQuery, the system schedules FindDistrictChunkJob (IJobChunk) to refresh CurrentDistrict/BorderDistrict for all matching chunks using the area search tree (non-incremental refresh for all entities in chunks).
The method carefully composes JobHandles so the search trees and update-collect data are read-safely while jobs run in parallel, disposing native containers at the appropriate job completions.
-
protected override void OnCreateForCompiler()
: System.Void
Helper called for compiler-generated initialization. Calls __AssignQueries and __TypeHandle.__AssignHandles to initialize ECS query & handle state used by jobs. -
private void __AssignQueries(ref SystemState state)
: System.Void
(Basic stub in this generated code) Prepares and assigns any EntityQuery objects required by the system. In this decompiled/generator output it creates an EntityQueryBuilder and disposes it; real queries for runtime are created in OnCreate. -
Nested job types and iterators (short summaries):
-
FindUpdatedDistrictItemsJob : IJobParallelForDefer
Iterates a deferred array of Bounds2 (updated district bounds). Uses two quad-tree iterators (ObjectIterator and NetIterator) to find object and net entities intersecting each bound and enqueues matching entities that currently have CurrentDistrict or BorderDistrict components into a parallel NativeQueue.- ObjectIterator / NetIterator: per-tree iterators that check AABB intersection and HasComponent on entities before enqueuing.
-
CollectUpdatedDistrictItemsJob : IJob
Drains the NativeQueue into a NativeList, sorts entity indices, and removes duplicates so the subsequent processing receives a compact, ordered list of unique entities to update. -
FindDistrictParallelJob : IJobParallelForDefer
For each entity in the update list, determines the containing district (for CurrentDistrict) or left/right districts for BorderDistrict using the AreaSearchTree. If a district change is detected it writes the new component values. If an entity's sub-lanes include lanes that require pathfinding updates, it adds a PathfindUpdated component to those sub-lane entities via the command buffer (unless the entity already has Updated or no sub-lane buffer).Important component lookups and buffer lookups are provided (transforms, edge geometry, district lookup, sub-lanes, triangles/nodes for point-in-triangle tests, lane types).
-
FindDistrictChunkJob : IJobChunk
Runs on chunks selected by m_CurrentDistrictQuery to refresh CurrentDistrict and BorderDistrict components for all entities in the chunk using the area tree. This is the non-incremental path that ensures every matching entity gets a current district assignment (used when entities are flagged Updated). -
DistrictIterator : INativeQuadTreeIterator<AreaSearchItem, QuadTreeBoundsXZ>
Helper used by area-tree iterations to find which area/district a position falls into. It performs point-in-triangle tests against triangles stored on the area entity to identify the correct area entity result. -
TypeHandle
Auto-generated container for ComponentLookup, BufferLookup and ComponentTypeHandle instances. __AssignHandles fills these from a SystemState so jobs and chunk code can use them safely.
Usage Example
// CurrentDistrictSystem runs automatically in the world and updates components.
// As a modder you typically read CurrentDistrict or BorderDistrict components from entities.
// Example: reading CurrentDistrict for an entity:
Entity entity = /* some entity you know */;
if (EntityManager.HasComponent<CurrentDistrict>(entity))
{
CurrentDistrict cur = EntityManager.GetComponentData<CurrentDistrict>(entity);
Entity districtEntity = cur.m_District;
// Use districtEntity (may be Entity.Null if not inside any district)
}
// If you need to trigger a manual refresh you can add Updated to an entity:
// (The system will pick up entities with Updated + CurrentDistrict/BorderDistrict)
EntityManager.AddComponent<Updated>(entity);
Notes and tips: - CurrentDistrictSystem depends on the game's area/object/net search trees. If you modify those trees or the district geometry, ensure you update the corresponding search system readers or synchronize with the UpdateCollectSystem. - Avoid performing structural changes that conflict with the system's scheduled command buffers. Use the provided ModificationBarrier when issuing parallel structural changes in jobs.