Skip to content

Game.UI.InGame.ChirpLinkSystem

Assembly: Assembly-CSharp
Namespace: Game.UI.InGame

Type: class

Base: GameSystemBase, IDefaultSerializable, ISerializable

Summary:
ChirpLinkSystem is a managed game system used by the UI to track and cache "chirp" (in‑game message) metadata and the links between chirps and game entities. It listens for created/updated/deleted chirp and link entities via EntityQueries, maintains an in‑memory cache of chirp sender and linked entities (including cached names via the NameSystem), and keeps ChirpLink buffers on linked entities in sync. The system also implements serialization to persist the cached chirp metadata for save/load and supports initialization when the game is loaded.

Key responsibilities: - Build and maintain a Dictionary for fast access to chirp sender and link names. - Register/unregister ChirpLink buffer entries on entities that are linked by chirps. - React to entity creation, update and deletion via EntityQueries in OnUpdate(). - Support save/load through ISerializable/IReader/IWriter implementations and restore links on load.


Fields

  • private NameSystem m_NameSystem
    Caches a reference to the game's NameSystem used to resolve and cache entity names for chirp senders and linked entities.

  • private EntityQuery m_CreatedChirpQuery
    Query for newly created Chirp entities (Chirp + Created, excluding Deleted). Used to initialize cached entries and register links when chirps are spawned.

  • private EntityQuery m_AllChirpsQuery
    Query for all Chirp entities (excluding Deleted). Used by Initialize() to build the full cache when needed (e.g., on load).

  • private EntityQuery m_DeletedChirpQuery
    Query for Chirp entities marked as Deleted. Used to remove cached entries and unregister links.

  • private EntityQuery m_UpdatedLinkEntityQuery
    Query for entities that have ChirpLink and Updated (excluding Deleted). Used to update cached link names when a linked entity changes.

  • private EntityQuery m_DeletedLinkEntityQuery
    Query for entities that had ChirpLink removed (Deleted). Used to remove link entries from cached chirp data when link entities get deleted.

  • private Dictionary<Entity, CachedChirpData> m_CachedChirpData
    Main in-memory cache mapping a Chirp entity -> CachedChirpData (sender cached name and optionally an array of cached linked entity names). This dictionary is serialized for save/load.


Nested types

  • public struct CachedChirpData : ISerializable
    Holds cached information about a chirp: the sender (CachedEntityName) and an array of CachedEntityName for each linked entity (m_Links). Provides constructors to build cache entries from Chirp and buffers, Update/Remove helpers to update cache when linked entities change, and Serialize/Deserialize implementations for save/load.

  • public struct CachedEntityName : ISerializable
    A small serializable struct containing an Entity and a NameSystem.Name snapshot (m_Entity, m_Name). Used to preserve the display name for a sender or linked entity even if the entity is later destroyed (m_Entity may be set to Entity.Null on removal).


Properties

  • (none public)
    This system exposes no public properties. It exposes TryGetData(Entity, out CachedChirpData) to query cached chirp data.

Constructors

  • public ChirpLinkSystem()
    Default constructor. The system is created by the World / system manager. Initialization of queries and cache is done in OnCreate().

Methods

  • protected override void OnCreate()
    Initializes the system: obtains NameSystem, creates the various EntityQuery instances used to detect created/updated/deleted chirps and link entities, allocates the cache dictionary, and sets RequireAnyForUpdate to ensure the system only runs when relevant changes occur.

  • public bool TryGetData(Entity chirp, out CachedChirpData data)
    Public accessor for the cached chirp information. Returns true and fills out data if the cache contains the chirp entity key, otherwise returns false.

  • protected override void OnUpdate()
    Primary update loop. Handles:

  • Created chirps: builds CachedChirpData entries, registers links (adds ChirpLink buffers to linked entities), and registers the sender.
  • Updated link entities: updates cached names for linked entities when a link entity has been updated.
  • Deleted link entities: removes link references from cached chirp data when a link entity is deleted.
  • Deleted chirps: unregisters all links associated with the deleted chirps and removes the chirp entry from the cache. The method uses temporary NativeArray allocations for queries (Allocator.TempJob/Temp) and disposes them appropriately.

  • private void RegisterLink(Entity linkEntity, Entity chirpEntity)
    Ensures the linkEntity has a DynamicBuffer, and adds a ChirpLink entry pointing to the chirpEntity if it doesn't already exist.

  • private void UnregisterLink(Entity linkEntity, Entity chirpEntity)
    Removes the ChirpLink that references chirpEntity from the linkEntity's ChirpLink buffer. If the buffer becomes empty after removal, the ChirpLink component is removed from the entity.

  • private bool LinkExists(DynamicBuffer<ChirpLink> links, Entity link)
    Helper to test whether a given chirp is already present in the ChirpLink buffer to avoid duplicates.

  • private void Initialize()
    Rebuilds the entire m_CachedChirpData from the current set of chirp entities (m_AllChirpsQuery). Used when the cached dictionary is empty after load or when a full rebuild is needed.

  • public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
    Serializes m_CachedChirpData into the writer. Writes the count and each entry (chirp Entity key and its CachedChirpData). Called as part of save.

  • public void Deserialize<TReader>(TReader reader) where TReader : IReader
    Deserializes the cache from reader into m_CachedChirpData. Called during load.

  • public void SetDefaults(Context context)
    Clears the cached data. Called to set default state (e.g., new game).

  • protected override void OnGameLoaded(Context serializationContext)
    Called when a game is loaded. If the cache is empty (likely because it was not present in the save), calls Initialize() to build the cache from current world entities.


Usage Example

// Obtain the system from a World (or from inside another system):
var chirpLinkSystem = World.GetOrCreateSystemManaged<Game.UI.InGame.ChirpLinkSystem>();

// Query cached data for a chirp entity:
if (chirpLinkSystem.TryGetData(chirpEntity, out var cached))
{
    // cached.m_Sender.m_Name contains the cached sender name snapshot
    // cached.m_Links (if not null) contains cached names for linked entities
}

Additional notes for modders: - The system is a managed GameSystem and is intended to run on the main thread using Unity's managed ECS system patterns. - It relies on EntityQuery results and temporary NativeArray allocations — be careful to respect allocation/disposal patterns if you copy/adapt code. - The cached data stores snapshots of entity names (via NameSystem) so the UI can display consistent names even if the underlying entity is later removed. When an entity is removed, the cache entry's Entity is set to Entity.Null to preserve the name text. - Because RegisterLink/UnregisterLink manipulate entity buffers, these are run on the main thread through the managed EntityManager API — avoid calling these manually from jobs. - The system implements ISerializable to persist its cache; any mod altering chirp/link entities may need to ensure compatibility with serialization ordering.