Game.Simulation.FindJobSystem
Assembly:
Namespace: Game.Simulation
Type: public class
Base: GameSystemBase
Summary:
FindJobSystem is responsible for matching unemployed citizens (job seekers) with available workplaces, scheduling pathfind requests for commuting, and finalizing hires when pathfinding completes. It runs on a 16-frame interval and coordinates with PathfindSetupSystem, EndFrameBarrier, TriggerSystem and other simulation services. Internally it uses three Burst-compiled jobs:
- CalculateFreeWorkplaceJob: aggregates available workplace slots into a small cache.
- FindJobJob: iterates job seeker entities, decides suitable job levels, enqueues pathfinding requests and writes PathInformation components.
- StartWorkingJob: consumes pathfind results, assigns employees to workplaces (updates buffers and Worker components), and emits TriggerActions for started work.
The system also maintains internal caches and debug watch values for the number of citizens that started working during an update.
Fields
-
private const int UPDATE_INTERVAL = 16
Used to indicate the system's update frequency (overrides GetUpdateInterval to return 16). -
private EntityQuery m_JobSeekerQuery
Query selecting job seeker entities that do not yet have PathInformation (i.e. need a path to a job). -
private EntityQuery m_ResultsQuery
Query selecting job seeker entities that already have PathInformation (results available to be processed). -
private EntityQuery m_FreeQuery
Query selecting entities that expose FreeWorkplaces (used to aggregate available jobs across providers). -
private SimulationSystem m_SimulationSystem
Reference to the SimulationSystem (used to get the current frame index for randomization, etc). -
private TriggerSystem m_TriggerSystem
Reference to the TriggerSystem used to enqueue TriggerAction events (e.g. CitizenStartedWorking). -
private CountHouseholdDataSystem m_CountHouseholdDataSystem
Reference used to obtain employable counts by education level and to check readiness of household data. -
private PathfindSetupSystem m_PathfindSetupSystem
Reference used to enqueue pathfind setup requests (SetupQueueItem) and to register writers. -
private EndFrameBarrier m_EndFrameBarrier
Barrier system used to create command buffers and ensure correct job dependencies for entity modifications. -
private NativeArray<int> m_FreeCache
A small NativeArray[int] of length 5 (allocator persistent) used to store aggregated counts of free workplace slots per education level (uneducated .. highly educated). -
private NativeValue<int> m_StartedWorking
Debug-watched NativeValue holding the count of citizens that started working during the most recent StartWorkingJob execution. -
private JobHandle m_WriteDeps
Holds write-side JobHandle dependencies for diagnostic/debug purposes (marked with DebugWatchDeps). -
private TypeHandle __TypeHandle
Internal struct storing Entity/Component type handles and ComponentLookup/BufferLookup objects used by the jobs. Assigned in OnCreateForCompiler.
Properties
- (This system exposes no public properties.)
Constructors
public FindJobSystem()
Default constructor; the system uses OnCreate to initialize dependencies and allocate native containers.
Methods
-
public override int GetUpdateInterval(SystemUpdatePhase phase)
Returns 16. Controls how often the system runs (every 16 simulation frames). -
protected override void OnCreate()
Initializes the system: acquires references to other systems (EndFrameBarrier, PathfindSetupSystem, SimulationSystem, TriggerSystem, CountHouseholdDataSystem), sets up entity queries (m_JobSeekerQuery, m_ResultsQuery, m_FreeQuery), allocates persistent native containers (m_FreeCache, m_StartedWorking) and calls RequireAnyForUpdate to ensure updates only when relevant queries have data. -
protected override void OnDestroy()
Disposes allocated native containers (m_StartedWorking, m_FreeCache) and performs base cleanup. -
protected override void OnUpdate()
Main update method: - If there are job seekers and household count data is ready:
- Schedules CalculateFreeWorkplaceJob to fill m_FreeCache by aggregating all FreeWorkplaces entities.
- Schedules FindJobJob (IJobChunk) in parallel over m_JobSeekerQuery to evaluate each job seeker, pick a suitable education level and enqueue a SetupQueueItem for pathfinding (using PathfindSetupSystem). It writes PathInformation components to mark pending pathfinds and uses the EndFrameBarrier's parallel EntityCommandBuffer.
- Registers PathfindSetupSystem and EndFrameBarrier producers for dependency tracking.
-
If there are results (entities with PathInformation), schedules StartWorkingJob to process finished PathInformation entries, assign employees to workplaces (updating Employee buffers and Worker components), enqueue TriggerActions for started work, and tally m_StartedWorking. Uses EndFrameBarrier's command buffer (single-threaded) and registers producers.
-
private void __AssignQueries(ref SystemState state)
Compiler helper — placeholder that constructs queries (in generated code it currently only constructs and disposes an EntityQueryBuilder). Called from OnCreateForCompiler. -
protected override void OnCreateForCompiler()
Compiler helper invoked during system creation to assign the component type handles in __TypeHandle and to set up queries / handles used by the Burst jobs. -
Nested job types (important behavior summarized):
- CalculateFreeWorkplaceJob : IJob
Aggregates FreeWorkplaces buffers across matching entities into the m_FreeCache (per education category). - FindJobJob : IJobChunk
For each job seeker chunk: validates owner/citizen/household state, determines origin (home/current building/temporary), computes desired job level based on the job seeker's education and availability in the global m_FreeCache, probabilistically filters some candidates based on distribution and prior workplace level, creates PathInformation (PathFlags.Pending) and enqueues a SetupQueueItem with origin/destination and PathfindParameters. Uses OwnedVehicle buffer to update allowed methods and uses a parallel command buffer for entity writes. - StartWorkingJob : IJob
Iterates result chunks (job seekers with PathInformation not pending), checks destination prefab/workplace data and free workplace slots, picks the best slot for the citizen level, adds an Employee entry to the destination's employee buffer, removes any existing Worker component on the citizen and adds a new Worker component (workplace, level, commute time, shift), enqueues TriggerAction of type CitizenStartedWorking, updates FreeWorkplaces for the destination, and increments m_StartedWorking.value.
Usage Example
// Typical usage in modding environment: obtain the running system from the world.
// The system is usually updated automatically by the simulation world; you can retrieve it to
// inspect or (carefully) interact with it (note: many internals are private).
var findJobSystem = World.DefaultGameObjectInjectionWorld.GetOrCreateSystemManaged<Game.Simulation.FindJobSystem>();
// The system runs every 16 simulation frames. Most integration is done by interacting with
// PathfindSetupSystem, TriggerSystem, or by exposing your own components that the system uses.
// Example: to observe how many citizens started working in the last run you would normally
// use the game's debug-watch tools for m_StartedWorking (it's private and exposed via DebugWatch).