Skip to content

Game.Buildings.BuildingModifier

Assembly: Assembly-CSharp
Namespace: Game.Buildings

Type: struct

Base: IBufferElementData, ISerializable

Summary:
Represents a small modifier applied to a building, stored as a 2D delta (Unity.Mathematics.float2). The struct is defined as a dynamic buffer element (InternalBufferCapacity(0)) and implements custom serialization/deserialization to remain compatible across a version refactor (Version.modifierRefactoring). After the refactor both X and Y components are serialized; before the refactor only the Y component was persisted and the Deserialize method reads accordingly.


Fields

  • public Unity.Mathematics.float2 m_Delta
    Stores the X/Y delta applied by this modifier. Serialization/deserialization populates this field; older save-data formats stored only the Y component.

  • [InternalBufferCapacity(0)] (struct attribute)
    Indicates the buffer has zero inline capacity and is treated as a dynamic buffer.

Properties

  • This type defines no properties.
    All state is stored directly in the public field m_Delta and via the buffer container.

Constructors

  • No explicit constructors (uses the default value-type constructor).
    When added to an entity buffer, instances are created/initialized by the caller or by the serialization system when loading.

Methods

  • public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
    Writes the full float2 m_Delta to the provided writer (writer.Write(m_Delta)). Used when saving/serializing building modifier data.

  • public void Deserialize<TReader>(TReader reader) where TReader : IReader
    Reads data from the provided reader into m_Delta. Behavior depends on the reader.context.version:

  • If reader.context.version >= Version.modifierRefactoring: reads the entire float2 into m_Delta.
  • Else (older versions): reads only a single float into m_Delta.y (legacy format), leaving m_Delta.x at its default (usually 0). Implementation uses ref locals to read directly into the struct field.

Usage Example

// Add a BuildingModifier to an entity's dynamic buffer
using Unity.Entities;
using Unity.Mathematics;
using Game.Buildings;

// assume entityManager and entity are available
var buffer = entityManager.AddBuffer<BuildingModifier>(entity);

// add a modifier (delta X=1.0, Y=2.0)
buffer.Add(new BuildingModifier { m_Delta = new float2(1.0f, 2.0f) });

// iterate modifiers
foreach (var mod in buffer)
{
    float2 delta = mod.m_Delta;
    // apply delta to building data/logic...
}

Notes: - The struct implements ISerializable to support custom persistence and backwards compatibility with pre-refactor saves where only the Y value was stored. - Because of InternalBufferCapacity(0), BuildingModifier buffers are stored as dynamic buffers (no inline capacity optimization).