Game.Prefabs.Modes.WorkPlaceGlobalMode
Assembly: Game (inferred — replace with actual assembly name if different)
Namespace: Game.Prefabs.Modes
Type: class
Base: EntityQueryModePrefab
Summary:
WorkPlaceGlobalMode is a mode prefab that adjusts the workplaces capacity of building prefabs by applying a global multiplier. It uses a Burst-compiled IJobChunk (ModeJob) to efficiently multiply WorkplaceData.m_MaxWorkers for all matched entities in parallel. The class also records and stores original WorkplaceData values so they can be restored later. It filters entities that have WorkplaceData and BuildingData components and only operates on entities backed by Workplace prefab instances when storing defaults. The restore path falls back to prefab defaults if original data is not available.
Fields
-
public float m_WorkplacesMultiplier
This public field holds the multiplier applied to WorkplaceData.m_MaxWorkers when the mode is applied. Typical values >1 increase workplace capacity; values between 0 and 1 reduce it. The value is copied into the Burst job (ModeJob) during scheduling. -
private Dictionary<Entity, WorkplaceData> m_OriginalWorkplaceData
A runtime dictionary that stores the original WorkplaceData for entities encountered during StoreDefaultData. Used by RestoreDefaultData to restore previously saved values. This dictionary is accessed on the main thread (not from the Burst job) and therefore not thread-safe; it should only be used on the main thread / by the system controlling mode changes.
Properties
- (none)
Constructors
public WorkPlaceGlobalMode()
Default constructor (implicit). No special initialization beyond field defaults. m_OriginalWorkplaceData is initialized in StoreDefaultData.
Methods
public override EntityQueryDesc GetEntityQueryDesc()
Builds and returns an EntityQueryDesc that matches entities containing WorkplaceData and BuildingData (both ReadOnly). This defines which entities the mode will operate on (Workplace-related buildings).
Behavior/notes:
- The query uses ComponentType.ReadOnly
protected override void RecordChanges(EntityManager entityManager, Entity entity)
Records component data access for change tracking. The implementation calls entityManager.GetComponentData(entity). This ensures that the system recognizes that WorkplaceData is relevant to this mode for the given entity.
Behavior/notes: - Used by the prefab/mode system to determine what components may be changed.
public override void StoreDefaultData(EntityManager entityManager, ref NativeArray<Entity> entities, PrefabSystem prefabSystem)
Iterates over the provided entity array and stores the current WorkplaceData for entities that correspond to Workplace prefabs. Initializes m_OriginalWorkplaceData.
Behavior/notes:
- Initializes m_OriginalWorkplaceData = new Dictionary
public override JobHandle ApplyModeData(EntityManager entityManager, EntityQuery requestedQuery, JobHandle deps)
Schedules the Burst-compiled ModeJob using JobChunkExtensions.ScheduleParallel and returns the resulting JobHandle.
Behavior/notes:
- Constructs ModeJob with:
- m_WorkplacesMultiplier copied from the instance field m_WorkplacesMultiplier.
- m_WorkplaceType filled via entityManager.GetComponentTypeHandle
public override void RestoreDefaultData(EntityManager entityManager, ref NativeArray<Entity> entities, PrefabSystem prefabSystem)
Restores WorkplaceData.m_MaxWorkers for the given entities using stored originals or prefab defaults.
Behavior/notes:
- For each entity:
- Reads current WorkplaceData from EntityManager.
- If m_OriginalWorkplaceData contains an entry for the entity, sets componentData.m_MaxWorkers to that stored value.
- Else, if prefabSystem.TryGetPrefab
- Nested type:
ModeJob
(private struct, [BurstCompile], implements IJobChunk) - Fields:
public float m_WorkplacesMultiplier
— multiplier used to scale m_MaxWorkers.public ComponentTypeHandle<WorkplaceData> m_WorkplaceType
— handle to read/write WorkplaceData in the chunk.
- Methods:
public void Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
Iterates the chunk's WorkplaceData array and scales each element's m_MaxWorkers by m_WorkplacesMultiplier (casts result to int). The job writes changes back into the NativeArray obtained from the chunk.- Implements IJobChunk.Execute by forwarding to the above Execute method.
- Behavior/notes:
- Burst compiled for performance.
- Operates per-chunk and writes WorkplaceData back into the chunk's NativeArray.
- Does not check prefab type; it applies to all entities matched by the EntityQuery (WorkplaceData + BuildingData). The Store/Restore logic controls which entities have their originals saved.
Usage Example
// Example usage inside a mod/system that toggles the mode:
WorkPlaceGlobalMode mode = new WorkPlaceGlobalMode();
mode.m_WorkplacesMultiplier = 1.25f; // increase all workplaces by 25%
// When applying the mode, the system will call:
// - GetEntityQueryDesc() to produce the EntityQuery
// - StoreDefaultData(...) once to save original values (typically on mode enable)
// - ApplyModeData(...) each frame/update, which schedules the ModeJob
// When disabling the mode, call RestoreDefaultData(...) to revert values.
Additional notes and tips: - The multiplier arithmetic casts back to int; fractional results are truncated. If you need different rounding behavior, adjust the job logic accordingly. - m_OriginalWorkplaceData is populated in StoreDefaultData and modified in RestoreDefaultData; ensure these methods are invoked from the main thread (they use managed Dictionary and PrefabSystem APIs). - Because ApplyModeData uses ScheduleParallel with a ComponentTypeHandle obtained from EntityManager, ensure the correct access mode (isReadOnly: false) is used so the job is allowed to write WorkplaceData. - If many entities are affected, the Burst job is efficient and parallelized; however, the Dictionary usage is single-threaded and only for storing/restoring defaults.