Skip to content

Game.Simulation.TouristSpawnSystem

Assembly:
Game (Assembly-CSharp / game runtime assembly)

Namespace:
Game.Simulation

Type:
class TouristSpawnSystem

Base:
GameSystemBase

Summary:
System responsible for spawning tourist households in the city. Runs on a fixed update interval (16 frames) and schedules a Burst-compiled IJob (SpawnTouristHouseholdJob) that: - Reads city Tourism and climate (weather, temperature, precipitation) information. - Computes tourist spawn probability using TourismSystem.GetTouristProbability and current weather/attractiveness. - Randomly selects a household prefab and creates a household entity flagged as Tourist. - Optionally assigns a CurrentBuilding (outside connection) to the spawned tourist household based on demand/OC spawn parameters. Uses an EndFrameBarrier command buffer to create and configure entities safely from the job.


Fields

  • private EntityQuery m_HouseholdPrefabQuery
    Query to select household prefab entities (reads ArchetypeData and HouseholdData). Used to build lists of prefab entities and component data for spawning.

  • private EntityQuery m_OutsideConnectionQuery
    Query to select outside connection entities (excludes electricity/water outside connections, Temp and Deleted). Used to pick a random outside connection for the tourist household spawn target.

  • private EntityQuery m_AttractivenessParameterQuery
    Query to access AttractivenessParameterData singleton (read-only).

  • private EntityQuery m_DemandParameterQuery
    Query to access DemandParameterData singleton (read-only). Used to retrieve TouristOCSpawnParameters for outside connection selection.

  • private EndFrameBarrier m_EndFrameBarrier
    Barrier system used to create a command buffer for structural changes made by the job (entity creation and component additions).

  • private SimulationSystem m_SimulationSystem
    Reference to the SimulationSystem used to read the current simulation frame (frameIndex).

  • private ClimateSystem m_ClimateSystem
    Reference to the ClimateSystem used to read current weather classification, temperature, precipitation and rain/snow flags.

  • private CitySystem m_CitySystem
    Reference to CitySystem used to identify the City entity.

  • private TypeHandle __TypeHandle
    Synthesized struct holding read-only ComponentLookup handles (Tourism, OutsideConnectionData, PrefabRef). Assigned in OnCreateForCompiler for safe access from jobs.

  • private struct SpawnTouristHouseholdJob (nested)
    Burst-compiled IJob that performs the actual spawn logic. Key points:

  • Reads lists of prefab entities, archetype data, household prefab data and outside connection entities.
  • Uses read-only component lookups for Tourism, OutsideConnectionData and PrefabRef.
  • Reads AttractivenessParameterData and DemandParameterData singletons.
  • Uses RandomSeed and Simulation frame to create deterministic randomness.
  • Checks Tourism component on the city entity and computes the spawn chance.
  • If a spawn is to occur, creates a new entity using the prefab archetype, sets PrefabRef and Household with Tourist flag, adds TouristHousehold component and optionally CurrentBuilding if a matching outside connection is found.
  • Uses an EntityCommandBuffer for entity creation and component setting.

  • private struct TypeHandle (nested)
    Holds ComponentLookups and a method to assign them from SystemState. Used to obtain read-only component access within the job.

Properties

  • (none declared on this type)

Constructors

  • public TouristSpawnSystem()
    Default (parameterless) constructor. No custom initialization logic beyond base constructor.

Methods

  • public override int GetUpdateInterval(SystemUpdatePhase phase)
    Returns 16. The system is scheduled to run once every 16 frames (coarse periodic update).

  • [Preserve] protected override void OnCreate()
    Initializes the system:

  • Retrieves EndFrameBarrier, SimulationSystem, ClimateSystem and CitySystem from the World.
  • Sets up EntityQuery instances: household prefab query, outside connection query, demand and attractiveness parameter queries.
  • Calls RequireForUpdate on the household prefab and outside connection queries to ensure the system only runs when relevant data exists.

  • [Preserve] protected override void OnUpdate()
    Builds and schedules the SpawnTouristHouseholdJob:

  • Converts queries to async lists / component data arrays (ToEntityListAsync / ToComponentDataListAsync) and collects their job handles.
  • Obtains component lookups (via __TypeHandle) and singletons for parameter data.
  • Fills job struct fields with climate, city and random seed data and a command buffer from the EndFrameBarrier.
  • Schedules the job (Burst-compiled) and adds the resulting dependency to the EndFrameBarrier via AddJobHandleForProducer.

  • [MethodImpl(MethodImplOptions.AggressiveInlining)] private void __AssignQueries(ref SystemState state)
    Compiler-generated placeholder for query assignment. In this class it does not create additional queries beyond those created in OnCreate; present to satisfy codegen patterns.

  • protected override void OnCreateForCompiler()
    Called by generated/compiled code paths:

  • Calls __AssignQueries and assigns component lookup handles via __TypeHandle.__AssignHandles(ref base.CheckedStateRef).

  • [Preserve] public TouristSpawnSystem()
    (Public parameterless constructor declaration retained for preservation by codegen/runtime.)

Nested job method:

  • private struct SpawnTouristHouseholdJob : IJob -> public void Execute()
    Core spawning logic executed on a worker thread (Burst):
  • Returns early if the city entity has no Tourism component.
  • Generates a Random instance from RandomSeed and the current frame to ensure deterministic randomness.
  • Uses TourismSystem.GetTouristProbability with attractiveness and weather/climate inputs to decide whether to spawn.
  • If spawning, selects a random household prefab, creates an entity with the prefab archetype, assigns PrefabRef and Household (Tourist flag) components, and adds a TouristHousehold with default values.
  • Attempts to pick a matching outside connection using BuildingUtils.GetRandomOutsideConnectionByParameters and adds CurrentBuilding to the spawned entity if found.
  • All entity creation and component modifications are added to the provided EntityCommandBuffer.

Usage Example

// Typical usage: the system is created and managed by the game's World.
// You can obtain the system instance and inspect or interact with it from a mod:

var world = Unity.Entities.World.DefaultGameObjectInjectionWorld;
var touristSystem = world?.GetExistingSystemManaged<Game.Simulation.TouristSpawnSystem>();
if (touristSystem != null)
{
    // The system runs automatically; you can read state from related systems if needed:
    // e.g. access the city entity via CitySystem, or adjust global parameters via parameter singletons.
}

Additional notes: - The system relies on several game-specific components and utilities: Tourism, HouseholdData, ArchetypeData, PrefabRef, TouristHousehold, CurrentBuilding, BuildingUtils, TourismSystem, DemandParameterData.m_TouristOCSpawnParameters, and climate/city systems. - It performs entity creation via an EndFrameBarrier command buffer to ensure structural changes are applied safely at the end of the frame. - The spawn decision takes weather and attractiveness into account to make tourism behavior responsive to game state.