Skip to content

Game.Events.Duration

Assembly: Game (assembly containing the mod/game code)

Namespace: Game.Events

Type: public struct Duration

Base: IComponentData, IQueryTypeParameter, ISerializable

Summary: Represents a time span expressed in game frames. This struct is a plain ECS component used to mark an entity with a start and end frame for an event or effect. It is also serializable via the Colossal.Serialization.Entities ISerializable API, so it can be written to and read from saved data streams.


Fields

  • public uint m_StartFrame Represents the frame index at which the duration begins. Stored as an unsigned 32-bit integer (uint). Use the current frame counter (for example UnityEngine.Time.frameCount or the game's frame tracking) when assigning this value.

  • public uint m_EndFrame Represents the frame index at which the duration ends. Also a uint. The code does not enforce any ordering (start <= end); callers should ensure values make sense for their logic.

Properties

  • (none)
    This struct exposes no properties; it uses public fields for data storage.

Constructors

  • (default) public Duration()
    No explicit constructors are defined in the source. The default parameterless constructor is available (auto-generated) and initializes both fields to 0. Create an instance with an object initializer to set start/end frame values.

Methods

  • public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
    Writes the m_StartFrame and m_EndFrame to the provided writer in that order. The method copies the fields to local variables and calls writer.Write on each value.

  • public void Deserialize<TReader>(TReader reader) where TReader : IReader
    Reads values back from the given reader into m_StartFrame and m_EndFrame using reader.Read and passing the fields by ref. The read order matches the write order (start then end).

Notes on serialization: - The generic writer/reader constraints allow the struct to be used with the Colossal serialization infrastructure. - Ensure the same ordering is respected when reading/writing (start then end) to maintain compatibility.

Usage Example

// Create a Duration component for an entity that should be active
// for 300 frames starting at the current frame.
uint start = (uint)UnityEngine.Time.frameCount;
uint end = start + 300u;
var duration = new Game.Events.Duration { m_StartFrame = start, m_EndFrame = end };

EntityManager entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
entityManager.AddComponentData(someEntity, duration);

// Later, inside a system, check if the duration is active:
uint currentFrame = (uint)UnityEngine.Time.frameCount;
var d = entityManager.GetComponentData<Game.Events.Duration>(someEntity);
bool isActive = currentFrame >= d.m_StartFrame && currentFrame <= d.m_EndFrame;

{{ Additional info: - Use this component in ECS queries to filter entities by having a duration. - When writing systems that update or remove expired durations, compare against the same frame source used to set the fields. - Consider edge cases where wraparound of uint might occur if using very long-running counters (unlikely in normal play). }}