Skip to content

Game.CitySystem

Assembly: Game (game assembly)
Namespace: Game.Simulation

Type: public class

Base: GameSystemBase, ICitySystem, IDefaultSerializable, ISerializable, IPostDeserialize

Summary:
CitySystem is the central ECS system that owns and manages the player's city entity and exposes high-level city data (money, XP). It handles creation and initialization of the city entity for new games, restoration and migration when loading older saves, and serialization/deserialization of the city entity reference. The system queries game parameter singletons (economy and service fee parameters), keeps a reference to the CityConfigurationSystem for game settings (e.g., unlimited money), and updates cached values (m_Money, m_XP) every update tick by reading the PlayerMoney and XP components from the city entity. PostDeserialize contains migration logic to add components or buffers for older save versions (Loan, Creditworthiness, DangerLevel, TradeCost, etc.) and to initialize buffers/components needed for a new city (Policy buffer, ServiceFee defaults, PlayerMoney, Population, Tourism, etc.).


Fields

  • private EntityQuery m_ServiceFeeParameterQuery
    Used to obtain the ServiceFeeParameterData singleton which provides default service fees for a new city.

  • private EntityQuery m_EconomyParameterQuery
    Used to obtain the EconomyParameterData singleton (e.g., starting money) for initialization.

  • private CityConfigurationSystem m_CityConfigurationSystem
    Cached reference to the CityConfigurationSystem obtained in OnCreate; used to read configuration such as unlimitedMoney.

  • private Entity m_City
    Entity reference to the city's main entity. Serialized/deserialized by the system and created on new games.

  • private int m_Money
    Cached copy of the player's money read each update from the PlayerMoney component on the city entity.

  • private int m_XP
    Cached copy of the city's XP read each update from the XP component on the city entity.

Properties

  • public Entity City { get; }
    Returns the internal city Entity (m_City). This is the primary handle to the city entity for other systems/mods.

  • public int moneyAmount { get; }
    Returns the cached money amount (m_Money). Updated each tick in OnUpdate by reading PlayerMoney component.

  • public int XP { get; }
    Returns the cached XP (m_XP). Updated each tick in OnUpdate by reading the XP component.

Constructors

  • public CitySystem()
    Default constructor. Marked with [Preserve] in the original source to prevent stripping. Initialization of queries and referenced systems occurs in OnCreate rather than the constructor.

Methods

  • protected override void OnCreate() : System.Void
    Sets up the system: caches CityConfigurationSystem via World.GetOrCreateSystemManaged(), and initializes two EntityQuery instances for ServiceFeeParameterData and EconomyParameterData singletons. This is where system-level references and queries are prepared.

  • protected override void OnUpdate() : System.Void
    If m_City is not Entity.Null, reads PlayerMoney and XP component data from the entity manager and updates the m_Money and m_XP caches. Used to expose up-to-date values via properties without requiring callers to read components directly.

  • public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
    Writes the city entity reference (m_City) into the provided writer. Used by the game's serialization pipeline.

  • public void Deserialize<TReader>(TReader reader) where TReader : IReader
    Reads the city entity reference from a reader into m_City and resets m_Money to 0. Further initialization/migration is handled in PostDeserialize.

  • public void SetDefaults(Context context) : System.Void
    Sets default field values for newly created instances of the system: sets m_City to Entity.Null and m_Money to 0.

  • public void PostDeserialize(Context context) : System.Void
    Performs initialization and migration after deserialization depending on the context.purpose and context.version:

  • For Purpose.NewGame: creates the city entity if none exists and adds required components/buffers (MilestoneLevel, XP, DevTreePoints, Policy buffer, CityModifier buffer, Loan, Creditworthiness, DangerLevel, TradeCost buffer, ServiceFee buffer with defaults, PlayerMoney initialized with economy start money, SpecializationBonus buffer, Population and Tourism with defaults).
  • For Purpose.LoadGame: performs version checks and adds missing components for compatibility (Loan, Creditworthiness for versions < Version.loanComponent; DangerLevel for versions < Version.dangerLevel).
  • For versions older than Version.cityTradeCost it generates TradeCost entries by iterating resources and computing costs.
  • Ensures PlayerMoney.m_Unlimited is set according to city configuration and removes Resources component if present in certain contexts.

This method is the hub that creates or migrates the city entity and ensures its components and buffers are consistent with the current game expectations.

Usage Example

// Get the CitySystem from the World and read current money and XP
var citySystem = World.GetOrCreateSystemManaged<Game.Simulation.CitySystem>();
Entity cityEntity = citySystem.City;
int money = citySystem.moneyAmount;
int xp = citySystem.XP;

// Typical OnCreate is already implemented by the system, but if you subclass:
[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    // base.OnCreate sets up the queries and CityConfigurationSystem
}

{{ Additional notes: - The system expects various component types and buffers to exist on the 'city' entity. Other mods that create or manipulate the city entity should be careful to preserve or migrate these components. - PostDeserialize contains save-version migration logic — if you rely on older save formats or add components that must survive upgrades, consider adding migration steps here. - Accessing or modifying city components directly via EntityManager (e.g., PlayerMoney) is common; use CitySystem.City as the entity handle. }}