Skip to content

Game.EndFrameBarrier

Assembly:
Game (city/Skylines 2 game assemblies)

Namespace:
Game

Type:
class

Base:
Unity.Entities.SystemBase (End-of-frame barrier system)

Summary:
EndFrameBarrier is a runtime/system helper used to collect EntityCommandBuffer producers that run during simulation jobs and to play back their recorded commands at the end of the frame. It exposes methods to create command buffers (including parallel writers) and to register JobHandles produced by jobs that write into those command buffers so the barrier can ensure the jobs finish before playback. It is commonly used by simulation systems that schedule parallel jobs and need to apply entity structural changes afterwards (for example HearseAISystem uses it to create an EntityCommandBuffer.AsParallelWriter and then call AddJobHandleForProducer with the scheduled job handle).


Fields

  • private System.Diagnostics.Stopwatch m_Stopwatch
    Used internally to measure/track the time spent on barrier playback (profiling/timing). This stopwatch helps the system keep statistics and/or debug how much time is spent executing the queued commands at the barrier.

  • private Unity.Jobs.JobHandle <producerHandle>k__BackingField
    Backing field for the producer job handle property. Tracks the combined JobHandle of producers (jobs that wrote to ECBs produced by this barrier) so playback can wait on these jobs before executing recorded commands.

Properties

  • public Unity.Jobs.JobHandle producerHandle { get; private set }
    The JobHandle representing outstanding work from producers that wrote into command buffers created by this barrier. Systems that schedule jobs which write to a command buffer produced by EndFrameBarrier should add their job handle to the barrier (via AddJobHandleForProducer) so the barrier can wait on them before playback. The setter is private — users update it indirectly via AddJobHandleForProducer.

Constructors

  • public EndFrameBarrier()
    Default constructor. Creates the barrier/system instance; typically you obtain an instance via World.GetOrCreateSystemManaged() in another system's OnCreate.

Methods

  • protected virtual OnCreate() : System.Void
    Initializes internal state (stopwatch, any internal queues/structures). When writing systems, you usually call base.World.GetOrCreateSystemManaged() to obtain the instance; the barrier itself sets up its own internal resources in OnCreate.

  • public Unity.Entities.EntityCommandBuffer CreateCommandBuffer()
    Creates a new EntityCommandBuffer that will be played back by this barrier. Use this when scheduling single-threaded producers that will record commands to be applied at the barrier.

  • public Unity.Entities.EntityCommandBuffer.ParallelWriter CreateCommandBuffer().AsParallelWriter()
    Creates a parallel-capable writer for an ECB that can be used from parallel jobs. The returned ParallelWriter must only be used from jobs that register their JobHandle with AddJobHandleForProducer.

  • public void AddJobHandleForProducer(Unity.Jobs.JobHandle handle)
    Registers a JobHandle from a producer job that wrote into a command buffer created by this barrier. The barrier will combine the handle(s) and ensure they complete before the recorded commands are played back. Call this immediately after scheduling the producer job(s).

  • public void AddJobHandleForProducer(JobHandle handle, int producerIndex) (overload / internal variants)
    Variants may exist to support parallel writers / multiple producers. Purpose remains: ensure the barrier waits for all recorded command producers.

  • public void AddJobHandleForProducerAndCompleteIfNeeded(JobHandle handle)
    (Some implementations provide helper variants — conceptually ensures that registered producer work is considered in the barrier's synchronization).

  • protected virtual OnUpdate()
    At end of frame the barrier's OnUpdate will ensure all registered producer handles have completed and then play back recorded command buffers. This is automatic; systems using the barrier only need to register their producer handles.

  • public void AddJobHandleForProducer(JobHandle handle)
    (Repeat/important) After scheduling any job that records to an ECB created by this barrier, call this method to add the job handle. The barrier will then ensure playback waits for the job.

Notes: - Always register job handles from jobs that write into barrier-produced ECBs to avoid race conditions or native container misuse. - When using the AsParallelWriter variant inside a parallel job, register the scheduled JobHandle with AddJobHandleForProducer so the barrier knows to wait.

Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    m_Stopwatch = new Stopwatch();
}

Additional common usage (example pattern from HearseAISystem): - Obtain the barrier in OnCreate: m_EndFrameBarrier = base.World.GetOrCreateSystemManaged(); - Create a parallel command buffer when scheduling a job: m_CommandBuffer = m_EndFrameBarrier.CreateCommandBuffer().AsParallelWriter(); - Schedule your job and get a JobHandle (jobHandle). - Register the job handle with the barrier so playback waits for job completion: m_EndFrameBarrier.AddJobHandleForProducer(jobHandle); - Barrier will play back ECB at end of frame once the registered job handles are complete.

This ensures safe, deterministic application of entity structural changes recorded from parallel simulation jobs.