Game.Prefabs.ServiceUpkeepData
Assembly: Assembly-CSharp (typical for game code / mods; actual assembly may vary)
Namespace: Game.Prefabs
Type: struct
Base: Implements Unity.Entities.IBufferElementData, ICombineBuffer
Summary:
ServiceUpkeepData is a buffer element used to describe a resource upkeep for a service/prefab. It stores a ResourceStack (resource type + amount) and a flag indicating whether the upkeep should scale with usage. The struct provides logic to combine duplicate upkeep entries (same resource and same scale flag) and to produce a scaled copy of the upkeep entry for a given usage scale.
Fields
-
public ResourceStack m_Upkeep
Holds the resource and amount consumed as upkeep. ResourceStack contains at least m_Resource (resource type/id) and m_Amount (integer amount). This field is the primary data carried by the buffer element. -
public bool m_ScaleWithUsage
Flag indicating whether this upkeep entry is intended to scale with service usage. Note: ApplyServiceUsage does not check this flag itself — callers should only call ApplyServiceUsage for entries that should be scaled (or check the flag before using the result).
Properties
- This struct exposes no properties.
Constructors
public ServiceUpkeepData()
The default (compiler-generated) parameterless constructor is available. It leaves fields at their default values (m_Upkeep default-initialized, m_ScaleWithUsage = false). There is no custom constructor defined in source.
Methods
public void Combine(NativeList<ServiceUpkeepData> result)
Combines this instance into the provided NativeList by searching for an existing entry with the same m_Upkeep.m_Resource and the same m_ScaleWithUsage flag. If found, it increments that entry's m_Upkeep.m_Amount by this instance's amount and returns. If no matching entry is found, the current instance is appended to the list.
Notes: - Comparison uses resource equality and the scale flag; different flags or different resources will not be merged. - Complexity: O(n) on the result list (linear search). - The method mutates the provided NativeList; ensure correct ownership/lifetime and thread usage.
public ServiceUpkeepData ApplyServiceUsage(float scale)
Returns a new ServiceUpkeepData with the same m_Resource and m_ScaleWithUsage flag, but with m_Upkeep.m_Amount scaled by the provided float scale. The scaled amount is converted to int via a cast (int)((float)amount * scale), which truncates toward zero (potentially losing fractional parts).
Notes: - The method does not check m_ScaleWithUsage — it always applies the numeric scaling. It is up to callers to only apply scaling when appropriate. - Beware of rounding/truncation when scaling small amounts; consider rounding strategies if needed.
Usage Example
// Combine multiple upkeep entries into a deduplicated list
var list = new NativeList<ServiceUpkeepData>(Allocator.Temp);
try
{
var entryA = new ServiceUpkeepData
{
m_Upkeep = new ResourceStack { m_Resource = ResourceType.Electricity, m_Amount = 10 },
m_ScaleWithUsage = true
};
var entryB = new ServiceUpkeepData
{
m_Upkeep = new ResourceStack { m_Resource = ResourceType.Electricity, m_Amount = 5 },
m_ScaleWithUsage = true
};
entryA.Combine(list);
entryB.Combine(list);
// list now has one entry for Electricity with m_Amount == 15
}
finally
{
list.Dispose();
}
// Applying usage scaling (caller should check m_ScaleWithUsage before calling)
var original = new ServiceUpkeepData
{
m_Upkeep = new ResourceStack { m_Resource = ResourceType.Water, m_Amount = 7 },
m_ScaleWithUsage = true
};
float usageScale = 0.5f;
var scaled = original.ApplyServiceUsage(usageScale);
// scaled.m_Upkeep.m_Amount == (int)(7 * 0.5f) == 3 (truncation)
Additional remarks:
- Because ServiceUpkeepData implements IBufferElementData it is suitable for use as a DynamicBuffer