Game.Prefabs.Modes.ServiceConsumptionGlobalMode
Assembly: Assembly-CSharp (game)
Namespace: Game.Prefabs.Modes
Type: public class
Base: EntityQueryModePrefab
Summary:
ServiceConsumptionGlobalMode is a prefab mode that allows scaling of service-building consumption and upkeep values (excluding zone buildings). It applies multipliers to ConsumptionData (upkeep, electricity, water, garbage) and adjusts ServiceUpkeepData entries that use money. The mode caches original money-upkeep values so they can be restored later. The main work is performed in a Burst-compiled IJobChunk (ModeJob) scheduled in parallel for performance on large entity sets.
Fields
-
public float m_UpkeepMultiplier
Multiplies building upkeep (both the ConsumptionData.m_Upkeep and ServiceUpkeepData entries whose resource is Money). Typical values: 1.0 = unchanged, 0.5 = half upkeep, 2.0 = double upkeep. -
public float m_ElectricityConsumptionMultiplier
Multiplier applied to ConsumptionData.m_ElectricityConsumption. -
public float m_WaterConsumptionMultiplier
Multiplier applied to ConsumptionData.m_WaterConsumption. -
public float m_GarbageAccumlationMultiplier
Multiplier applied to ConsumptionData.m_GarbageAccumulation. -
private Dictionary<Entity, ServiceUpkeepData> m_CachedUpkeepDatasDatas
Cache mapping entity -> ServiceUpkeepData (the Money upkeep entry) collected in StoreDefaultData so RestoreDefaultData can restore the original money upkeep amounts later. -
(nested)
private struct ModeJob : IJobChunk
Burst-compiled job that performs the per-chunk modification. Its fields and behavior: public float m_UpkeepMultiplier
public float m_ElectricityConsumptionMultiplier
public float m_WaterConsumptionMultiplier
public float m_GarbageAccumlationMultiplier
public ComponentTypeHandle<ConsumptionData> m_ConsumptionType
public BufferTypeHandle<ServiceUpkeepData> m_ServiceUpkeepType
ModeJob.Execute iterates entities in the chunk and: - Reads/writes ConsumptionData: scales m_Upkeep, m_ElectricityConsumption, m_WaterConsumption, m_GarbageAccumulation by corresponding multipliers (casts to int after multiplication). - Accesses the ServiceUpkeepData dynamic buffer and, for entries where m_Upkeep.m_Resource == Resource.Money, multiplies m_Upkeep.m_Amount by m_UpkeepMultiplier and writes back.
Properties
- (none declared in this class)
Constructors
public ServiceConsumptionGlobalMode()
No explicit constructor is declared in the source; the default parameterless constructor is used. Typical initialisation of multipliers happens in the inspector or by the system that instantiates the prefab.
Methods
public override EntityQueryDesc GetEntityQueryDesc()
Returns an EntityQueryDesc that targets entities having:- All: ConsumptionData (read-only) and ServiceUpkeepData (read-only)
- Any: BuildingData or BuildingExtensionData (so it selects building prefabs / building extensions)
-
None: SpawnableBuildingData (excludes spawnable/zone buildings) This ensures the mode targets service buildings (not zone buildings) that carry consumption/upkeep data.
-
protected override void RecordChanges(EntityManager entityManager, Entity entity)
Called to mark which component data/buffers are relevant for recording changes. It calls: - entityManager.GetBuffer
(entity) -
entityManager.GetComponentData
(entity) This ensures these components/buffers are tracked for change/restore. -
public override void StoreDefaultData(EntityManager entityManager, ref NativeArray<Entity> entities, PrefabSystem prefabSystem)
Caches the original money upkeep values for each given entity: - Iterates all provided entities
- For each entity, iterates the ServiceUpkeepData buffer
-
If a buffer entry's m_Upkeep.m_Resource == Resource.Money, stores that ServiceUpkeepData in m_CachedUpkeepDatasDatas keyed by the entity This prepares the mode for later restoring those exact original amounts.
-
public override JobHandle ApplyModeData(EntityManager entityManager, EntityQuery requestedQuery, JobHandle deps)
Schedules the Burst-compiled ModeJob (IJobChunk) with ScheduleParallel using the provided requestedQuery and dependency handle. It builds and passes ComponentTypeHandle(writable) and BufferTypeHandle (writable) plus the configured multipliers. Returns the job handle for chaining. -
public override void RestoreDefaultData(EntityManager entityManager, ref NativeArray<Entity> entities, PrefabSystem prefabSystem)
Restores original data for the provided entities: -
For each entity:
- Attempts to fetch the original prefab via prefabSystem.TryGetPrefab
(entity) and then TryGetExactly () to get base values; if absent, logs a warning and continues. - Restores the ConsumptionData fields (m_Upkeep, m_ElectricityConsumption, m_WaterConsumption, m_GarbageAccumulation) from the prefab's ServiceConsumption component.
- For the ServiceUpkeepData buffer entries where the resource is Money, it looks up the cached ServiceUpkeepData in m_CachedUpkeepDatasDatas and restores m_Upkeep.m_Amount to that cached amount. If no cached entry exists, logs a critical error.
- Attempts to fetch the original prefab via prefabSystem.TryGetPrefab
-
(nested)
private struct ModeJob.Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
Performs the chunk-level modification described above. The job also includes an explicit IJobChunk.Execute implementation that forwards to the typed Execute.
Usage Example
// Example: configure the prefab-mode (typically done in editor or prefab initialization)
ServiceConsumptionGlobalMode mode = /* obtain prefab instance or reference */;
mode.m_UpkeepMultiplier = 0.75f; // reduce money upkeep to 75%
mode.m_ElectricityConsumptionMultiplier = 1f; // keep electricity the same
mode.m_WaterConsumptionMultiplier = 1f; // keep water the same
mode.m_GarbageAccumlationMultiplier = 1.2f; // increase garbage accumulation by 20%
// The prefab system / game mode system will call ApplyModeData(...) which schedules
// the Burst IJobChunk to apply these multipliers across target entities.
// RestoreDefaultData(...) will revert to cached/prefab values when needed.
Notes and implementation details: - The job is Burst-compiled for performance and uses IJobChunk with ComponentTypeHandle and BufferTypeHandle (writable) to modify data in parallel safely. - Multiplications convert floats back to ints (via cast); be aware of rounding/truncation semantics. - Only ServiceUpkeepData entries whose m_Upkeep.m_Resource == Resource.Money are adjusted and cached — other resource types are left untouched. - The EntityQuery excludes spawnable/zone buildings to avoid affecting zoned building consumption/upkeep.