Skip to content

Game.Net.CostSystem

Assembly: Assembly-CSharp
Namespace: Game.Net

Type: class

Base: GameSystemBase

Summary:
CostSystem is a game system responsible for calculating construction, upgrade and removal costs for temporary network entities (roads/paths/edges and their subobjects) during simulation. It schedules a Burst-compiled IJobChunk (CalculateCostJob) that iterates matching Temp entities (edges/nodes) and computes: - construction cost for new/modified placeable nets via Curve/Edge/Elevation/PlaceableNetComposition data, - costs of placeable subobjects (including trees with special calculation), - refunds and upgrade costs using Recent data, EconomyParameterData and simulation frame index, - uses PrefabRef / PlaceableObjectData and Owner checks to decide which subobjects contribute.

The system only runs while the tool action mode is not an editor mode. It requires EconomyParameterData and Updated+Temp (with Edge or Node) entities to exist. A compiler-generated TypeHandle struct is used to store ComponentTypeHandles, ComponentLookup and BufferLookup used when scheduling the job.


Fields

  • private ToolSystem m_ToolSystem
    Reference to the world's ToolSystem (used to check the current action mode, e.g., editor vs play).

  • private SimulationSystem m_SimulationSystem
    Reference to SimulationSystem used to obtain the current simulation frame index (frameIndex) for refund/upgrade calculations.

  • private EntityQuery m_UpdatedTempNetQuery
    EntityQuery that matches entities with Updated and Temp components and either Edge or Node. This is the main query scheduled by the job.

  • private EntityQuery m_EconomyParameterQuery
    EntityQuery that retrieves the singleton EconomyParameterData required for cost calculations. Required for update so system only runs when economy data is available.

  • private TypeHandle __TypeHandle
    Compiler-generated helper struct that holds all ComponentTypeHandle, BufferTypeHandle and ComponentLookup/BufferLookup instances used by the CalculateCostJob. It is initialized in OnCreateForCompiler and assigned from the SystemState when scheduling.

Properties

  • None (no public properties exposed by this system)

Constructors

  • public CostSystem()
    Default constructor (marked [Preserve] in the source). The system is created by the ECS world; initialization is performed in OnCreate / OnCreateForCompiler.

Methods

  • protected override void OnCreate()
    Initializes references and entity queries:
  • Acquires ToolSystem and SimulationSystem from the World.
  • Builds m_UpdatedTempNetQuery to match Updated + Temp and (Edge | Node).
  • Builds m_EconomyParameterQuery for the EconomyParameterData singleton.
  • Calls RequireForUpdate on both queries so the system only runs when prerequisites exist.

  • protected override void OnUpdate()
    Main update: if the current tool action mode is not editor, it constructs a CalculateCostJob, populates all ComponentTypeHandle / ComponentLookup / BufferLookup fields (via InternalCompilerInterface + the stored __TypeHandle), sets m_EconomyParameterData and m_SimulationFrame, and schedules the job in parallel against m_UpdatedTempNetQuery. Job scheduling appends to the system's base.Dependency.

  • private void __AssignQueries(ref SystemState state)
    Compiler-generated helper (AggressiveInlining). In this build it constructs and disposes an EntityQueryBuilder; effectively a placeholder for any required query assignment for code-gen compatibility.

  • protected override void OnCreateForCompiler()
    Called to perform compiler-time initialization: calls __AssignQueries and __TypeHandle.__AssignHandles to populate handles used by the job when scheduling.

Nested/Generated types of note (described here because they are critical to the system's behavior):

  • CalculateCostJob (private BurstCompile struct, implements IJobChunk)
  • Purpose: core per-chunk logic that computes Temp.m_Cost and Temp.m_Value.
  • Key fields passed in: EntityTypeHandle, ComponentTypeHandle, BufferTypeHandle, ComponentLookup for Curve/Edge/Owner/Composition/Elevation/Tree/Recent/PrefabRef/PlaceableNetComposition/PlaceableObjectData, BufferLookup, m_SimulationFrame, m_EconomyParameterData.
  • High-level behavior:

    • For each Temp entity in the chunk:
    • Zero out m_Cost and m_Value initially.
    • If TempFlags.Essential present, compute construction and existing values:
      • For network composition changes (create/modify/replace/upgrade/remove-cost), compute construction cost differences using NetUtils.GetConstructionCost for new and original curves/compositions and elevation of edge endpoints.
      • Iterate local subobjects buffer (if present) and original entity subobjects (via m_SubObjects buffer lookup) to add placeable object construction costs; adjust for trees via ObjectUtils.GetContructionCost when Tree component present.
    • Determine m_Cost based on flags:
      • RemoveCost: cost is negative of original cost (and add new cost if not deleting).
      • Delete: if not hidden and Recent exists, refund using NetUtils.GetRefundAmount (negative).
      • Upgrade or default: compute upgrade cost using NetUtils.GetUpgradeCost with num2 (new) and num (old), and Recent + simulation frame/economy parameters if available.
    • Write computed Temp back to component array.
  • TypeHandle (private struct)
    Contains read-only/cached handles (EntityTypeHandle, ComponentTypeHandle reads, BufferTypeHandle reads, ComponentLookup, BufferLookup, and a mutable ComponentTypeHandle) and an __AssignHandles(ref SystemState) method to populate them from a SystemState.

Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    // Acquire needed systems
    m_ToolSystem = base.World.GetOrCreateSystemManaged<ToolSystem>();
    m_SimulationSystem = base.World.GetOrCreateSystemManaged<SimulationSystem>();

    // Query for updated Temp nets (Updated + Temp) and either Edge or Node
    m_UpdatedTempNetQuery = GetEntityQuery(new EntityQueryDesc {
        All = new ComponentType[] {
            ComponentType.ReadOnly<Updated>(),
            ComponentType.ReadOnly<Temp>()
        },
        Any = new ComponentType[] {
            ComponentType.ReadOnly<Edge>(),
            ComponentType.ReadOnly<Node>()
        }
    });

    // Query for economy parameters (singleton)
    m_EconomyParameterQuery = GetEntityQuery(ComponentType.ReadOnly<EconomyParameterData>());

    RequireForUpdate(m_EconomyParameterQuery);
    RequireForUpdate(m_UpdatedTempNetQuery);
}

{{ Additional notes: - This system is performance-sensitive: cost calculation is performed in a Burst-compiled job over chunks and uses BufferAccessor / BufferLookup, ComponentLookup and ComponentTypeHandle patterns common to Unity DOTS/IJobChunk code. - If you want to modify cost behavior in a mod, you can: - Hook into NetUtils or ObjectUtils (if available) to change formulas, - Replace or intercept EconomyParameterData or Recent data manipulation, - Or provide your own system that runs earlier/later and modifies Temp components before CostSystem runs (keep in mind scheduling and dependencies). - Be careful when altering queries/handles: __TypeHandle and OnCreateForCompiler are compiler-generated helper patterns used by the original build; changes to the component handle setup must be reflected when scheduling jobs. }}