Skip to content

Game.Common.PrepareCleanUpSystem

Assembly:
Assembly-CSharp (runtime game assembly; Cities: Skylines 2 modding)

Namespace:
Game.Common

Type:
class

Base:
GameSystemBase

Summary:
PrepareCleanUpSystem is a world/system-level helper that collects lists of entities that are marked as deleted or otherwise changed during the frame and forwards those lists to the CleanUpSystem. It builds two EntityQuery instances at creation: one to capture Deleted and Event-tagged entities, and another to capture newly Created/Updated/Applied/EffectsUpdated/BatchesUpdated/PathfindUpdated entities (excluding Deleted). During OnUpdate it converts those queries to NativeList asynchronously, hands those lists to the CleanUpSystem via AddDeleted/AddUpdated, and combines the produced job handles into the system dependency to ensure correct job ordering.


Fields

  • private CleanUpSystem m_CleanUpSystem
    Holds a reference to the CleanUpSystem instance retrieved from the same World. Used to pass the collected NativeList and their producer JobHandles for further cleanup processing.

  • private EntityQuery m_DeletedQuery
    EntityQuery configured with Any = { Deleted (read-only), Event (read-only) }. Used to find entities that should be treated as deleted or event-driven deletions.

  • private EntityQuery m_UpdatedQuery
    EntityQuery configured with Any = { Created, Updated, Applied, EffectsUpdated, BatchesUpdated, PathfindUpdated } and None = { Deleted }. Used to find entities that were created/updated/applied/effected/etc. this frame but aren't deleted.

Properties

  • This class does not declare any public properties.

Constructors

  • public PrepareCleanUpSystem()
    Default constructor. Marked with [Preserve] on the class methods but constructor itself is parameterless and does no special initialization; actual setup happens in OnCreate.

Methods

  • protected override void OnCreate()
    Called when the system is created. Retrieves the CleanUpSystem from the World and constructs two EntityQuery instances:
  • m_DeletedQuery: Any = Deleted or Event
  • m_UpdatedQuery: Any = Created, Updated, Applied, EffectsUpdated, BatchesUpdated, PathfindUpdated; None = Deleted
    The [Preserve] attribute is present on this method in the source to avoid stripping by managed code stripping tools.

  • protected override void OnUpdate()
    Executed each frame. Behavior:

  • Calls ToEntityListAsync(Allocator.TempJob, out JobHandle) on m_DeletedQuery and m_UpdatedQuery to obtain NativeList instances and their producer JobHandles.
  • Calls m_CleanUpSystem.AddDeleted(deletedEntities, outJobHandle) and m_CleanUpSystem.AddUpdated(updatedEntities, outJobHandle2) to hand off the lists and their job dependencies to the CleanUpSystem for processing.
  • Combines the two JobHandles via JobHandle.CombineDependencies and assigns the result to base.Dependency to ensure proper dependency chaining. Notes and cautions:
  • The code uses Allocator.TempJob for the NativeList allocations; ownership and disposal responsibility is expected to be handled by CleanUpSystem.AddDeleted/AddUpdated. When implementing similar systems, ensure that the receiving system properly disposes the NativeList after the associated jobs complete.
  • The method uses asynchronous conversion to entity lists to avoid blocking the main thread.

Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    // PrepareCleanUpSystem constructs its queries and stores a reference to CleanUpSystem here.
}

[Preserve]
protected override void OnUpdate()
{
    // This is analogous to the implementation in PrepareCleanUpSystem:
    JobHandle deletedHandle;
    NativeList<Entity> deletedEntities = m_DeletedQuery.ToEntityListAsync(Allocator.TempJob, out deletedHandle);

    JobHandle updatedHandle;
    NativeList<Entity> updatedEntities = m_UpdatedQuery.ToEntityListAsync(Allocator.TempJob, out updatedHandle);

    // Forward lists and their producer handles to the CleanUpSystem for processing.
    m_CleanUpSystem.AddDeleted(deletedEntities, deletedHandle);
    m_CleanUpSystem.AddUpdated(updatedEntities, updatedHandle);

    // Combine dependencies so other systems that depend on this system will wait.
    base.Dependency = JobHandle.CombineDependencies(deletedHandle, updatedHandle);
}

Additional notes for modders: - This system is intended to be run within Unity DOTS/ECS environment used by Cities: Skylines 2. When creating similar systems, follow the pattern of producing NativeList with ToEntityListAsync and passing the corresponding JobHandle to whatever system will consume/own the list. - Respect allocator choice (Allocator.TempJob) and ensure proper disposal on job completion to avoid leaking memory. - The class uses [Preserve] attributes on lifecycle methods to avoid managed code stripping; keep them if your mod uses code-stripping or AOT builds.