Skip to content

Game.MailBoxSystem

Assembly: Game
Namespace: Game.Simulation

Type: class

Base: GameSystemBase

Summary:
MailBoxSystem is an ECS simulation system that scans MailBox components each update interval and creates PostVanRequest entities when a mailbox has accumulated enough mail. It uses a Burst-compiled IJobChunk (MailBoxTickJob) scheduled in parallel to iterate mailboxes, reads the PostConfigurationData singleton to determine the accumulation tolerance, and issues entity creation commands via an EndFrameBarrier command buffer (as a parallel writer). The system runs every 512 frames and excludes PostFacility, Temp, Deleted, and Destroyed entities from the mailbox query.


Fields

  • private const uint UPDATE_INTERVAL = 512u
    The update interval (in frames) returned by GetUpdateInterval; the system runs every 512 frames.

  • private EntityQuery m_MailBoxQuery
    Query selecting entities with a MailBox and TransportStop, excluding PostFacility, Temp, Deleted, and Destroyed.

  • private EntityQuery m_PostConfigurationQuery
    Query to obtain the PostConfigurationData singleton used to determine mail accumulation tolerance.

  • private EntityArchetype m_PostVanRequestArchetype
    Archetype used when creating PostVanRequest entities (includes ServiceRequest, PostVanRequest, RequestGroup).

  • private SimulationSystem m_SimulationSystem
    Reference to the SimulationSystem in the world (cached on create).

  • private EndFrameBarrier m_EndFrameBarrier
    Reference to the EndFrameBarrier system used to obtain a command buffer for producing new entities.

  • private TypeHandle __TypeHandle
    Internal struct holding EntityTypeHandle, MailBox ComponentTypeHandle, and PostVanRequest ComponentLookup; assigned in OnCreateForCompiler.

  • private struct MailBoxTickJob (nested)
    Burst-compiled IJobChunk job that iterates mailbox chunks and calls RequestPostVanIfNeeded for each mailbox.

  • Job fields:

    • EntityTypeHandle m_EntityType — entity handle used to get Entities from chunk.
    • ComponentTypeHandle<Game.Routes.MailBox> m_MailBoxType — read-only mailbox component type handle.
    • ComponentLookup<PostVanRequest> m_PostVanRequestData — read-only lookup to check existing PostVanRequest components.
    • EntityArchetype m_PostVanRequestArchetype — archetype used for creating new PostVanRequest entities.
    • PostConfigurationData m_PostConfigurationData — configuration singleton with threshold values.
    • EntityCommandBuffer.ParallelWriter m_CommandBuffer — parallel command buffer used to create request entities.
  • Key behavior:

    • For each MailBox entity, if mail amount >= PostConfigurationData.m_MailAccumulationTolerance and the mailbox's m_CollectRequest entity does not already have a PostVanRequest component, creates a PostVanRequest entity with flags set to Collect | MailBoxTarget, sets the amount (capped at ushort 65535), and assigns RequestGroup(32).
  • private struct TypeHandle (nested)
    Helper to assign and store ECS type handles for use by the job; assigned via __AssignHandles(ref SystemState).


Properties

  • (No public properties exposed by this system)

Constructors

  • public MailBoxSystem()
    Default constructor; the system relies on OnCreate to initialize queries and references.

Methods

  • public override int GetUpdateInterval(SystemUpdatePhase phase)
    Returns 512, meaning the system is scheduled every 512 simulation frames.

  • [Preserve] protected override void OnCreate()

  • Caches references to SimulationSystem and EndFrameBarrier from the World.
  • Creates m_MailBoxQuery to match MailBox + TransportStop and exclude PostFacility, Temp, Deleted, Destroyed.
  • Creates m_PostConfigurationQuery to access PostConfigurationData.
  • Creates m_PostVanRequestArchetype for producing PostVanRequest entities.
  • Calls RequireForUpdate(m_MailBoxQuery) so the system runs only when relevant entities exist.

  • [Preserve] protected override void OnUpdate()

  • Builds and schedules the Burst-compiled MailBoxTickJob in parallel using JobChunkExtensions.ScheduleParallel, supplying type handles, the PostConfigurationData singleton, and an EndFrameBarrier command buffer as a parallel writer.
  • Adds the resulting JobHandle to the EndFrameBarrier via AddJobHandleForProducer so the command buffer is consumed after the job completes.
  • Sets base.Dependency to the scheduled job handle.

  • [MethodImpl(MethodImplOptions.AggressiveInlining)] private void __AssignQueries(ref SystemState state)
    Internal helper used by compiler-generated OnCreateForCompiler; here it constructs and disposes an EntityQueryBuilder (no-op in this compiled form).

  • protected override void OnCreateForCompiler()
    Calls base implementation, assigns queries and calls __TypeHandle.__AssignHandles(ref base.CheckedStateRef) to prepare type handles for jobs.

  • (Nested) private void MailBoxTickJob.Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
    Iterates entities in chunk, extracts MailBox components and entities, calls RequestPostVanIfNeeded for each entry.

  • (Nested) private void MailBoxTickJob.RequestPostVanIfNeeded(int jobIndex, Entity entity, Game.Routes.MailBox mailBox)
    If mailBox.m_MailAmount >= m_PostConfigurationData.m_MailAccumulationTolerance and no existing PostVanRequest component exists on mailBox.m_CollectRequest, creates a PostVanRequest entity (flags = Collect | MailBoxTarget), sets amount clamped to ushort max, and assigns RequestGroup(32) using the parallel command buffer.


Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    // MailBoxSystem registers its queries and will run every 512 frames.
    // No additional setup required for normal operation.
}

// Example: What the system does internally (conceptual)
// If you want to manually create a mailbox and see a PostVanRequest produced,
// ensure a PostConfigurationData singleton exists with an appropriate
// m_MailAccumulationTolerance value. When the mailbox's m_MailAmount
// reaches or exceeds that threshold, MailBoxSystem will create a
// PostVanRequest entity for collection.