Game.Prefabs.Modes.XPParametersMode
Assembly: Assembly-CSharp (typical for game scripts)
Namespace: Game.Prefabs.Modes
Type: class
Base: EntityQueryModePrefab
Summary:
XPParametersMode is a Unity component (prefab mode) used to apply XP-related configuration values into the runtime ECS singleton XPParameterData. It exposes two configurable float fields that control how much XP is granted per population and per happiness. The mode provides an EntityQuery description to find the XPParameterData singleton, copies values from this mode instance into that singleton at runtime, and can restore defaults from the associated XPParametersPrefab.
Fields
-
public float m_XPPerPopulation
Amount of XP to apply per unit of population. When ApplyModeData runs, this value is written into the XPParameterData singleton's m_XPPerPopulation field. -
public float m_XPPerHappiness
Amount of XP to apply per unit of happiness. When ApplyModeData runs, this value is written into the XPParameterData singleton's m_XPPerHappiness field.
Properties
- None declared on this type.
Constructors
public XPParametersMode()
Default constructor (not explicitly declared in source; provided by C#). Instances of this class are typically created/managed by Unity as a component on a prefab.
Methods
public override EntityQueryDesc GetEntityQueryDesc()
Returns an EntityQueryDesc that selects entities with a read-only XPParameterData component. This is used to locate the singleton entity that holds XPParameterData for reading/writing by the mode.
Behavior summary:
- Builds and returns an EntityQueryDesc whose All array contains ComponentType.ReadOnly
-
protected override void RecordChanges(EntityManager entityManager, Entity entity)
Records (accesses) the XPParameterData component on the given entity. The method calls entityManager.GetComponentData(entity). This can be used by the mode/prefab system to detect or record that this component is relevant/observed by the mode. -
public override JobHandle ApplyModeData(EntityManager entityManager, EntityQuery requestedQuery, JobHandle deps)
Applies the mode's configured values to the runtime XPParameterData singleton: - Obtains singleton entity via requestedQuery.GetSingletonEntity().
- Reads the current XPParameterData struct from that entity.
- Overwrites componentData.m_XPPerPopulation and componentData.m_XPPerHappiness with this mode's m_XPPerPopulation and m_XPPerHappiness.
- Writes the modified componentData back to the entity via entityManager.SetComponentData.
- Returns the incoming deps JobHandle unchanged.
Notes: - Assumes requestedQuery resolves to the singleton entity; calling code must ensure the query is correct and singleton exists. - The method performs synchronous component reads/writes on the EntityManager and does not schedule any additional jobs.
public override void RestoreDefaultData(EntityManager entityManager, ref NativeArray<Entity> entities, PrefabSystem prefabSystem)
Restores the XPParameterData on the given entity(s) to the defaults defined in the corresponding XPParametersPrefab:- Gets the first entity from entities.
- Retrieves XPParametersPrefab via prefabSystem.GetPrefab
(entity). - Reads the current XPParameterData from the entity, then overwrites m_XPPerPopulation and m_XPPerHappiness with the values from the prefab.
- Writes the modified XPParameterData back to the entity.
Notes: - This method depends on the PrefabSystem and the presence of a matching XPParametersPrefab that exposes default values.
Usage Example
// Example: programmatically applying values from an XPParametersMode instance into runtime ECS.
// Assume `entityManager` and `deps` are available, and a requestedQuery has been created using GetEntityQueryDesc().
XPParametersMode modeInstance = /* obtained from prefab/component */;
modeInstance.m_XPPerPopulation = 2.5f;
modeInstance.m_XPPerHappiness = 0.75f;
EntityQuery requestedQuery = entityManager.CreateEntityQuery(typeof(XPParameterData));
JobHandle resultDeps = modeInstance.ApplyModeData(entityManager, requestedQuery, deps);
// After this call, the XPParameterData singleton will have its m_XPPerPopulation and m_XPPerHappiness
// overwritten with the values set on modeInstance.
Additional notes: - The class is decorated with [ComponentMenu("Modes/Mode Parameters/", ...)], so it is intended to be added to prefabs through Unity's component menu. - The mode writes directly to a singleton XPParameterData component; ensure only one such singleton exists or that queries are scoped correctly. - RestoreDefaultData expects that the PrefabSystem can provide an XPParametersPrefab for the entity; that prefab type defines the default XP values.