Game.PlayerMoney
Assembly:
Namespace: Game.City
Type: struct
Base: System.ValueType, Unity.Entities.IComponentData, Unity.Entities.IQueryTypeParameter, Colossal.Serialization.Entities.ISerializable
Summary:
Represents the player's money balance for Cities: Skylines 2. Encapsulates an integer balance with hard clamping to a fixed range and an "unlimited" flag. Provides basic operations to add/subtract money and implement save/load via the game's serialization interfaces. Uses Unity.Mathematics.math.clamp to ensure values stay within the allowed limits.
Fields
-
public const int kMaxMoney
Constant that defines the maximum (and effectively the absolute cap) for money values: 2,000,000,000. -
private int m_Money
Backed integer storing the current money amount (clamped to the allowed range). All arithmetic updates operate on this field. -
public bool m_Unlimited
Flag indicating whether the player has unlimited money. When true, the public money property will report the maximum value rather than the stored m_Money.
Properties
public int money { get; }
Returns the current money available to the player. If m_Unlimited is false, returns the clamped m_Money value; if m_Unlimited is true, returns kMaxMoney (2000000000). This property is read-only.
Constructors
public PlayerMoney(int amount)
Initializes a new PlayerMoney instance. The provided amount is clamped to the inclusive range [-2000000000, 2000000000] using math.clamp. The m_Unlimited flag is initialized to false.
Methods
-
public void Add(int value)
Adds value to m_Money and clamps the result to the allowed range. Accepts negative values (equivalent to subtraction). -
public void Subtract(int amount)
Subtracts amount from the stored money by delegating to Add(-amount). Ensures clamping via Add. -
public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
Writes the current money state to the provided writer. Writes m_Money first, then writes m_Unlimited. The writer is expected to handle primitive value writes. Used for saving the component state. -
public void Deserialize<TReader>(TReader reader) where TReader : IReader
Reads the saved state from the provided reader. Reads m_Money first. The m_Unlimited flag is read only if the reader's context version is at least Version.unlimitedMoneyAndUnlockAllOptions — this preserves compatibility with older save versions that didn't store the unlimited flag.
Notes: - All numeric clamping uses Unity.Mathematics.math.clamp and the same hard-coded limits as kMaxMoney and its negative. - The "unlimited" behavior does not represent an infinite value; it causes the public money property to return the kMaxMoney constant.
Usage Example
// Creating a PlayerMoney with 10000 starting funds
var pm = new PlayerMoney(10000);
// Add funds
pm.Add(5000); // increases stored money by 5000
// Subtract funds
pm.Subtract(3000); // decreases stored money by 3000
// Read current available money (respects unlimited flag)
int available = pm.money;
// Toggle unlimited money (note: this field is public)
pm.m_Unlimited = true;
int unlimitedAvailable = pm.money; // returns 2000000000 (kMaxMoney)
// Serialization example (simplified usage)
void Save<TWriter>(TWriter writer, PlayerMoney playerMoney) where TWriter : IWriter
{
playerMoney.Serialize(writer);
}
void Load<TReader>(TReader reader, out PlayerMoney playerMoney) where TReader : IReader
{
playerMoney = new PlayerMoney(0);
playerMoney.Deserialize(reader);
}