Skip to content

Game.Simulation.ResourceExporterSystem

Assembly: Game
Namespace: Game.Simulation

Type: class

Base: GameSystemBase

Summary:
ResourceExporterSystem manages exporting of resources from producers (entities with a ResourceExporter component) to storage companies. It: - Scans exporters and either schedules pathfinding to find export targets or produces export events for already routed exporters. - Enqueues pathfinding requests (via PathfindSetupSystem) and uses an EndFrameBarrier command buffer to add/remove components safely from jobs. - Processes completed export events to adjust resources, update trade/transport cost estimates, and deduct sold quantities. - Uses two burst-compiled jobs: ExportJob (IJobChunk) to evaluate exporters and enqueue path/pathfind/setup operations, and HandleExportsJob (IJob) to consume the export queue and apply economic changes. - Relies on ResourceSystem for prefab data, TaxSystem for readers, and tight integration with Component/Buffer lookups and NativeQueue.


Fields

  • private EntityQuery m_ExporterQuery
    Holds the query used to find exporter entities (ResourceExporter, TaxPayer, PropertyRenter, Resources, TripNeeded, excluding ResourceBuyer, Deleted, Temp). The system requires this query for updates.

  • private EntityQuery m_EconomyParameterQuery
    Query ensuring EconomyParameterData is present; used to gate system updates until economy parameters exist.

  • private PathfindSetupSystem m_PathfindSetupSystem
    Reference to the PathfindSetupSystem used to enqueue pathfinding requests (SetupQueueItem). Acquired via World.GetOrCreateSystemManaged.

  • private EndFrameBarrier m_EndFrameBarrier
    Reference to an EndFrameBarrier used to create a parallel command buffer for safe structural changes from jobs.

  • private ResourceSystem m_ResourceSystem
    Reference to ResourceSystem used to access resource prefabs and register prefabs readers for job synchronization.

  • private TaxSystem m_TaxSystem
    Reference to TaxSystem used to register a reader dependency when scheduling jobs that touch economic data.

  • private NativeQueue<ExportEvent> m_ExportQueue
    NativeQueue used to collect export events produced by ExportJob and consumed by HandleExportsJob. Allocated with Allocator.Persistent and disposed in OnDestroy.

  • private TypeHandle __TypeHandle
    Internal aggregated handles (EntityTypeHandle, ComponentTypeHandle, BufferTypeHandle, ComponentLookup, etc.) used for job scheduling and lookups. Prepared in OnCreateForCompiler via __AssignHandles.

Properties

  • This system does not expose public properties.

Constructors

  • public ResourceExporterSystem()
    Default constructor. Marked with [Preserve] (via class-level usage) in original source; no custom initialization beyond base ctor. Primary initialization happens in OnCreate.

Methods

  • public override int GetUpdateInterval(SystemUpdatePhase phase)
    Returns the update interval used by this system. Implementation returns 16, indicating the system updates every 16 ticks/frames (as used by the game's system scheduler).

  • protected override void OnCreate()
    Initializes system references and resources:

  • Gets or creates PathfindSetupSystem, EndFrameBarrier, ResourceSystem and TaxSystem from the World.
  • Builds m_ExporterQuery and m_EconomyParameterQuery.
  • Allocates m_ExportQueue (NativeQueue with Allocator.Persistent).
  • Calls RequireForUpdate on the exporter and economy-parameter queries to ensure the system only updates when relevant data exists.

  • protected override void OnDestroy()
    Disposes the NativeQueue m_ExportQueue and calls base.OnDestroy().

  • protected override void OnUpdate()
    Schedules and coordinates the two primary pieces of work:

  • Constructs and schedules the ExportJob (IJobChunk) which:
    • Iterates exporter chunks, examines TripNeeded buffer and PathInformation, removes ResourceExporter or schedules pathfinding as needed, enqueues ExportEvent into m_ExportQueue, and adds TripNeeded entries for exporting trips.
    • Uses a parallel EndFrameBarrier command buffer to add/remove components from within the job and pushes pathfind setup items into PathfindSetupSystem's queue.
  • Schedules HandleExportsJob (IJob) which consumes m_ExportQueue and:
    • Computes amounts actually sold, updates resources on the seller, adjusts trade costs based on transport cost and distance, and adjusts seller/buyer trade cost buffers.
  • Adds required synchronization dependencies: registers the resource prefabs reader, EndFrameBarrier producer handle, and PathfindSetupSystem queue writer; also registers TaxSystem reader for economic data.

  • private void __AssignQueries(ref SystemState state)
    Internal method invoked by OnCreateForCompiler (empty in this implementation except for a temporary EntityQueryBuilder dispose). Present for compiler-generated initialization paths.

  • protected override void OnCreateForCompiler()
    Compiler-time helper: assigns query handles and calls __TypeHandle.__AssignHandles to prepare type handles for use by jobs.

Notes about nested/inner types: - ExportJob (private struct, BurstCompile, IJobChunk) — iterates exporters and either enqueues pathfinding or creates ExportEvent entries. Contains helper FindTarget that sets pending PathInformation, adds PathElement buffer, and enqueues a SetupQueueItem for pathfinding to a resource export target. - HandleExportsJob (private struct, BurstCompile, IJob) — dequeues ExportEvent entries and applies economic effects (resource reduction, price/transport cost updates) and updates TradeCost buffers. - ExportEvent (private struct) — simple container carrying resource, seller/buyer entities, amount and distance used between the two jobs. - TypeHandle (private struct) — groups required type handles/lookups and provides __AssignHandles to fetch them from a SystemState.

Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    m_PathfindSetupSystem = base.World.GetOrCreateSystemManaged<PathfindSetupSystem>();
    m_EndFrameBarrier = base.World.GetOrCreateSystemManaged<EndFrameBarrier>();
    m_ResourceSystem = base.World.GetOrCreateSystemManaged<ResourceSystem>();
    m_TaxSystem = base.World.GetOrCreateSystemManaged<TaxSystem>();
    m_ExporterQuery = GetEntityQuery(
        ComponentType.ReadOnly<ResourceExporter>(),
        ComponentType.ReadOnly<TaxPayer>(),
        ComponentType.ReadOnly<PropertyRenter>(),
        ComponentType.ReadOnly<Game.Economy.Resources>(),
        ComponentType.Exclude<ResourceBuyer>(),
        ComponentType.ReadWrite<TripNeeded>(),
        ComponentType.Exclude<Deleted>(),
        ComponentType.Exclude<Temp>());
    m_EconomyParameterQuery = GetEntityQuery(ComponentType.ReadOnly<EconomyParameterData>());
    m_ExportQueue = new NativeQueue<ExportEvent>(Allocator.Persistent);
    RequireForUpdate(m_ExporterQuery);
    RequireForUpdate(m_EconomyParameterQuery);
}

Notes and developer tips: - The system uses an internal NativeQueue to communicate between a parallel chunk job (producer) and a single IJob consumer; ensure proper disposal of the queue if copying or reusing patterns. - EndFrameBarrier is used to get a ParallelWriter EntityCommandBuffer for safe structural changes from the ExportJob. If modifying component add/remove logic, respect the chunkIndex parameter passed to EB command buffer writer. - PathfindSetupSystem queue writes are enqueued as parallel writers; ensure capacity and synchronization (AddQueueWriter/AddPrefabsReader/AddJobHandleForProducer are used to coordinate dependencies). - The export logic references ResourcePrefabs and ResourceData to compute weights/prices; if adding new resources or changing prefabs ensure ResourceSystem readers are registered so jobs see up-to-date prefab arrays.