Skip to content

Game.Tools.OriginalDeletedSystem

Assembly: Game
Namespace: Game.Tools

Type: class

Base: GameSystemBase

Summary:
OriginalDeletedSystem is a Unity ECS system that scans entities with the Temp component (excluding those already marked Deleted) to determine whether the "original" entity referenced by Temp has been deleted or no longer exists. It uses a burst-compiled IJobChunk (OriginalDeletedJob) to perform the checks in parallel and records results into a 2-element NativeArray (m_OriginalDeleted). Index 1 denotes the result discovered this frame (or when the job ran), and index 0 holds the previous-frame result (the system shifts values in OnUpdate). GetOriginalDeletedResult(int delay) completes the job dependency and inspects the two-slot buffer to answer whether the original entity was deleted within the requested delay window.


Fields

  • private EntityQuery m_TempQuery
    This query selects entities that have the Temp component and do not have Deleted. The job is scheduled only when this query is not empty.

  • private NativeArray<bool> m_OriginalDeleted
    A persistent NativeArray of length 2 used as a small ring/buffer for deletion results. m_OriginalDeleted[1] is used for the current detection pass, while m_OriginalDeleted[0] stores the previous frame's value (shifted in OnUpdate).

  • private JobHandle m_Dependency
    Holds the handle to the scheduled job so callers (GetOriginalDeletedResult) can Complete() it and read results safely.

  • private TypeHandle __TypeHandle
    Compiler-generated struct storing ComponentTypeHandle/ComponentLookup/EntityStorageInfoLookup instances used to build the job's inputs. Assigned in OnCreateForCompiler.

Properties

  • None (no public properties declared on this class)

Constructors

  • public OriginalDeletedSystem()
    Default parameterless constructor. The system uses Unity ECS lifecycle methods (OnCreate, OnUpdate, OnDestroy) for initialization, scheduling, and cleanup.

Methods

  • protected override void OnCreate()
    Initializes the system: builds m_TempQuery to target Temp components and exclude Deleted, and allocates m_OriginalDeleted as a Persistent NativeArray with two elements.

  • protected override void OnDestroy()
    Disposes the m_OriginalDeleted NativeArray and calls the base OnDestroy. Ensures persistent native memory is released.

  • public bool GetOriginalDeletedResult(int delay)
    Completes the scheduled job (m_Dependency.Complete()) and inspects m_OriginalDeleted. The delay parameter controls which entries to check:

  • delay == 0: checks both current (index 1) and previous (index 0) results (returns true if either is true).
  • delay == 1: checks only the previous result (index 0). The method returns true if any inspected slot indicates the original entity was deleted or missing.

  • protected override void OnUpdate()
    Prepares the two-slot buffer by shifting current -> previous (m_OriginalDeleted[0] = m_OriginalDeleted[1]) and clearing current (m_OriginalDeleted[1] = false). If m_TempQuery is not empty it creates and schedules the burst-compiled OriginalDeletedJob via JobChunkExtensions.ScheduleParallel, storing the returned JobHandle in base.Dependency and m_Dependency.

  • protected override void OnCreateForCompiler()
    Compiler helper that assigns query and type handles via __AssignQueries and __TypeHandle.__AssignHandles; called during system creation for the static/compiled path.

  • private void __AssignQueries(ref SystemState state)
    Compiler-generated placeholder creating (and disposing) an EntityQueryBuilder; present due to code generation patterns.

  • (Nested) private struct OriginalDeletedJob : IJobChunk
    Burst-compiled job that runs per chunk. Fields:

  • [ReadOnly] public ComponentTypeHandle<Temp> m_TempType — to read Temp components.
  • [ReadOnly] public ComponentLookup<Deleted> m_DeletedData — to check whether a referenced entity currently has a Deleted component.
  • [ReadOnly] public EntityStorageInfoLookup m_EntityLookup — to check whether an entity still exists in storage.
  • [NativeDisableParallelForRestriction] public NativeArray<bool> m_OriginalDeleted — shared two-slot result array (written from the job).

Execute(in ArchetypeChunk chunk, ...) iterates Temp components in the chunk; for each Temp with temp.m_Original != Entity.Null: - If m_DeletedData.HasComponent(temp.m_Original) then sets m_OriginalDeleted[1] = true (current frame deleted marker). - Else if !m_EntityLookup.Exists(temp.m_Original) then sets m_OriginalDeleted[0] = true (missing / removed earlier marker).

Note: the job writes to the shared NativeArray without an atomic operation; the array is marked with NativeDisableParallelForRestriction because the job only ever writes the constant indices 0 or 1 and only sets them to true — races are benign because multiple writers setting true produce the same logical result.

  • (Nested) private struct TypeHandle
    Container for compile-time type handles:
  • ComponentTypeHandle<Temp> __Game_Tools_Temp_RO_ComponentTypeHandle
  • ComponentLookup<Deleted> __Game_Common_Deleted_RO_ComponentLookup
  • EntityStorageInfoLookup __EntityStorageInfoLookup
  • public void __AssignHandles(ref SystemState state) — assigns the handles from the SystemState.

Usage Example

// Example: check whether any "original" entity referenced by Temp components was deleted.
// This could be called from another system or script that has access to the World.
var world = World.DefaultGameObjectInjectionWorld; // or your target World
if (world != null)
{
    var originalDeletedSystem = world.GetExistingSystemManaged<Game.Tools.OriginalDeletedSystem>();
    if (originalDeletedSystem != null)
    {
        // Wait 0 frames (check current and previous detection)
        bool wasDeleted = originalDeletedSystem.GetOriginalDeletedResult(0);
        if (wasDeleted)
        {
            // handle deleted original(s)
        }
    }
}

Notes and caveats: - The system uses a persistent NativeArray of length 2 to store results across frames; callers must be aware that results are updated asynchronously, so GetOriginalDeletedResult completes the dependency before reading. - The job relies on checking both a Deleted component and low-level EntityStorageInfoLookup.Exists to distinguish between entities that were explicitly marked Deleted (HasComponent) and entities that no longer exist in storage (removed earlier). - The job writes to a small shared array from parallel jobs; this is safe in this implementation because writes are idempotent (only ever setting true) and the array is marked with NativeDisableParallelForRestriction.