Game.CitizenPresenceSystem
Assembly: Assembly-CSharp
Namespace: Game.Simulation
Type: public class
Base: GameSystemBase
Summary:
System that updates the CitizenPresence component on building entities. It runs periodically (GetUpdateInterval returns 64) and schedules a Burst-compiled IJobChunk (CitizenPresenceJob) which:
- Iterates buildings that have CitizenPresence and are not Deleted/Temp,
- Reads m_Delta on CitizenPresence to determine a change request,
- Computes a capacity value based on WorkProvider.m_MaxWorkers, HouseholdCitizen buffer length and renters + building property counts,
- Converts the delta into a presence delta scaled by capacity, applies a randomized factor, clamps the result to [0,255] and writes it back to CitizenPresence.m_Presence,
- Resets m_Delta to 0.
The job uses component lookups (WorkProvider, PrefabRef, BuildingPropertyData), buffer lookups (Renter, HouseholdCitizen) and a RandomSeed to introduce variation.
Fields
-
private EntityQuery m_BuildingQuery
Used to select entities that contain the CitizenPresence component (and to exclude Deleted and Temp). Required for update when the system is active. -
private TypeHandle __TypeHandle
Holds component lookups and type handles used by the job and filled in OnCreateForCompiler. Used internally to obtain EntityTypeHandle, ComponentTypeHandle, ComponentLookup/BufferLookup handles for WorkProvider, PrefabRef, BuildingPropertyData, Renter, HouseholdCitizen, etc. -
(Nested types)
private struct CitizenPresenceJob
(BurstCompile)
Nested IJobChunk that performs the per-chunk processing described in the summary. Contains fields: - EntityTypeHandle m_EntityType
- ComponentTypeHandle
m_CitizenPresenceType - ComponentLookup
m_WorkProviderData - ComponentLookup
m_PrefabRefData - ComponentLookup
m_BuildingPropertyData - BufferLookup
m_Renters - BufferLookup
m_HouseholdCitizens -
RandomSeed m_RandomSeed
-
(Nested types)
private struct TypeHandle
Helper to cache and assign various type/component/buffer lookup handles from the SystemState.
Properties
- None exposed by this system (no public properties).
Constructors
public CitizenPresenceSystem()
Default constructor. The system uses OnCreate to set up its EntityQuery and registers it as required for update to ensure the system runs only when relevant components are present.
Methods
-
public override int GetUpdateInterval(SystemUpdatePhase phase)
Returns 64. The system is scheduled to run at this interval (every 64 simulation ticks / frames as defined by the engine's scheduling). -
[Preserve] protected override void OnCreate()
Creates an EntityQuery that selects entities with a read-only CitizenPresence component and excludes Deleted and Temp components: - All: CitizenPresence (ReadOnly)
-
None: Deleted, Temp
Calls RequireForUpdate(m_BuildingQuery) to avoid running when no matching entities exist. -
[Preserve] protected override void OnUpdate()
Builds and schedules the Burst-compiled CitizenPresenceJob as a parallel job using the cached type handles from __TypeHandle (converted via InternalCompilerInterface helpers) and RandomSeed.Next(). The scheduled JobHandle is stored back into base.Dependency. -
private void __AssignQueries(ref SystemState state)
Internal helper used during compiler wiring. Currently constructs/initializes query builders (no runtime logic beyond compiler setup in this implementation). -
protected override void OnCreateForCompiler()
Called by generated/compiled-infrastructure to assign queries and type handles used by the job (calls __AssignQueries and __TypeHandle.__AssignHandles). -
(Inside CitizenPresenceJob)
public void Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
Main per-chunk job logic: iterates chunk entities, skips entries with m_Delta == 0, computes capacity via GetCapacity, converts delta into a presence change scaled by capacity, applies random spread, clamps presence to [0,255], resets m_Delta to 0 and writes component back. -
(Inside CitizenPresenceJob)
private int GetCapacity(Entity entity)
Helper to compute capacity for a given entity: - Adds WorkProvider.m_MaxWorkers if the entity has WorkProvider
- Adds HouseholdCitizen buffer length if present
- Returns the summed capacity (used to scale presence changes)
Usage Example
// Example: from other system or tool code you can request a change in presence for a building:
// (This demonstrates how to change CitizenPresence.m_Delta so CitizenPresenceSystem will update presence.)
using Unity.Entities;
using Game.Buildings; // contains CitizenPresence
public void AddPresenceDelta(Entity buildingEntity, int delta)
{
if (!EntityManager.HasComponent<CitizenPresence>(buildingEntity))
return;
var cp = EntityManager.GetComponentData<CitizenPresence>(buildingEntity);
cp.m_Delta += delta; // CitizenPresenceSystem will pick this up next time it runs
EntityManager.SetComponentData(buildingEntity, cp);
}
Notes and modding tips: - The system intentionally resets m_Delta to 0 after applying changes; to apply sustained changes you must repeatedly adjust m_Delta or modify m_Presence directly. - Presence calculations are capacity-normalized and randomized; small deltas on very large-capacity buildings will produce small presence changes. - Because the job uses Burst and parallel scheduling, use the provided ComponentLookup/BufferLookup APIs when interacting with these components or buffers from other jobs/systems to avoid race conditions.