Skip to content

Game.Common.Target

Assembly:
Assembly-CSharp (game assembly, likely where game mod types are compiled)

Namespace:
Game.Common

Type:
struct

Base:
IComponentData, IQueryTypeParameter, ISerializable

Summary:
A small ECS component representing a reference to another Entity (a "target"). Intended for use with Unity.Entities in Cities: Skylines 2 mod code. The struct is serializable via Colossal.Serialization (IWriter / IReader) so it can be persisted in save data or transferred where the game's serialization pipeline is used. Typical uses include marking an entity as targeting another entity (for AI, interactions, locks, etc.) and querying/filtering entities that carry a Target component.


Fields

  • public Unity.Entities.Entity m_Target
    Holds the Entity that this component targets. This is a plain Entity identifier from Unity.Entities and is blittable, making the component suitable as an IComponentData (fast to store in archetypes). It is serialized/deserialized by the struct's ISerializable implementation.

Properties

  • This struct defines no properties.

Constructors

  • public Target(Unity.Entities.Entity target)
    Creates a new Target component initialized to the provided Entity. Use this when adding the component to an entity via EntityManager/EntityCommandBuffer.

Methods

  • public void Serialize<TWriter>(TWriter writer) where TWriter : Colossal.Serialization.Entities.IWriter
    Writes the m_Target field to the provided writer. Used by the Colossal.Serialization pipeline to persist the component. Implementation calls writer.Write(m_Target).

  • public void Deserialize<TReader>(TReader reader) where TReader : Colossal.Serialization.Entities.IReader
    Reads the m_Target field from the provided reader. Used when loading or reconstructing the component from serialized data. Implementation calls reader.Read(out m_Target).

Usage Example

// Adding the component to an entity with EntityManager
EntityManager entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
Entity someEntity = /* obtain or create entity */;
Entity targetEntity = /* target entity */;

// Create and add Target component
var targetComp = new Game.Common.Target(targetEntity);
entityManager.AddComponentData(someEntity, targetComp);

// Querying entities that have a Target component (example in a SystemBase)
Entities
    .WithAll<Game.Common.Target>()
    .ForEach((Entity e, in Game.Common.Target t) =>
    {
        // t.m_Target is the referenced entity
    }).ScheduleParallel();

// Serialization example (pseudo-usage; actual writer provided by game save pipeline)
void SaveComponent<TWriter>(ref TWriter writer, in Game.Common.Target comp) where TWriter : Colossal.Serialization.Entities.IWriter
{
    comp.Serialize(writer);
}

void LoadComponent<TReader>(ref TReader reader, out Game.Common.Target comp) where TReader : Colossal.Serialization.Entities.IReader
{
    comp = new Game.Common.Target();
    comp.Deserialize(reader);
}

{{ This Target component is lightweight and intended for common target-reference patterns in ECS systems. Because it implements IQueryTypeParameter it can be used directly in queries and type-based filters. When using serialization, ensure the surrounding save/load context provides the appropriate IWriter/IReader implementations from the game's Colossal.Serialization pipeline. }}