Skip to content

Game.Prefabs.UpkeepModifierData

Assembly:
Assembly-CSharp (most game types live in Assembly-CSharp; adjust if your project uses a different assembly)

Namespace:
Game.Prefabs

Type:
struct UpkeepModifierData

Base:
Implements IBufferElementData, ICombineBuffer, ISerializable

Summary:
UpkeepModifierData represents a per-resource multiplier that modifies upkeep costs for prefabs/entities. It stores which Resource it applies to and a multiplier. It provides a Transform method to apply the multiplier to an upkeep value, a Combine implementation to merge multiple modifiers for the same resource (multiplying their multipliers), and serialization/deserialization logic that is version-aware and uses EconomyUtils to map resources to compact indices for persistence.


Fields

  • public Resource m_Resource
    Holds the Resource enum/value that this modifier applies to (e.g., electricity, water, etc.). Used when combining and when serializing/deserializing via EconomyUtils.

  • public float m_Multiplier
    The multiplier applied to an upkeep value. Transform(upkeep) returns upkeep * m_Multiplier. When combining two modifiers for the same resource, m_Multiplier values are multiplied together.

Properties

  • None (this struct exposes public fields and implements required interface methods directly)

Constructors

  • public UpkeepModifierData()
    Implicit default (value) constructor. After default construction, m_Resource will be the default Resource value and m_Multiplier will be 0.0f — when creating instances you should explicitly set m_Multiplier (e.g., 1.0f for no change) and m_Resource to the intended resource.

Methods

  • public float Transform(float upkeep)
    Applies this modifier to an upkeep value by multiplying upkeep by m_Multiplier and returning the result.

  • public void Combine(NativeList<UpkeepModifierData> result)
    Merges this modifier into a list of existing modifiers. Iterates the provided NativeList; if an entry with the same m_Resource is found, multiplies that entry's m_Multiplier by this.m_Multiplier and returns. If no matching resource is found, adds this modifier to the list. Uses result.ElementAt(i) and result.Add(in this).

  • public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
    Writes the modifier to the provided writer. It writes the multiplier (float) first, then writes a compact sbyte resource index obtained via EconomyUtils.GetResourceIndex(m_Resource). The method is generic over the writer type constrained to IWriter.

  • public void Deserialize<TReader>(TReader reader) where TReader : IReader
    Reads the modifier from the provided reader. Reads m_Multiplier first. For older save versions (reader.context.version < Version.upkeepModifierRelative) the code reads and discards an extra float to remain compatible with the previous format. Then reads a sbyte that is converted back to a Resource via EconomyUtils.GetResource(value). Generic over reader type constrained to IReader.

Usage Example

using Unity.Collections;
using Game.Prefabs;
using Game.Economy;

// create and combine modifiers
var list = new NativeList<UpkeepModifierData>(Allocator.Temp);

var modA = new UpkeepModifierData { m_Resource = Resource.Electricity, m_Multiplier = 1.2f };
var modB = new UpkeepModifierData { m_Resource = Resource.Electricity, m_Multiplier = 0.9f };
var modC = new UpkeepModifierData { m_Resource = Resource.Water, m_Multiplier = 0.8f };

list.Add(modA);
modB.Combine(list); // finds modA by resource and multiplies its multiplier (1.2f * 0.9f)
modC.Combine(list); // adds modC to the list since Water is different

float baseUpkeep = 100f;
float modified = list[0].Transform(baseUpkeep); // applies the combined multiplier

list.Dispose();