Game.CountConsumptionSystem
Assembly: Assembly-CSharp
Namespace: Game.Simulation
Type: class
Base: GameSystemBase, IDefaultSerializable, ISerializable
Summary:
Counts and smooths per-resource consumption statistics for the economy. The system keeps two NativeArray
Fields
-
private System.Diagnostics.Stopwatch m_Stopwatch
Used for local timing/diagnostics if needed (declared but not used in this file). Present as a private field likely reserved for profiling or future timing measurements. -
private Unity.Jobs.JobHandle <producerHandle>k__BackingField
Auto-generated backing field for an internal/auto property (not exposed here). Holds a JobHandle used to track producer job dependencies. -
public static readonly int kUpdatesPerDay = 32
Constant that defines how many updates are considered to represent a "day" for consumption scaling and smoothing. Used in the smoothing math and when deriving the system update interval. -
private NativeArray<int> m_Consumptions
Holds the smoothed/ready consumption values for each resource. Allocated with Allocator.Persistent in OnCreate using EconomyUtils.ResourceCount as length. Values are updated by CopyConsumptionJob and read by other systems via GetConsumptions. -
private NativeArray<int> m_ConsumptionAccumulator
Accumulator array where writers add instantaneous consumption events. On each CopyConsumptionJob execution the accumulator values are used to update m_Consumptions and then zeroed out. -
private JobHandle m_ReadDeps
Accumulated job dependencies for jobs that read consumption data. Updated via AddConsumptionReader to ensure proper synchronization. -
private JobHandle m_WriteDeps
Accumulated job dependencies for jobs that write to the accumulator. Updated via AddConsumptionWriter and used when scheduling the copy job to combine write dependencies. -
private JobHandle m_CopyDeps
JobHandle produced by the CopyConsumptionJob schedule. Exposed to callers via GetConsumptions so they can depend on the latest copy completion.
Properties
public Unity.Jobs.JobHandle producerHandle { get; private set }
Backed byk__BackingField. Exposes a JobHandle used by producers to chain dependencies. The setter is private; other systems can potentially read this handle to chain work.
Constructors
public CountConsumptionSystem()
Default constructor. The real initialization happens in OnCreate (marked with Preserve). The constructor itself is empty in this implementation.
Methods
-
public override int GetUpdateInterval(SystemUpdatePhase phase)
Returns 262144 / kUpdatesPerDay. With the default kUpdatesPerDay of 32 this results in an update interval of 8192. This controls how often the system's OnUpdate runs relative to the simulation tick scheduling logic. -
public NativeArray<int> GetConsumptions(out JobHandle deps)
Returns the m_Consumptions NativeArray and outputs the m_CopyDeps JobHandle via the out parameter. Callers should use the returned deps to ensure they wait on the copy job before reading the array. -
public NativeArray<int> GetConsumptionAccumulator(out JobHandle deps)
Returns the m_ConsumptionAccumulator NativeArray and outputs the m_WriteDeps JobHandle. Writers should combine their job with the returned deps to ensure proper synchronization. -
public void AddConsumptionReader(JobHandle deps)
Combines the provided deps into m_ReadDeps using JobHandle.CombineDependencies. Use this when scheduling a job that reads the consumptions to ensure the system's copy job waits for the reader if needed. -
public void AddConsumptionWriter(JobHandle deps)
Combines the provided deps into m_WriteDeps using JobHandle.CombineDependencies. Use this when scheduling a job that writes to the accumulator so the copy job runs after writers finish. -
protected override void OnCreate()
Allocates the NativeArray buffers: - m_Consumptions = new NativeArray
(EconomyUtils.ResourceCount, Allocator.Persistent) -
m_ConsumptionAccumulator = new NativeArray
(EconomyUtils.ResourceCount, Allocator.Persistent) Marked with [Preserve] so it is retained for reflection/serialization. -
protected override void OnDestroy()
Disposes both NativeArray buffers (m_ConsumptionAccumulator and m_Consumptions) and calls base.OnDestroy. Ensures no NativeArray leaks. -
protected override void OnStopRunning()
Calls base.OnStopRunning. Present but contains no additional logic. -
protected override void OnUpdate()
Creates and schedules the Burst-compiled CopyConsumptionJob which: - Reads m_ConsumptionAccumulator and m_Consumptions
-
Produces smoothed m_Consumptions from the accumulator and resets the accumulator to 0 The job is scheduled with JobHandle.CombineDependencies(m_ReadDeps, m_WriteDeps) to respect reader/writer dependencies, then base.Dependency is set to that job handle. m_CopyDeps and m_WriteDeps are updated to the returned dependency so subsequent readers/writers can chain correctly.
-
public void SetDefaults(Context context)
Zeros both arrays (m_ConsumptionAccumulator and m_Consumptions) across their entire length. Typically called to initialize or reset state. -
public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
Writes both m_ConsumptionAccumulator and m_Consumptions into the writer. Uses direct writer.Write of NativeArray. -
public void Deserialize<TReader>(TReader reader) where TReader : IReader
Reads consumption arrays from the reader. Behavior differs based on save format: - If reader.context.format.Has(FormatTags.FishResource) is true, reads full arrays into m_ConsumptionAccumulator and m_Consumptions.
-
Otherwise (older format without the extra resource), reads only the first 40 elements into subarrays and sets element 40 (index 40) of both arrays to 0. This keeps compatibility with older saves that do not include the newer "fish" resource.
-
private struct CopyConsumptionJob : IJob [BurstCompile]
- Fields:
- public NativeArray
m_Accumulator - public NativeArray
m_Consumptions
- public NativeArray
- Execute():
- Iterates all indices and updates m_Consumptions[i] as:
- If previous m_Consumptions[i] == 0: m_Consumptions[i] = m_Accumulator[i]
- Else: m_Consumptions[i] = RoundToInt(kUpdatesPerDay * lerp(m_Consumptions[i] / kUpdatesPerDay, m_Accumulator[i], 0.3f))
- Resets m_Accumulator[i] to 0.
- Notes: Burst-compiled, uses math.lerp for smoothing and Mathf.RoundToInt for integer rounding. The calculation effectively performs exponential-like smoothing and keeps consumptions scaled relative to kUpdatesPerDay.
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
m_Stopwatch = new Stopwatch();
}
Additional notes: - Arrays are allocated with Allocator.Persistent; be sure to Dispose in OnDestroy to avoid memory leaks. - Use AddConsumptionReader/AddConsumptionWriter to properly chain job dependencies when reading from or writing to the system's arrays. - The Deserialize method contains compatibility code for older save formats (FormatTags.FishResource). When modding, be mindful of resource count changes and save format tags.