Game.Simulation.SpectatorSiteSystem
Assembly: Game
Namespace: Game.Simulation
Type: SpectatorSiteSystem
Base: GameSystemBase
Summary:
SpectatorSiteSystem is a simulation system that manages SpectatorSite components attached to entities. It runs every 64 simulation frames and schedules a parallel IJobChunk (SpectatorSiteJob) that inspects each SpectatorSite entry and removes the SpectatorSite component once the associated event has finished (taking into account the event's Duration and the Prefab's SpectatorEventData termination duration). The job uses an EndFrameBarrier's EntityCommandBuffer.ParallelWriter to perform removals safely from jobs, and it reads Duration, PrefabRef and SpectatorEventData component data to compute termination frames. The system depends on the SimulationSystem for the current simulation frame index and registers its producer job with the EndFrameBarrier.
Fields
-
private const uint UPDATE_INTERVAL = 64u
This constant defines the system update interval in frames. GetUpdateInterval returns 64, so the system executes once every 64 simulation frames. -
private SimulationSystem m_SimulationSystem
Reference to the SimulationSystem used to obtain the current simulation frame index (frameIndex). Populated in OnCreate via World.GetOrCreateSystemManaged(). -
private EndFrameBarrier m_EndFrameBarrier
Reference to an EndFrameBarrier system used to create an EntityCommandBuffer for deferred structural changes. The job's EntityCommandBuffer.ParallelWriter is obtained from this barrier. The system also calls AddJobHandleForProducer to register the dependency. -
private EntityQuery m_SiteQuery
Entity query selecting entities that have SpectatorSite component and exclude Deleted and Temp. The system calls RequireForUpdate(m_SiteQuery) so it only updates when there are matching entities. -
private TypeHandle __TypeHandle
Internal struct instance used to cache EntityTypeHandle, ComponentTypeHandleand ComponentLookup handles (Duration, PrefabRef, SpectatorEventData). Handles are assigned during OnCreateForCompiler.
Properties
- (none)
Constructors
public SpectatorSiteSystem()
Default constructor. The system uses the standard GameSystemBase construction pattern; initialization is done in OnCreate / OnCreateForCompiler.
Methods
-
public override int GetUpdateInterval(SystemUpdatePhase phase)
Returns the update interval in frames (64). This controls how often the system's OnUpdate runs relative to the simulation frame cadence. -
[Preserve] protected override void OnCreate()
Initializes references and queries: - Acquires SimulationSystem and EndFrameBarrier from the world.
- Creates m_SiteQuery to target entities with SpectatorSite and exclude Deleted/Temp.
-
Calls RequireForUpdate(m_SiteQuery) to ensure the system only runs if such entities exist.
-
[Preserve] protected override void OnUpdate()
Creates and schedules SpectatorSiteJob (IJobChunk) with: - m_SimulationFrame set from m_SimulationSystem.frameIndex.
- m_CommandBuffer from m_EndFrameBarrier.CreateCommandBuffer().AsParallelWriter().
- EntityTypeHandle / ComponentTypeHandle / ComponentLookup handles extracted via InternalCompilerInterface and cached __TypeHandle.
- Schedules the job as a parallel JobChunk and updates base.Dependency.
- Registers base.Dependency with m_EndFrameBarrier via AddJobHandleForProducer.
Job behavior (SpectatorSiteJob): - Iterates over SpectatorSite components in each chunk. - For each SpectatorSite, reads the referenced event entity and checks for a Duration component. - If Duration exists: reads Duration.m_EndFrame and the event's PrefabRef, then reads the Prefab's SpectatorEventData.m_TerminationDuration. It computes an effective end frame as duration.m_EndFrame + (uint)(262144f * spectatorEventData.m_TerminationDuration). - If the current simulation frame is >= computed end frame, it removes the SpectatorSite component via the parallel command buffer. - Uses ComponentLookup (read-only) for Duration, PrefabRef, SpectatorEventData and ComponentTypeHandle for SpectatorSite (read-write).
-
[MethodImpl(MethodImplOptions.AggressiveInlining)] private void __AssignQueries(ref SystemState state)
Compiler-generated helper used during OnCreateForCompiler. Currently builds (and disposes) an EntityQueryBuilder; left minimal here for the generated code path. -
protected override void OnCreateForCompiler()
Helper used by generated code paths to assign query and type handles: -
Calls __AssignQueries and __TypeHandle.__AssignHandles to prepare handles used by the job.
-
Nested types (important):
private struct SpectatorSiteJob : IJobChunk
The Burst-compiled job that inspects and potentially removes SpectatorSite components. Uses v128 chunkEnabledMask param for enabled-mask aware execution path (standard IJobChunk signature). Uses EntityCommandBuffer.ParallelWriter for thread-safe removals.private struct TypeHandle
Holds cached EntityTypeHandle, ComponentTypeHandleand ComponentLookup handles for Duration, PrefabRef and SpectatorEventData. Has a method __AssignHandles(ref SystemState) which calls state.GetEntityTypeHandle, GetComponentTypeHandle () and GetComponentLookup (isReadOnly: true) for required types.
Usage Example
// The system is internal to the simulation; example demonstrates the intended effect.
// This is what SpectatorSiteSystem effectively does each update interval:
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
// Acquire dependencies and set up the query (done in the system's OnCreate)
m_SimulationSystem = World.GetOrCreateSystemManaged<SimulationSystem>();
m_EndFrameBarrier = World.GetOrCreateSystemManaged<EndFrameBarrier>();
m_SiteQuery = GetEntityQuery(ComponentType.ReadWrite<SpectatorSite>(),
ComponentType.Exclude<Deleted>(),
ComponentType.Exclude<Temp>());
RequireForUpdate(m_SiteQuery);
}
[Preserve]
protected override void OnUpdate()
{
// Prepare job data (includes current simulation frame and command buffer)
var job = new SpectatorSiteJob
{
m_SimulationFrame = m_SimulationSystem.frameIndex,
m_CommandBuffer = m_EndFrameBarrier.CreateCommandBuffer().AsParallelWriter(),
// component handles filled via compiler-generated TypeHandle helpers
};
// Schedule the job in parallel across chunks and register with the EndFrameBarrier
Dependency = JobChunkExtensions.ScheduleParallel(job, m_SiteQuery, Dependency);
m_EndFrameBarrier.AddJobHandleForProducer(Dependency);
}
// Effect: SpectatorSite components are removed when their related event has passed
// (Duration end frame + termination duration multiplier) to stop the entity from being considered a spectator site.
Notes and implementation details: - The job uses BurstCompile for performance. - The termination duration is computed using a multiplier of 262144f * SpectatorEventData.m_TerminationDuration (this appears to convert a float duration to simulation frames or an internal fixed-point scale); the resulting float is cast to uint and added to Duration.m_EndFrame. - Structural changes (component removals) are deferred via the EndFrameBarrier's EntityCommandBuffer.ParallelWriter to remain safe for multi-threaded jobs. - The system only runs when there are SpectatorSite entities (RequireForUpdate), and it executes every 64 frames (UPDATE_INTERVAL).