Skip to content

Game.CleanUpSystem

Assembly:
Assembly-CSharp (game's runtime assembly; actual assembly name may vary)

Namespace:
Game.Common

Type:
class

Base:
GameSystemBase

Summary:
This system centralizes cleanup work for entities that have been marked for deletion or that had transient "updated" marker components. It accepts NativeList instances and corresponding JobHandle dependencies from other systems (via AddDeleted and AddUpdated), completes those job handles in OnUpdate, and then: - destroys the deleted entities, and - removes a set of marker components from updated entities. It also disposes the NativeLists that were passed in. Methods and constructor are annotated with [Preserve] to avoid stripping.


Fields

  • private NativeList<Entity> m_DeletedEntities
    Holds the list of entities that should be destroyed when the system runs. The system takes ownership of this NativeList and will Dispose() it during OnUpdate. Do not dispose the same list elsewhere once handed to AddDeleted.

  • private NativeList<Entity> m_UpdatedEntities
    Holds the list of entities from which marker components will be removed. The system takes ownership of this NativeList and will Dispose() it during OnUpdate. Do not dispose the same list elsewhere once handed to AddUpdated.

  • private JobHandle m_DeletedDeps
    JobHandle representing dependencies for jobs that produced/populated m_DeletedEntities. OnUpdate calls Complete() on this handle before destroying the entities.

  • private JobHandle m_UpdatedDeps
    JobHandle representing dependencies for jobs that produced/populated m_UpdatedEntities. OnUpdate calls Complete() on this handle before removing components.

  • private ComponentTypeSet m_UpdateTypes
    Cached ComponentTypeSet initialized in OnCreate that lists the marker components to remove from updated entities: Created, Updated, Applied, EffectsUpdated, BatchesUpdated, and PathfindUpdated (all ReadWrite). This avoids allocating the set repeatedly each frame.

Properties

  • None.
    This system exposes no public properties; interaction is via AddDeleted and AddUpdated methods.

Constructors

  • public CleanUpSystem()
    Default constructor annotated with [Preserve]. No special initialization is performed here; primary setup occurs in OnCreate.

Methods

  • protected override void OnCreate() : System.Void
    Initializes m_UpdateTypes with a ComponentTypeSet containing the marker components that should be removed from updated entities. This avoids repeated allocations during updates. Method is annotated with [Preserve].

  • public void AddDeleted(NativeList<Entity> deletedEntities, JobHandle deletedDeps) : System.Void
    Accepts ownership of a NativeList that enumerates entities to destroy and a JobHandle for dependencies. The system stores the list and handle and will Complete() the handle and destroy the entities in its next OnUpdate. The caller must not dispose the provided NativeList after calling this method.

  • public void AddUpdated(NativeList<Entity> updatedEntities, JobHandle updatedDeps) : System.Void
    Accepts ownership of a NativeList that enumerates entities from which marker components will be removed and a JobHandle for dependencies. The system stores the list and handle and will Complete() the handle and remove components in its next OnUpdate. The caller must not dispose the provided NativeList after calling this method.

  • protected override void OnUpdate() : System.Void
    Completes both stored JobHandles (m_DeletedDeps and m_UpdatedDeps), then:

  • Destroys all entities in m_DeletedEntities via EntityManager.DestroyEntity(m_DeletedEntities.AsArray()).
  • Removes the component types in m_UpdateTypes from all entities in m_UpdatedEntities via EntityManager.RemoveComponent(m_UpdatedEntities.AsArray(), in m_UpdateTypes). Finally it disposes both NativeLists. This method relies on AddDeleted/AddUpdated having been called prior to running; if they were not set, null/uninitialized lists/handles may cause errors.

Usage Example

// Example producer system that schedules jobs and passes work to CleanUpSystem.
// Assumes a reference to the CleanUpSystem instance is available.

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    // CleanUpSystem will initialize its ComponentTypeSet in its OnCreate.
}

// Somewhere in a producer system's update:
void ProduceAndRegisterCleanup()
{
    // Build NativeList<Entity> deletedEntities and updatedEntities in a job or main thread.
    var deletedEntities = new NativeList<Entity>(Allocator.TempJob);
    var updatedEntities = new NativeList<Entity>(Allocator.TempJob);

    // (Populate lists, possibly via jobs. Assume jobs return JobHandle jobHandleDeleted / jobHandleUpdated)

    // Example: register with the global cleanup system so it completes dependencies and performs destruction/removal.
    var cleanupSystem = World.DefaultGameObjectInjectionWorld.GetExistingSystem<Game.Common.CleanUpSystem>();
    cleanupSystem.AddDeleted(deletedEntities, jobHandleDeleted);
    cleanupSystem.AddUpdated(updatedEntities, jobHandleUpdated);

    // Do NOT dispose deletedEntities/updatedEntities here — CleanUpSystem takes ownership and will Dispose() them.
}

Additional notes and cautions: - CleanUpSystem completes the provided job handles in OnUpdate; the caller must not Complete or otherwise reuse those handles expecting the lists to remain valid after handing them to this system. - The system takes ownership of the provided NativeLists and disposes them; double-dispose will cause crashes. - If multiple producers call AddDeleted/AddUpdated before the system's OnUpdate, the last call overwrites the stored lists/handles — this class is designed for a single producer-per-frame pattern or coordination to aggregate lists before registration. - The [Preserve] attributes prevent the runtime code stripping from removing the methods/constructor in builds that use managed code stripping.