Game.Zones.UpdateCollectSystem
Assembly: Game
Namespace: Game.Zones
Type: class
Base: GameSystemBase
Summary:
UpdateCollectSystem is an ECS system that collects bounding boxes (Bounds2) of zone Blocks that were created, updated or deleted during the current frame. It schedules Burst-compiled jobs to gather bounds efficiently in parallel, merges bounds where appropriate using a search tree from SearchSystem, and exposes the collected bounds via a persistent NativeList
Fields
-
private EntityQuery m_BlockQuery
Holds the EntityQuery used to find Block entities that are Created, Updated or Deleted (except Temp). This query drives the chunk job that collects bounds. -
private SearchSystem m_SearchSystem
Reference to the global SearchSystem used to look up existing bounds for entities in a spatial search tree (NativeQuadTree). The search tree is used to merge old and new bounds when necessary. -
private NativeList<Bounds2> m_UpdatedBounds
Persistent NativeList that stores the results (the collected Bounds2 instances) produced by the system each frame. This list is returned by GetUpdatedBounds and must be accessed according to the JobHandle returned by that method. -
private JobHandle m_WriteDependencies
JobHandle tracking jobs that write/update the m_UpdatedBounds list (writers). Consumers that want to read the list as read-only should depend on this handle. -
private JobHandle m_ReadDependencies
JobHandle tracking jobs that read from m_UpdatedBounds (readers). Writers are combined with this to ensure correct ordering. -
private TypeHandle __TypeHandle
Internal container holding EntityTypeHandle and ComponentTypeHandleused by job scheduling. Assigned via __AssignHandles during OnCreateForCompiler.
Properties
public bool isUpdated { get; private set; }
Flag indicating whether the system detected any relevant Block changes this frame. Set to true when there are blocks to process; cleared when the query is empty.
Constructors
public UpdateCollectSystem()
Default constructor. The system performs most initialization in OnCreate.
Methods
-
[Preserve] protected override void OnCreate()
Initializes the system: gets or creates the SearchSystem, builds the EntityQuery (Blocks with Created/Updated/Deleted, excluding Temp), and allocates the persistent m_UpdatedBounds list (Allocator.Persistent). -
[Preserve] protected override void OnDestroy()
Completes outstanding job dependencies (m_WriteDependencies and m_ReadDependencies), disposes the persistent NativeList, and performs base cleanup. -
[Preserve] protected override void OnUpdate()
Main scheduling function. If the block query is empty, it completes dependencies, clears m_UpdatedBounds and sets isUpdated = false. Otherwise: - Creates a temporary NativeQueue
. - Prepares and schedules a Burst-compiled IJobChunk (CollectUpdatedBlockBoundsJob) to collect bounds per chunk in parallel. This job uses the SearchSystem's search tree (acquired as read-only) and writes bounds into the queue via a ParallelWriter.
- Schedules a DequeueBoundsJob (IJob) that dequeues the queue into the persistent m_UpdatedBounds NativeList.
- Manages and stores JobHandle dependencies: writer/readers and notifies the SearchSystem of the job reading the tree.
-
Updates base.Dependency and internal m_WriteDependencies/m_ReadDependencies appropriately.
-
public NativeList<Bounds2> GetUpdatedBounds(bool readOnly, out JobHandle dependencies)
Returns the persistent NativeListwith bounds collected for the frame. If readOnly is true, dependencies is the write handle (m_WriteDependencies) which readers must complete before accessing the list. If readOnly is false, dependencies combines write and read dependencies and isUpdated is set to true (signaling the list will be used for writing). Consumers must respect and synchronize using the returned dependencies. -
public void AddBoundsReader(JobHandle handle)
Adds a reader dependency (combines handle into m_ReadDependencies). Use this when a system will read m_UpdatedBounds asynchronously so writers can be ordered correctly. -
public void AddBoundsWriter(JobHandle handle)
Sets the write dependency (m_WriteDependencies = handle) and resets m_ReadDependencies. Use this to register that some job will write to the bounds list. -
[MethodImpl(MethodImplOptions.AggressiveInlining)] private void __AssignQueries(ref SystemState state)
Internal: assigned or prepares entity queries for the compiler variant. (This is a compiler helper and does not populate the query for runtime logic — query is created in OnCreate.) -
protected override void OnCreateForCompiler()
Internal override that calls __AssignQueries and assigns component/entity type handles via __TypeHandle.__AssignHandles.
Nested job types (private, Burst-compiled):
- CollectUpdatedBlockBoundsJob : IJobChunk
Reads entities and Block components and the Created/Deleted markers to collect bounds. For Created chunks it enqueues bounds computed from Block. For Deleted it queries the search tree for previous bounds to enqueue. For normal updates it calculates new bounds and compares them to previous bounds from the search tree; it enqueues merged or individual bounds depending on size checks. Uses NativeQuadTree
-
DequeueBoundsJob : IJob
Dequeues all bounds from the NativeQueue into the persistent NativeList. -
TypeHandle (private struct)
Internal struct to cache EntityTypeHandle and ComponentTypeHandles for Block, Created and Deleted, and to assign them from SystemState.
Usage Example
// Example usage from another system or mod component:
var updateCollect = World.GetOrCreateSystemManaged<Game.Zones.UpdateCollectSystem>();
// Request read-only access to updated bounds
JobHandle deps;
NativeList<Game.Mathematics.Bounds2> updatedBounds = updateCollect.GetUpdatedBounds(readOnly: true, out deps);
// Ensure job completion before accessing the NativeList on the main thread
deps.Complete();
// Iterate bounds on main thread
for (int i = 0; i < updatedBounds.Length; i++)
{
var b = updatedBounds[i];
// handle bounding box (b) — e.g., trigger rebuild or refresh
}
// If you scheduled a job that read the bounds asynchronously, register it as a reader:
JobHandle myReaderJob = /* some scheduled job that reads updatedBounds */;
updateCollect.AddBoundsReader(myReaderJob);
// If you scheduled a job that writes to the bounds list, register it as writer:
JobHandle myWriterJob = /* some scheduled job that writes updatedBounds */;
updateCollect.AddBoundsWriter(myWriterJob);
Notes and tips: - Always honor the JobHandle returned by GetUpdatedBounds before accessing the returned NativeList on the main thread. - Prefer using readOnly=true when only reading the list; writers need to register via AddBoundsWriter so the system can manage ordering. - The system uses Burst jobs and NativeQueue/NativeList; be careful to Dispose any temporaries you create and to complete JobHandles to avoid memory safety issues.