Game.Prefabs.UpkeepModifier
Assembly: Game
Namespace: Game.Prefabs
Type: class
Base: ComponentBase, implements IServiceUpgrade
Summary:
UpkeepModifier is a prefab component used to define per-prefab upkeep (running cost) modifiers for resources. It exposes an array of UpkeepModifierInfo entries (m_Modifiers) which are converted into runtime UpkeepModifierData entries and stored on the entity as a dynamic buffer during initialization. The component registers the required component types so the entity will have a UpkeepModifierData buffer available for systems that apply upkeep modifications.
Fields
public UpkeepModifierInfo[] m_Modifiers
Holds the list of configured upkeep modifiers (resource + multiplier) on the prefab. Each UpkeepModifierInfo is converted into an UpkeepModifierData and added to the entity's DynamicBuffer during Initialize. This field can be set in the prefab inspector or populated programmatically. Initialize checks for null before populating the buffer.
Properties
- (none declared in this class)
There are no explicit C# properties defined on UpkeepModifier. The component exposes its data via the public m_Modifiers field and through the UpkeepModifierData buffer created on the entity.
Constructors
public UpkeepModifier()
Default parameterless constructor (compiler-provided). No special construction logic is defined in the class.
Methods
-
public override void GetPrefabComponents(HashSet<ComponentType> components)
Adds ComponentType.ReadWrite() to the provided components set. This ensures entities created from the prefab will include a DynamicBuffer so the modifier entries can be stored at runtime. -
public override void GetArchetypeComponents(HashSet<ComponentType> components)
No components are added here. The method is provided as an override point but currently leaves archetype component set unchanged. -
public void GetUpgradeComponents(HashSet<ComponentType> components)
Implements IServiceUpgrade. Adds ComponentType.ReadWrite() to the provided components set for upgrade scenarios. This registers the UpkeepModifier component itself for upgrade-related archetypes. -
public override void Initialize(EntityManager entityManager, Entity entity)
Called during prefab initialization. If m_Modifiers is not null, obtains the DynamicBufferfor the entity and appends an UpkeepModifierData entry for each configured UpkeepModifierInfo. Each entry sets: - m_Resource = EconomyUtils.GetResource(upkeepModifierInfo.m_Resource)
- m_Multiplier = upkeepModifierInfo.m_Multiplier This populates the runtime buffer used by systems that modify building upkeep costs.
Additional notes: - Uses EconomyUtils.GetResource to translate the info's resource identifier into the runtime resource representation used by UpkeepModifierData. - The method relies on GetPrefabComponents having added the UpkeepModifierData buffer type to the prefab so the buffer exists at initialization.
Usage Example
// Example: programmatically set modifiers before prefab initialization (or via inspector)
// and let Initialize populate the entity buffer.
public class MyCustomPrefab : UpkeepModifier
{
// This could be set in code or through the prefab inspector
void Configure()
{
m_Modifiers = new[]
{
new UpkeepModifierInfo { m_Resource = ResourceType.Electricity, m_Multiplier = 0.9f },
new UpkeepModifierInfo { m_Resource = ResourceType.Water, m_Multiplier = 1.1f }
};
}
public override void Initialize(EntityManager entityManager, Entity entity)
{
// Ensure any base initialization runs (UpkeepModifier.Initialize will write the buffer)
base.Initialize(entityManager, entity);
// Later, systems can read the buffer:
var buffer = entityManager.GetBuffer<UpkeepModifierData>(entity);
foreach (var entry in buffer)
{
// entry.m_Resource and entry.m_Multiplier are available for upkeep calculations
}
}
}
Additional information / dependencies:
- Related types: UpkeepModifierInfo, UpkeepModifierData, EconomyUtils, ComponentBase, IServiceUpgrade, ComponentType, DynamicBuffer