Game.Prefabs.HappinessFactorParameterPrefab
Assembly:
Game (in-game assembly / Assembly-CSharp)
Namespace:
Game.Prefabs
Type:
Class
Base:
PrefabBase
Summary:
Prefab that defines per-happiness-factor parameters used by the citizen happiness system. It exposes two parallel arrays (m_BaseLevels and m_LockedEntities) indexed by CitizenHappinessSystem.HappinessFactor enum values (the code expects 25 entries). During LateInitialize it resolves any referenced PrefabBase entries into Entities (via PrefabSystem) and writes a DynamicBuffer
Fields
-
public int[] m_BaseLevels = new int[25];
Array of base happiness levels, one entry per CitizenHappinessSystem.HappinessFactor. Values are written into the HappinessFactorParameterData buffer during LateInitialize. The array length is 25 in the source; ensure it matches the number of enum values expected by the game. -
public PrefabBase[] m_LockedEntities = new PrefabBase[25];
Parallel array mapping each happiness factor to an optional PrefabBase that, if set, will be resolved to an Entity and stored in the HappinessFactorParameterData.m_LockedEntity field. Null entries are converted to Entity.Null. The [EnumValue] attribute makes the inspector show enum labels for array indices.
Properties
- None declared on this type. All data is exposed via public fields and the prefab/component system.
Constructors
public HappinessFactorParameterPrefab()
Default constructor — no custom runtime initialization beyond the field initializers (arrays initialized to length 25). Instances are typically created/edited in the Unity editor.
Methods
-
public override void GetDependencies(List<PrefabBase> prefabs)
Adds any PrefabBase references from m_LockedEntities to the provided dependencies list so the prefab system knows about referenced prefabs. Calls base.GetDependencies(prefabs) first. -
public override void GetPrefabComponents(HashSet<ComponentType> components)
Adds ComponentType.ReadWrite() to the components set so the prefab archetype will include the DynamicBuffer component. Calls base.GetPrefabComponents(components) first. -
public override void GetArchetypeComponents(HashSet<ComponentType> components)
Overrides the base hook but does not add additional archetype components in this class (calls base.GetArchetypeComponents). -
public override void LateInitialize(EntityManager entityManager, Entity entity)
Main initialization step applied after the prefab entity is created. It: - Gets the PrefabSystem (World.DefaultGameObjectInjectionWorld.GetOrCreateSystemManaged
()). - Retrieves the DynamicBuffer
from the entity. - Iterates the m_BaseLevels array and, for each index, resolves the corresponding m_LockedEntities entry to an Entity (or Entity.Null).
- Adds a HappinessFactorParameterData entry to the buffer with m_BaseLevel set and m_LockedEntity set to the resolved Entity. Notes: the implementation iterates using m_BaseLevels.Length — ensure m_LockedEntities has at least that length to avoid index issues. The buffer type must be present on the prefab entity (ensured by GetPrefabComponents).
Usage Example
// Read the initialized data from the prefab entity at runtime
var world = World.DefaultGameObjectInjectionWorld;
var prefabSystem = world.GetOrCreateSystemManaged<PrefabSystem>();
var entityManager = world.EntityManager;
// Assume 'happinessPrefab' is a PrefabBase reference set in some inspector or obtained earlier:
Entity prefabEntity = prefabSystem.GetEntity(happinessPrefab);
// The LateInitialize of HappinessFactorParameterPrefab will have populated this buffer
var buffer = entityManager.GetBuffer<HappinessFactorParameterData>(prefabEntity);
for (int i = 0; i < buffer.Length; i++)
{
var entry = buffer[i];
Debug.Log($"Factor {i}: baseLevel={entry.m_BaseLevel}, lockedEntity={entry.m_LockedEntity}");
}
Additional notes: - The [EnumValue(typeof(CitizenHappinessSystem.HappinessFactor))] attributes make the fields editor-friendly by showing enum labels for each array index. - This prefab is intended to be used as part of the CitizenHappinessSystem data setup — ensure the HappinessFactorParameterData buffer component is used by the systems that read these parameters.