Game.CompanyDividendSystem
Assembly:
Assembly-CSharp (game runtime assembly)
Namespace: Game.Simulation
Type:
class
Base:
GameSystemBase
Summary:
CompanyDividendSystem is a simulation system that distributes periodic "dividends" (money) from processing companies to the households of their employees. It scans processing companies (excluding storage companies), computes a per-employee dividend based on the company's money and number of employees, enqueues payments to be applied to household resources, and then applies those payments via a second job. The system uses Unity DOTS jobs (IJobChunk + a single-threaded IJob that dequeues) and a NativeQueue for thread-safe handshake between jobs. It depends on SimulationSystem and ResourceSystem and runs at a frequency determined by kUpdatesPerDay.
Fields
-
private struct Dividend { public int m_Amount; public Entity m_Receiver; }
Represents a single payment: amount of money and the target entity (household) that should receive it. Instances are enqueued by the chunk job and dequeued by the processor job. -
private static readonly int kUpdatesPerDay = 1
Number of dividend update cycles per in-game day. Used to compute the system's update interval. -
private EntityQuery m_EconomyParameterQuery
Query used to fetch the EconomyParameterData singleton required by the job. -
private EntityQuery m_CompanyQuery
Query selecting processing companies to consider for dividend payments. The query requires ProcessingCompany + Resources + Employee buffer + PrefabRef + UpdateFrame and excludes StorageCompany, Deleted, and Temp. -
private SimulationSystem m_SimulationSystem
Reference to the SimulationSystem (used to get the current simulation frame). -
private ResourceSystem m_ResourceSystem
Reference to ResourceSystem (provides resource prefabs and reader registration). -
private NativeQueue<Dividend> m_DividendQueue
A persistent NativeQueue used to collect Dividend items from the parallel chunk job and then consumed by the single-threaded ProcessDividendsJob. -
private TypeHandle __TypeHandle
Internal struct holding EntityTypeHandle, ComponentTypeHandles, BufferTypeHandles and ComponentLookup/BufferLookup instances used by the jobs. Initialized in OnCreateForCompiler.
Properties
- None (no public properties are declared on this system).
Constructors
public CompanyDividendSystem()
Default constructor. No custom initialization beyond default behavior. The system performs setup in OnCreate.
Methods
-
public override int GetUpdateInterval(SystemUpdatePhase phase)
Returns the update interval (in simulation ticks) for this system. Implementation:return 262144 / (kUpdatesPerDay * 16);
With kUpdatesPerDay == 1 the computed interval is 262144 / 16 = 16384. This controls how often the system's scheduled work runs with respect to the engine's update scheduling. -
protected override void OnCreate()
Initializes the system: - Resolves SimulationSystem and ResourceSystem.
- Creates a persistent NativeQueue
. - Builds EntityQueries (m_EconomyParameterQuery and m_CompanyQuery).
-
Calls RequireForUpdate on both queries to prevent the system from updating if the required data isn't present.
-
protected override void OnDestroy()
Disposes the persistent NativeQueue and calls base.OnDestroy() to perform standard cleanup. -
protected override void OnUpdate()
Schedules the work for each update: - Computes the current update frame index using SimulationUtils.GetUpdateFrame with kUpdatesPerDay and stride 16.
- Schedules DividendJob as a parallel IJobChunk to iterate companies matching m_CompanyQuery. DividendJob:
- Filters chunks by shared UpdateFrame matching the current update frame.
- For each company chunk, reads its Resources buffer and Employee buffer.
- Skips companies with no employees or negative money.
- Computes per-employee dividend: num = max(0, companyMoney / (8 * employeeCount)).
- Enqueues a Dividend per employee's household (using HouseholdMember lookup) with m_Amount = num and m_Receiver = household.
- Subtracts total payout from the company's Resources buffer.
- Registers job as a reader with ResourceSystem (m_ResourceSystem.AddPrefabsReader).
- Schedules ProcessDividendsJob (IJob) which dequeues all Dividend items and, for each, checks the target has a Resources buffer and adds money to that buffer using EconomyUtils.AddResources(Resource.Money, amount, resources).
-
Assigns resulting JobHandle to base.Dependency.
-
private void __AssignQueries(ref SystemState state)
Internal stub used by compiler-generated path. (In this file it creates and disposes an EntityQueryBuilder; actual handle assignments are done in __AssignHandles on the TypeHandle.) -
protected override void OnCreateForCompiler()
Called during compilation-time initialization; calls base and assigns type handles by invoking __AssignQueries and __TypeHandle.__AssignHandles(ref base.CheckedStateRef). Prepares the TypeHandle values used when scheduling jobs.
Nested job types (summarized):
- ProcessDividendsJob : IJob
(Burst compiled)
- Fields: BufferLookup
- Execute(): Dequeues all Dividend entries and applies them to the receiver's Resources buffer if present.
DividendJob : IJobChunk
(Burst compiled)- Large set of handles and lookups (EntityTypeHandle, SharedComponentTypeHandle
, BufferTypeHandle , BufferTypeHandle , lookups for HouseholdMember, DeliveryTruck, ResourceData, etc.) -
Execute(in ArchetypeChunk chunk, ...): Performs the per-company dividend computation and enqueues Dividend items for each employee's household.
-
TypeHandle
(private struct) - Holds all type handles / lookups used by the jobs and provides __AssignHandles(ref SystemState state) to initialize them.
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
// Typical initialization performed by the system:
m_SimulationSystem = World.GetOrCreateSystemManaged<SimulationSystem>();
m_ResourceSystem = World.GetOrCreateSystemManaged<ResourceSystem>();
m_DividendQueue = new NativeQueue<Dividend>(Allocator.Persistent);
m_EconomyParameterQuery = GetEntityQuery(ComponentType.ReadOnly<EconomyParameterData>());
m_CompanyQuery = GetEntityQuery(
ComponentType.ReadOnly<Game.Companies.ProcessingCompany>(),
ComponentType.ReadWrite<Resources>(),
ComponentType.ReadOnly<Employee>(),
ComponentType.ReadOnly<PrefabRef>(),
ComponentType.ReadOnly<UpdateFrame>(),
ComponentType.Exclude<Game.Companies.StorageCompany>(),
ComponentType.Exclude<Deleted>(),
ComponentType.Exclude<Temp>());
RequireForUpdate(m_CompanyQuery);
RequireForUpdate(m_EconomyParameterQuery);
}
Notes and implementation details:
- The dividend amount calculation is intentionally conservative: per-employee portion is companyMoney / (8 * employeeCount). This reduces the fraction of company funds paid out each cycle.
- The system enqueues Dividend structs from the parallel IJobChunk and then processes them in a single-threaded IJob to safely mutate target DynamicBuffer