Skip to content

Game.Common.Destroyed

Assembly:
Namespace: Game.Common

Type: struct

Base: IComponentData, IQueryTypeParameter, ISerializable

Summary:
Destroyed is a lightweight ECS component that records a destruction-related event entity together with a float field (m_Cleared) used by save/load logic. It is serializable via the Colossal.Serialization interfaces and is intended to be attached to entities to mark that something was destroyed (and optionally when/if it was cleared). This struct is used in the game's ECS workflows and participates in serialization/deserialization across versions.


Fields

  • public Entity m_Event
    Holds an Entity reference that represents the destruction event. This is written first during serialization and read first during deserialization. When present, it can be used to look up or trigger additional destruction handling.

  • public float m_Cleared
    A float flag/value related to the cleared state of the destroyed item. In the constructor it is initialized to 0f. During deserialization this value is only read when the reader's context version is >= Version.destroyedCleared (i.e., it is a later-save compatibility field).

Properties

  • None
    This struct exposes no properties; its data are public fields and it implements the required interfaces for ECS and serialization.

Constructors

  • public Destroyed(Entity _event)
    Initializes a Destroyed instance with the given event Entity and sets m_Cleared to 0f. This constructor is convenient when creating the component to add to an entity immediately after an event is created.

Methods

  • public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
    Writes the component fields to the provided writer in the following order: m_Event, then m_Cleared. This method is used by the save system to persist component state.

  • public void Deserialize<TReader>(TReader reader) where TReader : IReader
    Reads component fields from the provided reader. Reads m_Event unconditionally; reads m_Cleared only if reader.context.version >= Version.destroyedCleared to maintain backward compatibility with older save formats. Note: the method uses ref local variables (ref Entity value, ref float cleared) to read values directly into the struct fields.

Usage Example

// Create a Destroyed component tied to an event entity and add it to an entity.
Entity eventEntity = /* create or obtain event entity */;
Destroyed destroyedComp = new Destroyed(eventEntity);

// Using EntityManager from Unity.Entities
EntityManager em = World.DefaultGameObjectInjectionWorld.EntityManager;
Entity targetEntity = /* an entity representing the destroyed object */;
em.AddComponentData(targetEntity, destroyedComp);

// Example of reading the component later:
if (em.HasComponent<Destroyed>(targetEntity))
{
    Destroyed d = em.GetComponentData<Destroyed>(targetEntity);
    // use d.m_Event and d.m_Cleared as needed
}

Additional notes: - The struct is safe to default-initialize (default(Entity) and 0f) if you need an empty marker. - The conditional read of m_Cleared ensures older saves without that field remain compatible; ensure save-version constants (Version.destroyedCleared) are available in the mod context if you interact with serialization versions.