Skip to content

Game.Objects.Damaged

Assembly: Game
Namespace: Game.Objects

Type: struct

Base: IComponentData, IQueryTypeParameter, ISerializable

Summary:
Represents damage information as a 3-component vector (Unity.Mathematics.float3) for an entity in the ECS. This component is serializable and contains compatibility logic for older save formats: if the saved data predates the introduction of distinct damage types, only the Y component is read and preserved. Useful for storing per-entity damage values (for example different damage channels) when modding Cities: Skylines 2.


Fields

  • public Unity.Mathematics.float3 m_Damage
    Holds the damage values. The three components (x, y, z) can represent multiple damage channels/types. Note: Deserialize handles older save versions which only wrote a single value (stored in the y component).

Properties

  • This type has no properties.

Constructors

  • public Damaged(Unity.Mathematics.float3 damage)
    Initializes the component with the provided damage vector.

Methods

  • public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
    Writes the full float3 damage vector to the writer when saving. Uses the generic IWriter interface from Colossal.Serialization.Entities.

  • public void Deserialize<TReader>(TReader reader) where TReader : IReader
    Reads damage data from the reader. If reader.context.version >= Version.damageTypes, the method reads the full float3 into m_Damage. If the saved data uses an older version (before Version.damageTypes), only a single float value is read into the y component of m_Damage to maintain backward compatibility with older save formats.

Notes: - The method uses ref locals to read directly into the struct field. - Version.damageTypes is a version constant defined elsewhere in the codebase that indicates when multiple damage components were introduced.

Usage Example

using Unity.Entities;
using Unity.Mathematics;
using Game.Objects;

// Add the component to an entity (EntityManager usage)
EntityManager entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
Entity e = entityManager.CreateEntity();

// Set damage with three channels (x, y, z)
entityManager.AddComponentData(e, new Damaged(new float3(1.0f, 2.0f, 0.5f)));

// Read damage back
Damaged d = entityManager.GetComponentData<Damaged>(e);
float3 damage = d.m_Damage;

Additional notes: - The component implements IQueryTypeParameter so it can be used in Entity queries in relevant APIs. - Implementing ISerializable ensures save/load compatibility; modders should be aware of the version check in Deserialize to avoid corrupting old saves.