Skip to content

Game.Simulation.HouseholdPetSpawnSystem

Assembly: Game
Namespace: Game.Simulation

Type: class

Base: GameSystemBase

Summary:
HouseholdPetSpawnSystem is an ECS system responsible for turning household "pet requests" into spawned animal entities. It finds household entities with a Target and a HouseholdPet marker, selects an appropriate animal prefab (using pet/household data and a random seed), and spawns a creature entity with the appropriate components (PrefabRef, Transform, Target, Pet owner link, TripSource, Unspawned, AnimalCurrentLane, random seed). The heavy work is performed inside a Burst-compiled IJobChunk (HouseholdPetSpawnJob) which uses an EndFrameBarrier command buffer (parallel writer) to create and modify entities safely at the end of the frame. The system also creates ResetTrip events when a transport already exists for a household pet.


Fields

  • private EntityQuery m_HouseholdPetQuery
    Query that matches household pet entities: ReadOnly, ReadOnly, excludes Deleted and Temp. Required for system updates.

  • private EntityQuery m_AnimalPrefabQuery
    Query for animal prefab entities (ReadOnly, ReadOnly) used to gather prefab chunks for random selection.

  • private EntityArchetype m_ResetTripArchetype
    Archetype used to create ResetTrip/Event entities (ResetTrip + Event) when a transport exists and a reset trip must be scheduled.

  • private EndFrameBarrier m_EndFrameBarrier
    End-of-frame barrier used to create an EntityCommandBuffer. The job writes to the barrier's command buffer (AsParallelWriter) to perform entity creation/modification.

  • private TypeHandle __TypeHandle
    Internal generated struct that holds EntityTypeHandle, ComponentTypeHandles, and ComponentLookup handles used by the JobChunk. Populated in OnCreateForCompiler.

  • private struct HouseholdPetSpawnJob (nested)
    Burst-compiled IJobChunk that does per-chunk processing of household pet entities. See Methods section for details.

  • private struct TypeHandle (nested)
    Compiler-generated helper storing component/lookup handles and a method to assign them from SystemState.

Properties

  • None (the system exposes no public properties)

