Skip to content

Game.TerrainInitializeSystem

Assembly: Game
Namespace: Game.Simulation

Type: class

Base: GameSystemBase

Summary:
Initializes and wires together terrain-related systems and reads terrain configuration data (TerrainPropertiesPrefab) at startup. This system obtains references to several terrain/water/snow systems and a prefab system in OnCreate, creates EntityQuery objects to find terrain-related singleton components, and uses RequireForUpdate to ensure it only runs when terrain properties exist. In OnUpdate it reads the TerrainPropertiesPrefab for the singleton terrain properties entity and applies configuration values (currently setting WaterSystem.MaxSpeed from the prefab).


Fields

  • private EntityQuery m_TerrainPropertiesQuery
    Holds an EntityQuery that matches entities with Created and TerrainPropertiesData components. Used as the primary trigger/query for this system; the system calls RequireForUpdate on it so the system will only update when such an entity exists.

  • private EntityQuery m_TerrainMaterialPropertiesQuery
    Holds an EntityQuery that matches entities with Created and TerrainMaterialPropertiesData components. In this class it is created in OnCreate but not currently used in OnUpdate; likely intended for future terrain material initialization.

  • private TerrainSystem m_TerrainSystem
    Reference to the game's TerrainSystem created/obtained from the World. Cached in OnCreate for use by this system (not explicitly used in the provided code but available for initialization tasks).

  • private TerrainRenderSystem m_TerrainRenderSystem
    Reference to the TerrainRenderSystem obtained from the World and cached for possible rendering-related initialization tasks.

  • private TerrainMaterialSystem m_TerrainMaterialSystem
    Reference to the TerrainMaterialSystem obtained from the World and cached for possible material setup.

  • private WaterSystem m_WaterSystem
    Reference to the WaterSystem obtained from the World. Used in OnUpdate to set the MaxSpeed property from the TerrainProperties prefab.

  • private WaterRenderSystem m_WaterRenderSystem
    Reference to the WaterRenderSystem obtained from the World and cached for possible water rendering initialization.

  • private SnowSystem m_SnowSystem
    Reference to the SnowSystem obtained from the World and cached for possible snow-related initialization.

  • private PrefabSystem m_PrefabSystem
    Reference to the PrefabSystem obtained from the World. Used to resolve a TerrainPropertiesPrefab instance from the singleton terrain properties entity.

Properties

  • This class exposes no public properties. All state is held in private fields and manipulated in lifecycle methods.

Constructors

  • public TerrainInitializeSystem()
    Default constructor. The system uses the standard GameSystemBase lifecycle and performs initialization in OnCreate rather than the constructor. The constructor is empty in the source.

Methods

  • protected override void OnCreate() : System.Void
    Called when the system is created. What it does:
  • Calls base.OnCreate().
  • Caches references to required systems via base.World.GetOrCreateSystemManaged() for PrefabSystem, TerrainSystem, TerrainMaterialSystem, TerrainRenderSystem, WaterSystem, WaterRenderSystem, and SnowSystem.
  • Creates two EntityQuery objects:
    • m_TerrainPropertiesQuery: ReadOnly Created and TerrainPropertiesData.
    • m_TerrainMaterialPropertiesQuery: ReadOnly Created and TerrainMaterialPropertiesData.
  • Calls RequireForUpdate(m_TerrainPropertiesQuery) so the system will only update when the terrain properties singleton is present.
  • Note: methods and fields are marked with [Preserve] to avoid stripping, and the class has a [FormerlySerializedAs] attribute indicating a previous serialized name.

  • protected override void OnUpdate() : System.Void
    Called every frame when the system is enabled to run (i.e., when the Required query has matches). What it does:

  • Gets the singleton entity matched by m_TerrainPropertiesQuery.
  • Uses m_PrefabSystem.GetPrefab(singletonEntity) to obtain the TerrainPropertiesPrefab instance associated with that entity.
  • Applies configuration from the prefab to systems; specifically sets m_WaterSystem.MaxSpeed = prefab.m_WaterMaxSpeed.
  • Note: currently this OnUpdate only sets water maximum speed; other cached systems are not modified here but are available for further initialization.

Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    // Cache systems and create queries just like the original
    m_PrefabSystem = base.World.GetOrCreateSystemManaged<PrefabSystem>();
    m_WaterSystem = base.World.GetOrCreateSystemManaged<WaterSystem>();
    m_TerrainPropertiesQuery = GetEntityQuery(ComponentType.ReadOnly<Created>(), ComponentType.ReadOnly<TerrainPropertiesData>());
    RequireForUpdate(m_TerrainPropertiesQuery);
}

[Preserve]
protected override void OnUpdate()
{
    // Read terrain prefab and apply its settings.
    Entity singletonEntity = m_TerrainPropertiesQuery.GetSingletonEntity();
    TerrainPropertiesPrefab prefab = m_PrefabSystem.GetPrefab<TerrainPropertiesPrefab>(singletonEntity);

    // Use the prefab value, or override with a mod-provided value
    m_WaterSystem.MaxSpeed = prefab.m_WaterMaxSpeed;
    // Example override:
    // m_WaterSystem.MaxSpeed = Mathf.Max(prefab.m_WaterMaxSpeed, 12.0f);
}

Additional notes for modders: - RequireForUpdate(m_TerrainPropertiesQuery) ensures this system doesn't run until the terrain properties singleton exists. If you need the system to run earlier, change or remove that requirement. - The PrefabSystem is used to fetch prefab data associated with an entity; terrain configuration values are typically stored in such prefabs. - Several systems are cached in OnCreate for convenience; if you add initialization logic (material, render, snow, etc.) you should do it in OnCreate or OnUpdate depending on timing needs. - Attributes: - [Preserve] prevents the method/class from being stripped by linkers/IL2CPP optimization. - [FormerlySerializedAs] indicates a previous type name for backwards compatibility with serialized data.