Skip to content

Game.Prefabs.TriggerPrefabSystem

Assembly:
Assembly-CSharp

Namespace:
Game.Prefabs

Type:
class

Base:
GameSystemBase

Summary:
TriggerPrefabSystem is an ECS system responsible for maintaining a runtime index of prefab entities that carry TriggerData. It queries prefab entities that have TriggerData and PrefabData and that are either newly Created or Deleted, and updates an internal TriggerPrefabData container accordingly. The system schedules a chunk job (UpdateTriggerPrefabDataJob) to add or remove prefab entries from the TriggerPrefabData, exposes the container to readers via ReadTriggerPrefabData(out JobHandle), and allows external systems/jobs to register read-dependencies via AddReader(JobHandle). The system allocates TriggerPrefabData with a Persistent allocator and disposes it on destroy.


Fields

  • private TriggerPrefabData m_PrefabData
    Holds the mapping/index of prefab Entity -> TriggerData. Created in OnCreate with Allocator.Persistent and disposed in OnDestroy. Mutated by the scheduled UpdateTriggerPrefabDataJob.

  • private EntityQuery m_PrefabQuery
    EntityQuery used to find prefabs that have a TriggerData buffer and PrefabData and that are either Created or Deleted. Required for the system to run (RequireForUpdate).

  • private JobHandle m_ReadDependencies
    A JobHandle representing combined external reader dependencies added via AddReader. Combined with base.Dependency when scheduling the update job so writers wait for readers.

  • private JobHandle m_WriteDependencies
    A JobHandle representing the last scheduled writer job that updated m_PrefabData. Returned to callers via ReadTriggerPrefabData so readers can wait on the writer.

  • private TypeHandle __TypeHandle
    Container type used to cache EntityTypeHandle, Deleted component handle and TriggerData buffer type handle. Populated by __AssignHandles for use when scheduling the job.

Properties

This type exposes no public properties.

Constructors

  • public TriggerPrefabSystem()
    Default constructor. Marked with [Preserve] attribute to avoid managed stripping; no additional runtime initialization beyond what OnCreate performs.

Methods

  • protected override void OnCreate()
    Creates the m_PrefabQuery to select prefabs with TriggerData and PrefabData and either Created or Deleted tags. Allocates m_PrefabData (TriggerPrefabData) with Allocator.Persistent. Calls RequireForUpdate(m_PrefabQuery) so the system only runs when relevant entities exist.

  • protected override void OnDestroy()
    Disposes m_PrefabData and calls base.OnDestroy(). Ensures the persistent native resources are released.

  • protected override void OnUpdate()
    Schedules the UpdateTriggerPrefabDataJob over m_PrefabQuery. Sets up job input handles using InternalCompilerInterface.Get*TypeHandle on the cached __TypeHandle entries and passes m_PrefabData to the job. Combines current base.Dependency and m_ReadDependencies when scheduling so writers wait for registered readers. After scheduling, clears m_ReadDependencies and stores the returned job into m_WriteDependencies and base.Dependency.

  • public TriggerPrefabData ReadTriggerPrefabData(out JobHandle dependencies)
    Returns the internal TriggerPrefabData and outputs a JobHandle (dependencies) that callers should wait on before reading the container. The returned dependencies are the m_WriteDependencies (the last writer job handle).

  • public void AddReader(JobHandle handle)
    Registers an external reader dependency by combining the provided handle into m_ReadDependencies. This ensures the next writer job will depend on that reader handle (so writers wait for readers to complete).

  • protected override void OnCreateForCompiler()
    Compiler helper called to assign queries and type handles for generated code paths. Calls __AssignQueries and __TypeHandle.__AssignHandles.

  • private void __AssignQueries(ref SystemState state)
    Internal helper (used by generated/OnCreateForCompiler) — currently performs an ephemeral EntityQueryBuilder(Allocator.Temp).Dispose() (placeholder for compiled query assignment).

Nested types / job details:

  • private struct UpdateTriggerPrefabDataJob : IJobChunk
  • Fields:
    • [ReadOnly] public EntityTypeHandle m_EntityType — entity handles for the chunk.
    • [ReadOnly] public ComponentTypeHandle<Deleted> m_DeletedType — read-only handle to test for Deleted tag on chunk.
    • [ReadOnly] public BufferTypeHandle<TriggerData> m_TriggerType — read-only buffer handle to access TriggerData buffer on each entity.
    • public TriggerPrefabData m_TriggerPrefabData — the container to add/remove prefabs from.
  • Behavior:
    • For every chunk processed, retrieves the per-chunk Entity array and the TriggerData buffer accessor.
    • If the chunk Has Deleted tag (chunk.Has(ref m_DeletedType)), iterates entities and for each TriggerData entry calls m_TriggerPrefabData.RemovePrefab(prefab, triggerData).
    • Otherwise, iterates entities and calls m_TriggerPrefabData.AddPrefab(prefab, triggerData) for each TriggerData entry.
    • Implemented Execute(in ArchetypeChunk, int, bool, in v128) and maps to IJobChunk.Execute.

Notes on thread-safety and usage: - TriggerPrefabData is passed to the job and mutated by the scheduled job. The system uses JobHandles (m_ReadDependencies and m_WriteDependencies) to manage synchronization: callers should call ReadTriggerPrefabData to get the container and the returned dependencies JobHandle, and must wait on that JobHandle before reading the container. If a caller will run a job that reads the container, it should call AddReader with the job handle so that the next writer will depend on that reader. - The job uses buffer accessors and chunk-level Deleted checks to efficiently process batches of prefab entities.

Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    // TriggerPrefabData is created by the system with Persistent allocator.
}

// Example consumer which reads the TriggerPrefabData:
public void ConsumeTriggerData(TriggerPrefabSystem triggerSystem)
{
    JobHandle deps;
    TriggerPrefabData data = triggerSystem.ReadTriggerPrefabData(out deps);
    // Ensure completion before accessing data on the main thread:
    deps.Complete();

    // Read from data...
}

// Example of registering a job as a reader:
public void ScheduleReaderJob(TriggerPrefabSystem triggerSystem, JobHandle myReadJobHandle)
{
    // Tell the system that a reader job exists so the next writer will wait for it:
    triggerSystem.AddReader(myReadJobHandle);
}