Skip to content

Game.Areas.UpdateCollectSystem

Assembly: Game (game assembly containing runtime systems)
Namespace: Game.Areas

Type: class

Base: GameSystemBase

Summary:
UpdateCollectSystem is an ECS system responsible for collecting updated 2D bounds (Bounds2) for area entities in the game (lots, districts, map tiles and spaces). It queries area entities that have Nodes and Triangles and that were Created, Updated or Deleted, then schedules burst-compiled jobs to compute and aggregate updated triangle bounds. The system uses a SearchSystem's quad-tree and triangle-count map to reconcile previous bounds with newly computed triangle bounds, de-duplicate / merge bounds intelligently, and expose NativeList outputs per area category. It manages job read/write dependencies so other systems can register as readers and synchronize with scheduled jobs.


Fields

  • private SearchSystem m_SearchSystem
    UpdateCollectSystem's reference to the SearchSystem used to obtain the search quad-tree and triangle counts needed while collecting updated bounds. This system is used as a read-only data source when scheduling the collect jobs.

  • private UpdateBufferData m_LotData
    Holds per-category data (NativeList, EntityQuery, JobHandle dependencies and updated flag) for lots. Use this to retrieve updated lot bounds and to add reader dependencies.

  • private UpdateBufferData m_DistrictData
    Same as m_LotData but for districts.

  • private UpdateBufferData m_MapTileData
    Same as m_LotData but for map tiles.

  • private UpdateBufferData m_SpaceData
    Same as m_LotData but for spaces.

  • private TypeHandle __TypeHandle
    Stores precomputed type handles (EntityTypeHandle, BufferTypeHandles and ComponentTypeHandles) used by the jobs. These are assigned during OnCreateForCompiler and used to get safe type handles from the SystemState for scheduling jobs.

  • private struct UpdateBufferData (nested)

  • public NativeList<Bounds2> m_Bounds
    NativeList that stores the collected Bounds2 results for this category. Allocated with Allocator.Persistent in Create and disposed in Dispose.
  • public EntityQuery m_Query
    The entity query used to find relevant area entities for the category.
  • public JobHandle m_WriteDependencies
    JobHandle that represents the write-side dependencies (scheduled jobs producing m_Bounds). Callers should wait on this before reading m_Bounds.
  • public JobHandle m_ReadDependencies
    JobHandle that represents read-side dependencies requested by external readers. External systems can combine into this to ensure they signal read access before the system writes again.
  • public bool m_IsUpdated
    Flag indicating whether this buffer has been updated (i.e., UpdateBounds was called since last clear).
  • Methods:

    • Create(EntityQuery query) — Allocates m_Bounds and stores query.
    • Dispose() — Disposes m_Bounds.
    • Clear() — Completes write/read dependencies, clears m_Bounds and resets m_IsUpdated.
  • private struct CollectUpdatedAreaBoundsJob (nested, [BurstCompile])
    JobChunk that iterates area entity chunks and enqueues triangle bounds (Bounds2) into a NativeQueue. It handles Created/Deleted/Updated/normal cases:

  • For Created chunks, computes bounds for every triangle and enqueues them.
  • For Deleted chunks, uses m_TriangleCount and the search quad-tree to enqueue old bounds that must be removed.
  • For normal/updated entities, compares existing tree bounds and new triangle bounds to decide whether to merge bounds or enqueue individually (attempts to minimize redundant bounds). This job reads Entity, Node buffer, Triangle buffer, Created/Deleted components, and uses the provided read-only quad-tree and triangle-count map.

  • private struct DequeueBoundsJob (nested, [BurstCompile])
    IJob that dequeues all Bounds2 values from the NativeQueue and writes them into the target NativeList (data.m_Bounds). This transfers results produced by the parallel CollectUpdatedAreaBoundsJob into a contiguous list for later readers.


Properties

  • public bool lotsUpdated => m_LotData.m_IsUpdated
    True if the system has collected updated lot bounds in this update cycle (i.e., UpdateBounds was scheduled for lots).

  • public bool districtsUpdated => m_DistrictData.m_IsUpdated
    True if the system has collected updated district bounds.

  • public bool mapTilesUpdated => m_MapTileData.m_IsUpdated
    True if the system has collected updated map tile bounds.

  • public bool spacesUpdated => m_SpaceData.m_IsUpdated
    True if the system has collected updated space bounds.

Each property is a quick check to know whether the corresponding NativeList contains new data since the last Clear().


Constructors

  • public UpdateCollectSystem()
    Default constructor. The system sets up type handles and queries in OnCreate / OnCreateForCompiler and allocates per-category buffers in OnCreate.