Constructors

  • public HouseholdPetSpawnSystem()
    Default constructor (preserved attribute on the class' lifecycle methods). No special construction logic; initialization occurs in OnCreate.

Methods

  • public override int GetUpdateInterval(SystemUpdatePhase phase)
    Returns the fixed update interval for the system. This system returns 16 (so it runs every 16 frames/phase ticks).

  • [Preserve] protected override void OnCreate()
    Initializes queries and archetypes:

  • Retrieves/creates EndFrameBarrier system.
  • Creates m_HouseholdPetQuery (HouseholdPet + Target, excluding Deleted/Temp).
  • Creates m_AnimalPrefabQuery (AnimalData + PetData).
  • Creates m_ResetTripArchetype (Event + ResetTrip).
  • Calls RequireForUpdate(m_HouseholdPetQuery) so system only updates when matching entities exist.

  • [Preserve] protected override void OnUpdate()
    Main scheduling logic:

  • Obtains the animal prefab archetype chunk list asynchronously (ToArchetypeChunkListAsync).
  • Constructs and schedules HouseholdPetSpawnJob in parallel against m_HouseholdPetQuery, wiring in entity/component handles and lookups, random seed, the prefab chunk list, the reset trip archetype, and a parallel command buffer from m_EndFrameBarrier.
  • Disposes the native list with Dispose(jobHandle).
  • Registers jobHandle as a producer with m_EndFrameBarrier (AddJobHandleForProducer) and assigns base.Dependency = jobHandle.

  • protected override void OnCreateForCompiler()
    Compiler-time support that calls __AssignQueries and assigns handles in __TypeHandle.__AssignHandles using the SystemState.

  • private void __AssignQueries(ref SystemState state)
    Generated stub (currently empty except for creating and disposing an EntityQueryBuilder). Present for compiler/IL2CPP compatibility.

  • Nested: HouseholdPetSpawnJob.Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
    Job logic summary:

  • Reads arrays for entities, CurrentTransport, CurrentBuilding, Target, PrefabRef from the chunk.
  • If there are no CurrentBuilding components: removes Target from each entity (cleanup).
  • If there are no CurrentTransport components: for each household entity:
    • Use household prefab -> HouseholdPetData to determine pet type.
    • Use a Random instance derived from RandomSeed and the entity index.
    • Call ObjectEmergeSystem.SelectAnimalPrefab to pick an animal prefab/chunk and random seed for the animal.
    • If a valid prefab is selected and the source building has a Transform, call SpawnPet to create the creature entity:
    • Adds CurrentTransport to the household entity pointing to the created pet and removes CurrentBuilding from the household.
    • Removes Target from the household entity (in all branches).
  • If CurrentTransport exists for the household entity:
    • If the transport entity is not marked Deleted, create a ResetTrip entity (m_ResetTripArchetype) with ResetTrip component linking creature, source, and target and a delay (512u), then remove CurrentBuilding and Target from the household entity.
  • The job uses m_CommandBuffer (parallel writer) for all entity creation/modification.

  • Nested: HouseholdPetSpawnJob.SpawnPet(int jobIndex, Entity householdPet, Entity source, Entity target, Entity prefab, PseudoRandomSeed randomSeed)
    Helper used by the job to create the animal entity:

  • Reads ObjectData for the prefab to get the archetype.
  • Creates an entity with that archetype and sets:
    • PrefabRef(prefab)
    • Transform (copied from source building Transform)
    • Target(target)
    • Game.Creatures.Pet(owner = householdPet)
    • randomSeed
    • TripSource(source, 512u)
    • Unspawned (default tag)
    • AnimalCurrentLane (default tag)
  • Returns the created entity.

  • Nested: private struct TypeHandle.__AssignHandles(ref SystemState state)
    Assigns EntityTypeHandle/ComponentTypeHandle/ComponentLookup instances from the SystemState for use in the job.

Nested types details

  • HouseholdPetSpawnJob (BurstCompile, implements IJobChunk)
  • ReadOnly fields: m_EntityType, m_CurrentTransportType, m_CurrentBuildingType, m_TargetType, m_PrefabRefType, m_PetDataType, m_DeletedData, m_TransformData, m_HouseholdPetData, m_ObjectData, m_AnimalPrefabChunks, m_RandomSeed, m_ResetTripArchetype
  • Writable field: m_CommandBuffer (EntityCommandBuffer.ParallelWriter)
  • Implements Execute and uses a SpawnPet helper. The job removes/sets components on household entities and creates/reset trip entities as needed.

  • TypeHandle

  • Holds the component and lookup handles used by the job. Populated by __AssignHandles when the system is created by the compiler/runtime.

Usage Example

Example of a minimal setup that will let HouseholdPetSpawnSystem process a household pet request. The system is a managed ECS system and is registered in the World automatically when the game loads systems. To request a pet from a household, add a HouseholdPet marker and a Target to the household entity (Target should point to the target building entity). The system will then schedule the job and spawn the pet if appropriate.

// Pseudocode usage that triggers the system:
Entity household = entityManager.CreateEntity();
entityManager.AddComponentData(household, new HouseholdPet());         // mark household as requesting a pet
entityManager.AddComponentData(household, new CurrentBuilding(homeBuildingEntity)); // where the household currently is
entityManager.AddComponentData(household, new Target(targetBuildingEntity));       // where pet should go / target

// On the next update (every 16 ticks as defined by GetUpdateInterval),
// HouseholdPetSpawnSystem will run, pick an animal prefab, spawn it, and set CurrentTransport on the household.

Notes and tips: - The system uses ObjectEmergeSystem.SelectAnimalPrefab to choose a specific animal prefab using HouseholdPetData and PetData from prefabs — modders can influence available pet types by adding/modifying AnimalData/PetData/HouseholdPetData on prefab entities. - The job is Burst-compiled and schedules parallel work; any modifications to component layouts should maintain compatibility with the job's expected components (CurrentTransport, CurrentBuilding, Target, PrefabRef, PetData). - EndFrameBarrier is used to defer entity changes until it is safe to mutate archetypes in a job. If you need to observe spawned pet entities immediately, use the barrier's command buffer dependency to wait for the job to complete.