Skip to content

Game.Prefabs.ResourceSystem

Assembly: Game
Namespace: Game.Prefabs

Type: class

Base: GameSystemBase

Summary:
ResourceSystem is an ECS system responsible for initializing and tracking resource prefabs and resource info entities. It reads ResourcePrefab data from the PrefabSystem and writes their properties into ResourceData components on prefab entities, creates corresponding ResourceInfo entities, tracks an array of resource prefab Entities and resource info Entities indexed by resource type, accumulates a base consumption sum for use by household/consumption logic, and exposes helper methods to query prefabs and register job dependencies for safe reading of the prefab arrays. The system also handles lifecycle (allocation and disposal) of internal NativeArray storage and synchronizes with job handles used by other systems.


Fields

  • private EntityQuery m_PrefabGroup
    Stores the query matching created prefab entities that have PrefabData and ResourceData. Used to iterate prefab archetype chunks and initialize ResourceData from ResourcePrefab.

  • private EntityQuery m_InfoGroup
    Query matching Created + ResourceInfo entities. Used to discover existing ResourceInfo entities and ensure m_ResourceInfos is up-to-date.

  • private PrefabSystem m_PrefabSystem
    Reference to the PrefabSystem (fetched from the World) used to lookup concrete ResourcePrefab data for a given PrefabData.

  • private NativeArray<Entity> m_ResourcePrefabs
    Persistent NativeArray sized to EconomyUtils.ResourceCount that stores the Entity for each resource's prefab (indexed by resource index). Allocated with Allocator.Persistent in OnCreate and disposed in OnDestroy.

  • private NativeArray<Entity> m_ResourceInfos
    Persistent NativeArray sized to EconomyUtils.ResourceCount that stores the Entity for each resource's ResourceInfo. Allocated with Allocator.Persistent in OnCreate and disposed in OnDestroy.

  • private JobHandle m_PrefabsReaders
    Aggregated JobHandle of jobs that read the prefab arrays. The system completes this handle before writing to arrays and allows other jobs to register themselves as readers via AddPrefabsReader to ensure proper dependency chaining.

  • private int m_BaseConsumptionSum
    Cached integer representing the computed sum of base consumption weights used by household behavior. Exposed via the BaseConsumptionSum property.

  • private TypeHandle __TypeHandle
    Internal compiler-generated struct containing EntityTypeHandle and ComponentTypeHandle instances used for chunk access. It is assigned via __AssignHandles(ref SystemState) in OnCreateForCompiler.

  • private struct TypeHandle
    Contains:

  • EntityTypeHandle __Unity_Entities_Entity_TypeHandle (ReadOnly)
  • ComponentTypeHandle<PrefabData> __Game_Prefabs_PrefabData_RO_ComponentTypeHandle (ReadOnly)
  • ComponentTypeHandle<ResourceData> __Game_Prefabs_ResourceData_RW_ComponentTypeHandle (Read/Write)
  • ComponentTypeHandle<ResourceInfo> __Game_Economy_ResourceInfo_RO_ComponentTypeHandle (ReadOnly)
  • void __AssignHandles(ref SystemState state) — assigns all handles from the provided SystemState (called during system creation/compilation path).

Properties

  • public int BaseConsumptionSum { get; }
    Read-only property that returns the computed base consumption sum (m_BaseConsumptionSum). This value is updated during OnUpdate after initializing ResourceData for all resource prefabs and calculating a weighted sum used by household behavior logic.

Constructors

  • public ResourceSystem()
    Default parameterless constructor. The system performs initialization work in OnCreate rather than the constructor.

