Skip to content

Game.Tools.UpgradeDeletedSystem

Assembly: Assembly-CSharp
Namespace: Game.Tools

Type: class

Base: GameSystemBase

Summary:
UpgradeDeletedSystem listens for deleted service-upgrade prefabs and processes their effects on their owner entities. It schedules a Burst-compiled IJobChunk (UpgradeDeletedJob) that inspects owners of upgrades, computes clear areas, and (when appropriate) issues creation commands for sub-areas and sub-nets that should be spawned as a consequence of the upgrade being deleted or changed. The job uses terrain and water surface data, prefab geometry data and buffers (sub-areas, sub-nets, installed upgrades, etc.) to decide what must be recreated or updated for the owner entity. All entity-creation/modification is done via a ToolOutputBarrier command buffer (parallel writer) so the work is safe to run as a parallel job.


Fields

  • private TerrainSystem m_TerrainSystem
    Reference to the TerrainSystem used to obtain terrain height data needed to adjust net and area geometry.

  • private WaterSystem m_WaterSystem
    Reference to the WaterSystem used to obtain water surface data for nets that are on water.

  • private ToolOutputBarrier m_ToolOutputBarrier
    Barrier system used to create an EntityCommandBuffer for producing CreationDefinition entities and other updates from the job.

  • private CityConfigurationSystem m_CityConfigurationSystem
    Used to read city configuration flags such as left-hand traffic (affects net geometry).

  • private EntityQuery m_DeletedQuery
    EntityQuery used to require system update. It targets deleted service-upgrade prefabs (ServiceUpgrade + Deleted + Transform, excluding Temp).

  • private TypeHandle __TypeHandle
    Cached struct holding Entity/Component/Buffer/Lookup handles used to initialize and feed the UpgradeDeletedJob. Populated in OnCreateForCompiler / __AssignHandles.

Properties

  • None (this system exposes no public properties).

Constructors

  • public UpgradeDeletedSystem()
    Default constructor. Marked with [Preserve] attribute to avoid stripping. The actual initialization of dependencies and query happens in OnCreate.

Methods

  • protected override void OnCreate()
    Sets up system dependencies: retrieves or creates references to TerrainSystem, WaterSystem, ToolOutputBarrier and CityConfigurationSystem. Builds the EntityQuery (m_DeletedQuery) that controls when this system runs and calls RequireForUpdate to ensure it only runs when matching entities exist.

  • protected override void OnGamePreload(Purpose purpose, GameMode mode)
    Enables or disables the system depending on the current game mode. This implementation sets base.Enabled = mode.IsGame() so the system only runs during normal gameplay.

  • protected override void OnUpdate()
    Main runtime entry. Creates and schedules the Burst-compiled UpgradeDeletedJob as a parallel IJobChunk using the cached type handles and lookups. It obtains terrain height and water surface data (the latter returns a dependent JobHandle), creates a parallel EntityCommandBuffer from m_ToolOutputBarrier, combines dependencies and schedules the job. After scheduling it registers the job as a reader with m_TerrainSystem and m_WaterSystem and adds the job handle to m_ToolOutputBarrier. The job performs most of the logic that inspects owners, computes cleared areas, selects spawnables, creates CreationDefinition entities for sub-areas and nets, and marks owner entities (and nested subobjects) with Updated.

  • protected override void OnCreateForCompiler()
    Internal helper used to assign queries and component type handles for the generated/compiled system code path. Calls __AssignQueries and __TypeHandle.__AssignHandles.

  • private void __AssignQueries(ref SystemState state)
    Small helper used by the generated code path; currently creates and disposes an EntityQueryBuilder (placeholder for code-gen requirements).

  • [Nested] UpgradeDeletedJob (private struct, [BurstCompile], implements IJobChunk)
    This is the worker job scheduled from OnUpdate. Key responsibilities:

  • Iterate archetype chunks that match the required components (Owner, Transform, PrefabRef, etc.).
  • For each owner instance, check if the owner itself is deleted or missing transform data and skip if necessary.
  • Compute "clear areas" from object geometry (sub-areas, sub-nets, installed upgrades) to determine which sub-objects or nets must be recreated or left removed.
  • Handle prefab sub-nets: compute averaged node positions, adjust net curves to terrain/water/lot where relevant (uses NetUtils.AdjustPosition, ObjectUtils.LocalToWorld), and create CreationDefinition + NetCourse entities for nets that should be recreated.
  • Handle prefab sub-areas: select spawnable prefabs (using pseudo-random seeds and placeholder selection logic), check area geometry and clear areas and create CreationDefinition entities containing node buffers for sub-area instantiation.
  • Mark owner entities and their nested subobjects with Updated to ensure they are refreshed.
  • Make use of ComponentLookup, BufferLookup and Native collections (NativeList, NativeParallelHashMap) and disposes them properly before job exit.
  • Uses an EntityCommandBuffer.ParallelWriter to create entities/components from within the parallel job.
  • Contains helper methods:
    • UpdateObject(int jobIndex, Entity entity): recursively marks an entity and its sub-objects with Updated unless the entity is Deleted.
    • CreateSubNet(...): builds CreationDefinition + NetCourse entities for a sub-net, handling water, terrain and lot adjustments and checks against cleared areas.
    • GetOwnerLot(Entity lotOwner, out BuildingUtils.LotInfo lotInfo): attempts to compute lot info for the owner (if the owner is a building lot) to support FlattenTerrain adjustments.

Notes about the job: - It is Burst compiled for performance. - It uses RandomSeed.Next() with per-chunk randomness to select spawnable elements deterministically per frame/chunk. - The job carefully checks ClearAreaHelpers.ShouldClear to avoid creating elements that are overlapped by cleared areas.

Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    // Typical initialization performed by the system:
    m_TerrainSystem = base.World.GetOrCreateSystemManaged<TerrainSystem>();
    m_WaterSystem = base.World.GetOrCreateSystemManaged<WaterSystem>();
    m_ToolOutputBarrier = base.World.GetOrCreateSystemManaged<ToolOutputBarrier>();
    m_CityConfigurationSystem = base.World.GetOrCreateSystemManaged<CityConfigurationSystem>();
    m_DeletedQuery = GetEntityQuery(
        ComponentType.ReadOnly<Game.Buildings.ServiceUpgrade>(),
        ComponentType.ReadOnly<Deleted>(),
        ComponentType.ReadOnly<Transform>(),
        ComponentType.Exclude<Temp>()
    );
    RequireForUpdate(m_DeletedQuery);
}

Additional notes for modders: - UpgradeDeletedSystem is intended to be an internal gameplay system that reacts to deleted service-upgrade prefabs and ensures owner entities (and dependent subobjects) are kept consistent by re-creating required sub-areas and nets where appropriate. - If you need to produce CreationDefinition entities yourself (to spawn prefab sub-objects or nets), follow the pattern in CreateSubNet and the sub-area creation: create an entity, add a CreationDefinition with m_Prefab, m_RandomSeed, m_Owner and CreationFlags.Permanent, then add Updated and any required buffer data such as node positions or Course data. - Be careful when interacting with this system from custom jobs — changes to terrain or water surface data should be coordinated, and command buffers should be produced via a suitable barrier (ToolOutputBarrier) if running in jobs.