Game.Buildings.CityServiceWorkplaceInitializeSystem
Assembly: Assembly-CSharp
Namespace: Game.Buildings
Type: class
Base: GameSystemBase
Summary:
This system initializes and updates WorkProvider components on city service buildings (service workplaces) when relevant upkeep or upgrade data changes. It listens for changes to CityServiceUpkeep / ServiceUpgrade and, using a Burst-compiled IJobChunk (UpdateWorkplaceJob), computes the correct maximum worker count for each building and either adds, updates, or removes the WorkProvider component. Structural changes are issued through a ModificationBarrier5 command buffer to remain safe with the Jobs system. The system also reads a singleton BuildingEfficiencyParameterData to set the initial efficiency cooldown on newly (re)created WorkProviders.
Fields
-
private EntityQuery m_UpdatedQuery
This query targets entities relevant for update: entities with CityServiceUpkeep and Created, excluding Deleted/Temp/ServiceUpgrade (with a second query for ServiceUpgrade changed entries). The system is required for update when this query matches. -
private ModificationBarrier5 m_ModificationBarrier
A barrier system used to create an EntityCommandBuffer for structural changes (adding/removing WorkProvider components) safely from jobs. -
private TypeHandle __TypeHandle
Holds cached component/entity type handles and ComponentLookup/BufferLookup instances used by UpdateWorkplaceJob. It is assigned in OnCreateForCompiler and used to build the job's handles each frame. -
private EntityQuery __query_1169823966_0
A built query used to fetch the singleton BuildingEfficiencyParameterData used by the job to set initial efficiency cooldowns. -
Nested:
private struct UpdateWorkplaceJob : IJobChunk
- This Burst-compiled job iterates matching chunks and for each entity:
- Resolves the effective entity (follows Owner if present),
- Looks up the entity's prefab and checks for WorkplaceData,
- Calls CityUtils.GetCityServiceWorkplaceMaxWorkers(...) to get the correct max worker count,
- If a WorkProvider exists on the entity, updates its m_MaxWorkers and adjusts m_EfficiencyCooldown,
- If no WorkProvider exists and max workers > 0 and entity is Created, adds a WorkProvider via the command buffer,
- If max workers == 0 and WorkProvider exists, removes the WorkProvider component.
-
Job fields include many ComponentLookup/BufferLookup members referenced in the calculation (PrefabRef, InstalledUpgrade buffer, SchoolData, Student buffer, etc.), the command buffer, and BuildingEfficiencyParameterData.
-
Nested:
private struct TypeHandle
- Holds EntityTypeHandle, ComponentTypeHandle
and many ComponentLookup/BufferLookup fields. It exposes __AssignHandles(ref SystemState) which fills handles from the SystemState. This is the compiled helper for safe access inside the job.
Properties
- None (this system has no public properties)
Additional info: All required read/write/lookups are provided to the job via the TypeHandle and job struct fields rather than as public properties.
Constructors
public CityServiceWorkplaceInitializeSystem()
Default constructor. The system relies on OnCreate/OnCreateForCompiler to set up queries and cached handles.
Methods
protected override void OnCreate()
: System.Void- Builds the m_UpdatedQuery (two EntityQueryDesc combined) to detect CityServiceUpkeep changes and ServiceUpgrade cases.
- Obtains the ModificationBarrier5 from the World to use for structural changes.
-
Calls RequireForUpdate on m_UpdatedQuery and also requires the singleton BuildingEfficiencyParameterData so the system only runs when relevant data exists.
-
protected override void OnUpdate()
: System.Void - Prepares an instance of UpdateWorkplaceJob, filling all ComponentLookup/BufferLookup/EntityTypeHandle fields using InternalCompilerInterface and the cached __TypeHandle handles.
- Sets the job's command buffer created from m_ModificationBarrier and the m_EfficiencyParameters singleton fetched from __query_1169823966_0.
-
Schedules the job with JobChunkExtensions.Schedule against m_UpdatedQuery and registers the job handle with the ModificationBarrier5 so structural changes are synchronized.
-
protected override void OnCreateForCompiler()
: System.Void -
Called by generated/compiled code paths; assigns queries and component handles by calling __AssignQueries and __TypeHandle.__AssignHandles with the system's CheckedStateRef.
-
private void __AssignQueries(ref SystemState state)
: System.Void -
Internal helper that constructs the EntityQuery used to fetch BuildingEfficiencyParameterData (includes system-level query options).
-
(Nested/Job)
UpdateWorkplaceJob.Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
- The core chunk-processing method described above. It is the Burst-compiled worker that mutates WorkProvider components via ComponentLookup or emits structural commands via the command buffer.
Additional notes: - The job and most lookups are marked ReadOnly where appropriate; WorkProvider lookup is used as read/write via ComponentLookup. Structural additions/removals are done via the command buffer to keep changes job-safe. - The system uses CityUtils.GetCityServiceWorkplaceMaxWorkers(...) to compute max workers; the correctness of outcomes depends on that utility function and presence/format of PrefabRef and InstalledUpgrade buffers.
Usage Example
// This system runs automatically as part of the game's ECS world when CityServiceUpkeep/service upgrades change.
// Example: if you implement a mod that changes BuildingEfficiencyParameterData, ensure the singleton exists so this system runs:
// Example snippet showing how the system sets a WorkProvider when a service building becomes active:
// (Conceptual — the system does this internally in UpdateWorkplaceJob)
Entity building = /* some service building entity */;
int maxWorkers = CityUtils.GetCityServiceWorkplaceMaxWorkers(building, ref prefabRefs, ref installedUpgrades, ref deleteds, ref workplaceDatas, ref schoolDatas, ref studentBufs);
if (maxWorkers == 0)
{
// system will remove WorkProvider via command buffer
}
else
{
// system will add or update WorkProvider.m_MaxWorkers and set initial cooldown:
short initialCooldown = (short)(-efficiencyParameters.m_ServiceBuildingEfficiencyGracePeriod);
// WorkProvider { m_MaxWorkers = maxWorkers, m_EfficiencyCooldown = initialCooldown }
}
Developer tips: - If you rely on WorkProvider being present immediately after creation, note that it may be added by this system via a command buffer and therefore becomes live on the next sync point. Use the barrier/job dependencies if you need to read it in the same frame. - The job is Burst-compiled and uses ComponentLookup and BufferLookup. Ensure any custom components used in CityUtils.GetCityServiceWorkplaceMaxWorkers are compatible with Burst and job safety rules.