Game.Prefabs.Modes.PlaceableObjectGlobalMode
Assembly: Assembly-CSharp
Namespace: Game.Prefabs.Modes
Type: class
Base: EntityQueryModePrefab
Summary:
PlaceableObjectGlobalMode is a mode prefab used to modify PlaceableObjectData for placeable objects (including buildings but excluding zone buildings, spawnable buildings, trees and plants) across a set of entities. It schedules a Burst-compiled IJobChunk (ModeJob) that multiplies the m_ConstructionCost and m_XPReward fields of PlaceableObjectData by configurable multipliers. The mode also provides a RestoreDefaultData implementation which resets modified data back to values read from the original PlaceableObject prefab data via a PrefabSystem lookup.
Fields
private struct ModeJob
A nested Burst-compiled IJobChunk implementation that performs the per-chunk modification of PlaceableObjectData. ModeJob contains:public float m_ConstructionCostMultiplier
— multiplier used to scale m_ConstructionCost.public float m_XPRewardMultiplier
— multiplier used to scale m_XPReward.-
public ComponentTypeHandle<PlaceableObjectData> m_PlaceableObjectType
— component type handle used to access PlaceableObjectData in the chunk. The job multiplies each PlaceableObjectData.m_ConstructionCost and m_XPReward by the provided multipliers and writes the modified values back into the chunk's native array. It is Burst-compiled and scheduled in parallel through JobChunkExtensions.ScheduleParallel. -
public float m_ConstructionCostMultiplier
Public field exposed on the prefab that defines the multiplier applied to PlaceableObjectData.m_ConstructionCost when the mode is applied. Typical values: 1.0 (no change), 0.5 (50% cost), 2.0 (double cost). -
public float m_XPRewardMultiplier
Public field exposed on the prefab that defines the multiplier applied to PlaceableObjectData.m_XPReward when the mode is applied. Typical values: 1.0 (no change), 0 (disable XP), 1.5 (increase XP by 50%).
Properties
- This class does not define public properties.
Constructors
public PlaceableObjectGlobalMode()
Default parameterless constructor (implicit). Instances are typically created by the prefab/system and configured via serialized fields in the prefab or inspector.
Methods
public override EntityQueryDesc GetEntityQueryDesc()
: EntityQueryDesc
Builds and returns an EntityQueryDesc that selects entities to modify. The query requires:- All: PlaceableObjectData (ReadOnly), PlaceableInfoviewItem (ReadOnly)
- Any: BuildingData (ReadOnly), BuildingExtensionData (ReadOnly)
-
None: SpawnableBuildingData (ReadOnly), TreeData (ReadOnly), PlantData (ReadOnly)
In short: selects placeable objects (including non-zone buildings), excludes spawnable buildings, trees and plants. -
protected override void RecordChanges(EntityManager entityManager, Entity entity)
: void
Called to record that the mode changed a particular entity. Implementation reads PlaceableObjectData for the entity (entityManager.GetComponentData(entity)) — this is used by the mode/prefab system to track which entities were affected so they can later be restored. -
public override JobHandle ApplyModeData(EntityManager entityManager, EntityQuery requestedQuery, JobHandle deps)
: JobHandle
Schedules the nested ModeJob over the provided requestedQuery using JobChunkExtensions.ScheduleParallel. The scheduled job will: - set m_ConstructionCostMultiplier and m_XPRewardMultiplier from the prefab fields,
- acquire a non-read-only ComponentTypeHandle
from the EntityManager, -
modify each PlaceableObjectData entry in selected chunks as described above. Returns the JobHandle of the scheduled job (so callers can chain dependencies).
-
public override void RestoreDefaultData(EntityManager entityManager, ref NativeArray<Entity> entities, PrefabSystem prefabSystem)
: void
Iterates the provided entities and attempts to restore their PlaceableObjectData fields from the original prefab data: - For each entity, it uses prefabSystem.TryGetPrefab
(entity, out prefabBase) and prefabBase.TryGetExactly (out component) to obtain the original PlaceableObject component values. - If the prefab and component are found, it sets PlaceableObjectData.m_ConstructionCost and m_XPReward to the original values from the prefab (component.m_ConstructionCost, component.m_XPReward).
-
If the prefab or component cannot be resolved, it logs a warning via ComponentBase.baseLog.Warn and skips the entity. This ensures changes made by ApplyModeData can be reverted to the prefab defaults.
-
Nested Job:
ModeJob.Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
: void
Implementation detail of the IJobChunk that iterates the chunk's PlaceableObjectData native array and writes back updated values. There is also the explicit interface implementation of IJobChunk.Execute that forwards to this method.
Usage Example
// Example usage from a system/mod initialization to apply the mode programmatically.
// Typically this prefab is configured in the editor. Below demonstrates programmatic use.
PlaceableObjectGlobalMode modePrefab = /* obtain the prefab instance from your prefab system */;
modePrefab.m_ConstructionCostMultiplier = 0.75f; // reduce construction cost to 75%
modePrefab.m_XPRewardMultiplier = 1.2f; // increase XP reward by 20%
// Create an EntityQuery from the mode's description and apply the mode:
EntityManager em = World.DefaultGameObjectInjectionWorld.EntityManager;
EntityQueryDesc desc = modePrefab.GetEntityQueryDesc();
EntityQuery query = em.CreateEntityQuery(desc);
// Schedule the job (deps would be obtained from your system update context)
JobHandle deps = default;
JobHandle handle = modePrefab.ApplyModeData(em, query, deps);
// remember to complete or schedule handle into your system/job chain
handle.Complete();
Notes and tips: - The modification happens in a Burst-compiled, parallel IJobChunk for performance. Ensure multi-threading and Burst are compatible with other scheduled jobs that touch the same components (use proper dependency chaining). - RestoreDefaultData relies on PrefabSystem resolving the original PlaceableObject prefab and component. If prefabs are missing or types differ, restoration will be skipped for those entities and a warning is logged. - The EntityQuery deliberately excludes SpawnableBuildingData, TreeData and PlantData to avoid modifying zone or nature-related prefabs. Adjust the query if you need different selection behavior.