Game.HospitalMode
Assembly:
Assembly-CSharp (game assembly where this class is defined)
Namespace:
Game.Prefabs.Modes
Type:
public class HospitalMode
Base:
LocalModePrefab
Summary:
HospitalMode is a LocalModePrefab used to apply mode-specific adjustments to hospital prefabs in Cities: Skylines 2. It contains an array of ModeData entries, each pointing to a hospital PrefabBase and providing modifiers (patient capacity multiplier and treatment bonus). At runtime it resolves the target prefab to an ECS Entity via PrefabSystem and reads/updates the HospitalData component on that entity. The class also logs a critical message (via ComponentBase.baseLog) when a referenced Prefab does not contain a Hospital component.
Fields
-
public ModeData[] m_ModeDatas
An array of mode entries. Each ModeData describes a target hospital prefab and the mode-specific modifiers to apply. -
Serializable public class ModeData
public PrefabBase m_Prefab
Reference to the hospital prefab this mode entry targets. The code obtains the Hospital component from this prefab to resolve the corresponding ECS Entity.public float m_PatientCapacityMultiplier
Multiplier applied to the prefab's HospitalData.m_PatientCapacity when ApplyModeData runs. The final capacity is computed as (existing m_PatientCapacity * m_PatientCapacityMultiplier).public int m_TreatmentBonus
Integer treatment bonus to set on HospitalData.m_TreatmentBonus when ApplyModeData runs.
Notes: - The ModeData class is marked [Serializable] so it is editable in the Unity inspector on the HospitalMode prefab/component. - When a referenced prefab does not contain a Hospital component, the code logs a critical error and skips that entry.
Properties
- (none)
Constructors
public HospitalMode()
Default constructor (no special runtime initialization in the source).
Methods
public override void RecordChanges(EntityManager entityManager, PrefabSystem prefabSystem)
Iterates m_ModeDatas and for each entry:- Gets the Hospital component from the referenced PrefabBase.
- If the Hospital component is missing, logs a critical error and continues.
- Resolves the ECS Entity via prefabSystem.GetEntity(component.prefab).
-
Calls entityManager.GetComponentData
(entity) (the value is not stored). This call is typically used to register that this component type will be read/changed by the system that applies modes (ensures proper dependency/recording behavior in the mod/tooling context). -
public override void ApplyModeData(EntityManager entityManager, PrefabSystem prefabSystem)
Iterates m_ModeDatas and for each entry: - Resolves the Hospital component on the given PrefabBase; logs and skips on missing component.
- Gets the ECS Entity representing the prefab.
- Reads HospitalData from the entity, modifies:
- m_PatientCapacity = (int)(existing m_PatientCapacity * m_PatientCapacityMultiplier)
- m_TreatmentBonus = m_TreatmentBonus from ModeData
-
Writes the modified HospitalData back to the entity via entityManager.SetComponentData. This applies the configured mode adjustments to the underlying ECS data so the simulation uses the modified hospital values.
-
public override void RestoreDefaultData(EntityManager entityManager, PrefabSystem prefabSystem)
Iterates m_ModeDatas and for each entry: - Resolves the Hospital component on the PrefabBase; logs and skips when missing.
- Retrieves the ECS Entity for the prefab.
- Reads HospitalData, and restores:
- m_PatientCapacity = component.m_PatientCapacity (the original prefab value stored on the Mono/authoring Hospital component)
- m_TreatmentBonus = component.m_TreatmentBonus
- Writes the restored HospitalData back to the entity. This method resets any previously applied mode adjustments back to the prefab defaults.
Behavioral notes and caveats: - All methods assume PrefabSystem maps a Mono/authoring prefab (Hospital.prefab) to an ECS Entity. If that mapping fails, subsequent ECS calls will throw. - The class uses ComponentBase.baseLog.Critical for missing targets — ensure logs are monitored when configuring mode entries. - Patient capacity is converted to int after multiplication; be mindful of rounding/truncation.
Usage Example
Configure this component on a mode prefab in the Unity editor, or create entries from code. Example showing how one might create and configure a ModeData entry in C# (editor or runtime setup):
// Example: creating and configuring a HospitalMode and a single ModeData entry
var hospitalMode = new HospitalMode();
hospitalMode.m_ModeDatas = new HospitalMode.ModeData[1];
var entry = new HospitalMode.ModeData();
entry.m_Prefab = /* assign a PrefabBase reference to the Hospital prefab (from assets/inspector) */;
entry.m_PatientCapacityMultiplier = 1.5f; // increase capacity by 50%
entry.m_TreatmentBonus = 10; // flat treatment bonus
hospitalMode.m_ModeDatas[0] = entry;
// At runtime the game/mod framework calls RecordChanges, ApplyModeData, RestoreDefaultData
// with appropriate EntityManager and PrefabSystem instances to apply or revert the mode.
Additional tips: - Configure m_ModeDatas using the Unity inspector on the HospitalMode component attached to a mode prefab so designers can tweak multipliers and bonuses without code changes. - Test ApplyModeData and RestoreDefaultData in isolation to ensure no unintended persistent changes to prefab ECS data.