Methods

  • [Preserve] protected override void OnCreate()
    Initializes the system:
  • Retrieves the PrefabSystem from the World.
  • Creates two EntityQuery instances:
    • m_PrefabGroup: matches Created + PrefabData + ResourceData (prefab entities to initialize).
    • m_InfoGroup: matches Created + ResourceInfo.
  • Allocates m_ResourcePrefabs and m_ResourceInfos as persistent NativeArrays sized to EconomyUtils.ResourceCount.
  • Prepares other internal state. The TypeHandle assignment is performed in OnCreateForCompiler (compiler path method).

  • [Preserve] protected override void OnDestroy()
    Tears down the system:

  • Completes any outstanding m_PrefabsReaders to ensure no jobs are still using the prefab arrays.
  • Disposes m_ResourcePrefabs and m_ResourceInfos.
  • Calls base.OnDestroy().

  • [Preserve] protected override void OnUpdate()
    Main update logic:

  • Completes m_PrefabsReaders and resets it.
  • If there are prefab entities in m_PrefabGroup:
    • Iterates chunks, reads PrefabData and ResourceData components and the corresponding ResourcePrefab via m_PrefabSystem.
    • Copies many fields from ResourcePrefab into ResourceData (flags such as m_IsMaterial, m_IsProduceable, weights, price, base consumption, age weights, car consumption, temperature requirements, needed work per unit, etc).
    • Accumulates a weighted base consumption value using HouseholdBehaviorSystem.GetWeight and stores the rounded integer into m_BaseConsumptionSum.
    • For each resource prefab encountered, if the resource index slot in m_ResourcePrefabs is empty, sets it and creates a corresponding ResourceInfo entity via an EntityCommandBuffer; fills the ResourceInfo and adds Created.
    • Plays back the ECB to apply created ResourceInfo entities to the EntityManager.
  • If there are ResourceInfo entities in m_InfoGroup:
    • Iterates chunks of ResourceInfo entities and ensures m_ResourceInfos entries reflect any externally created ResourceInfo entities (keeps array synchronized even if ResourceInfo entities were created elsewhere).
  • Disposes native chunk arrays used for iteration.

  • public ResourcePrefabs GetPrefabs()
    Returns a ResourcePrefabs wrapper constructed with m_ResourcePrefabs (likely a small struct exposing the NativeArray safely), allowing other systems to access the prefab array (read-only semantics should be followed externally).

  • public void AddPrefabsReader(JobHandle handle)
    Combines the provided handle with m_PrefabsReaders via JobHandle.CombineDependencies. Used by background jobs that read the prefab arrays to register themselves so the system knows to complete those jobs before writing.

  • public Entity GetPrefab(Resource resource)
    Returns the Entity for the prefab corresponding to the provided Resource enum/value by indexing m_ResourcePrefabs with EconomyUtils.GetResourceIndex(resource). If not initialized, the array entry may be Entity.Null.

  • [MethodImpl(MethodImplOptions.AggressiveInlining)] private void __AssignQueries(ref SystemState state)
    Compiler/IL-generated placeholder used during the codegen path; the implementation in the decompiled source only contains a temporary EntityQueryBuilder disposal. The real handle assignments are done in __TypeHandle.__AssignHandles and OnCreateForCompiler.

  • protected override void OnCreateForCompiler()
    Compiler helper that calls __AssignQueries and __TypeHandle.__AssignHandles(ref base.CheckedStateRef). Ensures type handles are assigned in the compiled path.

  • private void TypeHandle.__AssignHandles(ref SystemState state)
    Assigns all EntityTypeHandle and ComponentTypeHandle fields from the provided SystemState; used by InternalCompilerInterface.GetXXXTypeHandle helpers in OnUpdate.

Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();

    // Example: after this system runs, read the computed base consumption sum:
    // (In practice you would read this from another system after ResourceSystem.OnUpdate has executed)
    int baseConsumption = BaseConsumptionSum;

    // Example: obtain a prefab entity for a resource (Resource is the game's resource enum)
    // JobHandle handle = default; // some job handle that reads prefab arrays
    // AddPrefabsReader(handle);   // register the job as a reader so this system will wait before writing

    // Example usage retrieving a specific resource prefab (Resource enum must be in scope)
    // Entity waterPrefab = GetPrefab(Resource.Water);
}