Methods

  • protected override void OnCreate()
    Initializes the system: gets (or creates) the SearchSystem, and calls Create on each UpdateBufferData with a query for Lot, District, MapTile and Space respectively. The queries look for Area + Node + Triangle + specific category component, and any of Created/Updated/Deleted (excluding Temp). This sets up the persistent NativeList buffers.

  • protected override void OnDestroy()
    Disposes the per-category UpdateBufferData (which in turn disposes their NativeList and releases resources).

  • protected override void OnStopRunning()
    Called when the system stops running; calls Clear() on all UpdateBufferData which completes dependencies and clears the lists.

  • protected override void OnUpdate()
    Called each frame; for each category (lots, districts, map tiles, spaces) it either clears the data if the query is empty, or schedules UpdateBounds(ref data, dependency) and combines the returned JobHandle into the system's Dependency. This drives the job scheduling that gathers updated bounds.

  • private JobHandle UpdateBounds(ref UpdateBufferData data, JobHandle inputDeps)
    Core helper that:

  • Marks data.m_IsUpdated = true.
  • Creates a temporary NativeQueue (Allocator.TempJob).
  • Acquires the search quad-tree and triangle-count map from m_SearchSystem (read-only) and obtains the reader JobHandle dependencies.
  • Constructs and schedules CollectUpdatedAreaBoundsJob in parallel over data.m_Query (JobChunk schedule).
  • Schedules DequeueBoundsJob that drains the queue into data.m_Bounds.
  • Disposes the queue after the dequeue job and records write/read dependencies (data.m_WriteDependencies is set to the final job that writes m_Bounds).
  • Calls m_SearchSystem.AddSearchTreeReader(jobHandle) to register the job as a reader of the search tree.
  • Returns the JobHandle corresponding to completion of the Dequeue job (the job that produces the NativeList).

Important: callers reading the returned NativeList should wait on the out JobHandle (or use GetUpdated* which returns the JobHandle to wait on).

  • public NativeList<Bounds2> GetUpdatedLotBounds(out JobHandle dependencies)
    Returns the NativeList containing collected lot bounds and outputs the JobHandle (m_LotData.m_WriteDependencies) that must be completed before reading the list.

  • public NativeList<Bounds2> GetUpdatedDistrictBounds(out JobHandle dependencies)
    Same as above for districts.

  • public NativeList<Bounds2> GetUpdatedMapTileBounds(out JobHandle dependencies)
    Same as above for map tiles.

  • public NativeList<Bounds2> GetUpdatedSpaceBounds(out JobHandle dependencies)
    Same as above for spaces.

  • public void AddLotBoundsReader(JobHandle handle)
    Combine the provided handle into m_LotData.m_ReadDependencies so the system respects external read dependencies before overwriting/clearing results.

  • public void AddDistrictBoundsReader(JobHandle handle)
    Combine the provided handle into m_DistrictData.m_ReadDependencies.

  • public void AddMapTileBoundsReader(JobHandle handle)
    Combine the provided handle into m_MapTileData.m_ReadDependencies.

  • public void AddSpaceBoundsReader(JobHandle handle)
    Combine the provided handle into m_SpaceData.m_ReadDependencies.

  • private EntityQuery GetQuery<T>()
    Helper that constructs the EntityQuery used for a category T (Lot/District/MapTile/Space). Query requires: Area, Node (buffer), Triangle (buffer), and T; Any of Created/Updated/Deleted; None Temp. Used by OnCreate to create each category query.

  • private void __AssignQueries(ref SystemState state)
    Compiler helper used by OnCreateForCompiler (no runtime behavior beyond the generated stub in this decompiled view).

  • protected override void OnCreateForCompiler()
    Compiler-time helper invoked by generated code to assign queries and type handles. It calls __AssignQueries and assigns type handles via __TypeHandle.__AssignHandles.

  • private struct TypeHandle.__AssignHandles(ref SystemState state)
    Method that obtains all needed type handles from SystemState (EntityTypeHandle, BufferTypeHandles for Node/Triangle, ComponentTypeHandles for Created/Deleted). Called from OnCreateForCompiler.

Notes about inner jobs: - CollectUpdatedAreaBoundsJob is Burst-compiled and uses job chunk iteration to operate efficiently in parallel over entity chunks. It uses the search quad-tree (NativeQuadTree) and the NativeParallelHashMap containing triangle counts. - DequeueBoundsJob copies the queue out to the persistent list on a single thread (IJob) after the parallel job completes.


Usage Example

// Example usage in another system or MonoBehaviour that wants to consume updated lot bounds:

// Acquire the system (managed system instance)
var updateCollect = World.DefaultGameObjectInjectionWorld.GetExistingSystemManaged<Game.Areas.UpdateCollectSystem>();

// After World.Systems have been updated (or scheduled), retrieve the list and its dependency:
JobHandle deps;
NativeList<Bounds2> lotBounds = updateCollect.GetUpdatedLotBounds(out deps);

// Option A: Wait for the producer to complete, then read the list synchronously:
deps.Complete();
for (int i = 0; i < lotBounds.Length; i++)
{
    Bounds2 b = lotBounds[i];
    // use b...
}

// Option B: If you schedule a job that reads the list, register that job as a reader:
// myReadJob.jobInputList = lotBounds;
JobHandle myReadJobHandle = /* schedule job that reads lotBounds */;
updateCollect.AddLotBoundsReader(myReadJobHandle);

// Be sure to respect the lifetime of the NativeList (UpdateCollectSystem owns and disposes it).

Notes and tips: - Always wait on the returned JobHandle (from GetUpdatedBounds) before accessing the NativeList contents on the main thread, or register your job via AddBoundsReader to participate in the system's dependency tracking. - The system attempts to minimize redundant bounds by merging new triangle bounds with existing quad-tree entries when appropriate, but callers should still expect multiple entries and possibly overlapping bounds. - The NativeList returned is owned by the system; do not dispose it. If you need to persist bounds beyond the system's lifetime, copy them into your own container.