Skip to content

Game.Prefabs.Chirp

Assembly:
Assembly-CSharp (game scripts, typical for Cities: Skylines 2 mods)

Namespace:
Game.Prefabs

Type:
class

Base:
ComponentBase

Summary:
Chirp is a prefab component that provides a list of "chirp" prefabs. When a trigger using this component fires, one of the configured chirp prefabs is intended to be selected (randomly) and used by the trigger system. At initialization the component resolves the inspector-assigned PrefabBase entries into entities and stores them in a DynamicBuffer of TriggerChirpData on the prefab entity so runtime systems can pick and spawn a chirp entity when the trigger occurs.


Fields

  • public PrefabBase[] m_Chirps
    An array of PrefabBase references configured in the inspector. The tooltip indicates: "When the trigger happens, one of these chirps will be selected randomly". Null entries are ignored. These entries are resolved into entities and added to the TriggerChirpData buffer during LateInitialize.

Properties

  • (none)
    This class does not declare public properties. It adds and populates a buffer component (TriggerChirpData) on the prefab entity instead of exposing properties.

Constructors

  • public Chirp()
    Default constructor (implicit). No special construction logic is required; runtime initialization is handled in LateInitialize.

Methods

  • public override void GetPrefabComponents(HashSet<ComponentType> components)
    Adds the required component type(s) for prefab entities. Specifically, this method adds ComponentType.ReadWrite() to the provided set so the prefab will have the buffer component used to store resolved chirp entities.

  • public override void GetArchetypeComponents(HashSet<ComponentType> components)
    Empty override. This implementation does not add any additional archetype components here.

  • public override void LateInitialize(EntityManager entityManager, Entity entity)
    Performs runtime resolution of the inspector-specified PrefabBase entries into entity references. Implementation details:

  • Calls base.LateInitialize.
  • Gets the PrefabSystem from the entityManager's world.
  • Obtains the DynamicBuffer from the given entity.
  • If m_Chirps is null or empty, returns early.
  • Iterates m_Chirps and for each non-null PrefabBase it calls PrefabSystem.GetEntity(prefabBase) and adds a TriggerChirpData entry with the resolved entity into the buffer. This populates the TriggerChirpData buffer so trigger-handling systems can pick and spawn one of the chirp prefabs at runtime.

Usage Example

Example showing how a runtime system or script might read the populated TriggerChirpData buffer and pick a random chirp entity to spawn/use when a trigger fires:

using Unity.Entities;
using UnityEngine;

public static class ChirpTriggerHelper
{
    // Called when the trigger fires for the given prefab entity
    public static Entity? PickRandomChirp(EntityManager em, Entity prefabEntity)
    {
        if (!em.HasBuffer<TriggerChirpData>(prefabEntity))
            return null;

        var buffer = em.GetBuffer<TriggerChirpData>(prefabEntity);
        if (buffer.Length == 0)
            return null;

        int idx = UnityEngine.Random.Range(0, buffer.Length);
        return buffer[idx].m_Chirp;
    }
}

Notes and tips: - Ensure the prefab entity has the TriggerChirpData buffer type (this component's GetPrefabComponents adds it). - LateInitialize resolves PrefabBase -> Entity using PrefabSystem.GetEntity; that must be available during LateInitialize. - Null entries in m_Chirps are skipped during resolution. - Actual spawning/instantiation of the selected chirp entity will depend on the game's prefab instantiation API or PrefabSystem usage in your mod/system.