Skip to main content

StatManager

A class in Root.Township, Alta.StatSystem.

Holds an entity's stats (health, hunger, speed and so on), ticks them on the server, saves them, and syncs changed stats to clients. A component on players and creatures.

Constructors​

MemberWhat it is
StatManager()Unity makes components itself; use AddComponent or spawn a prefab instead of new.

Fields​

9 private and internal fields
MemberWhat it is
private readonly List<TimedStatModifier> appliedModifiersTimed modifiers currently on the stats; saved, and cancelled on DeathReset.
private List<StatDefinitionApplication> definitionApplicationsThe stats this manager has, with their defaults and ranges; backing field for DefinitionApplications.
private Timeline.IEntry entryTimeline entry for the repeating Tick.
private float incrementalTickTimeSeconds between incremental ticks; 0 turns them off.
private Timeline.IEntry incrementalUpdateEntryTimeline entry for the repeating TickIncremental.
private readonly Dictionary<Stat, IndirectStatModifier> indirectModifiersIndirect modifier stack for each stat that has one; saved and loaded with the manager.
private MethodSync syncFullMethod sync for the full stat message.
private MethodSyncStruct<int> syncStatMethod sync for the single stat message.
private float tickTimeSeconds between Tick calls (default 1); 0 turns ticking off. Server only.

Properties​

MemberWhat it is
List<StatDefinitionApplication> DefinitionApplications { get; }The stats this manager has, as set up on the prefab. Their order is the sync index.
float IncrementalTickTime { get; }Seconds between incremental ticks for incrementing stats; 0 turns them off.
Dictionary<StatDefinition, Stat> Stats { get; }All stats on this manager, keyed by definition.

Events​

MemberWhat it is
event Action<StatManager> ResetRaised after DeathReset runs.
event Action<StatManager> UpdatedRaised on each tick.
event Action<StatManager> UpdatedIncrementalRaised on each incremental tick.

Methods​

MemberWhat it is
void ApplyIndirectModifier(string statName, IndirectModifier modifier)Adds an indirect modifier to a stat (found by stat, definition or name). Indirect modifiers are saved.
void ApplyIndirectModifier(StatDefinition statDefinition, IndirectModifier modifier)Adds an indirect modifier to a stat (found by stat, definition or name). Indirect modifiers are saved.
void ApplyIndirectModifier(Stat stat, IndirectModifier modifier)Adds an indirect modifier to a stat (found by stat, definition or name). Indirect modifiers are saved.
TimedStatModifier ApplyTimedModifier(StatModifier modifier, float duration)Applies a stat modifier now and removes it after the duration. Returns the timed modifier; it is saved with the entity.
void DeathReset(IEnumerable<StatDefinition> statsToReset, IEnumerable<StatThresholdValue> statsToModify)On death: resets some stats, sets others to threshold values, resets hunger if the server setting says so, and cancels timed modifiers.
IEnumerator GetEnumerator() (iterator)Yields a single item, the whole Stats.Values collection, not each stat. Iterate Stats instead.
bool GetStat(StatDefinition definition, out Stat stat)Finds a stat by definition, or by definition name ignoring case. Returns false if it is not on this manager.
bool GetStat(string name, out Stat stat)Finds a stat by definition, or by definition name ignoring case. Returns false if it is not on this manager.
override void Initialize()Sets up full and single stat sync. On the server, starts the tick timers and syncs each stat when it changes.
override void InitializeAsServer(SpawnData spawnData)Server: forces every stat to push its value once.
override void OnConnected(Player player)Server: sends every stat to a newly connected player.
protected virtual void Tick()Server: runs every tick time (1 second by default). Raises Updated, then raises external change events for changed stats.
9 private and internal methods
MemberWhat it is
private void Add(StatDefinitionApplication application)Creates a Stat from a stat definition application and stores it in Stats; called for each entry in Awake.
private void Awake()Creates every stat from definitionApplications. On the server it then calls each definition's Applied.
private void OnValidate()Editor: copies each definition's name onto its application entry.
private void RemoveTimedModifier(TimedStatModifier modifier)Removes a timed modifier from appliedModifiers when it ends.
private void SceneDestroy(NetworkEntity entity, bool isUnload)Cancels the tick timers when the entity is destroyed.
private void SyncFull(IPlayer player, Stream stream)Sends every stat with its full range and modifiers; sent to each player when they connect. Rejects the message if a client sends it.
private void SyncSingleStat(IPlayer player, Stream stream, int definitionIndex)Sends one stat, by its index in definitionApplications, with its full data. Rejects the message if a client sends it.
private void SyncStat(Stat stat)Server: listens to each stat's NetworkChanged and sends that stat to the chunk.
private void TickIncremental()Runs every incrementalTickTime: raises UpdatedIncremental and fires the external change events of changed incrementing stats.

Used in​

Members of other types that take, return or hold a StatManager:

BurnEffect.Burn, BurnEffect.burningManager, EffectHandler.statManager, EffectHandler.StatManager, HeatManager.BurnUpdate, HeatManager.OnReset, FixedImpactor.damageManager, ImpactModifierStatAffect.AffectStatManager, ITarget.StatManager, Target.StatManager, ToggleEnemyHealthBars.CheckEnemyNeedsHealthBar, BurnStat.Applied, ControlledStat.Applied, FullEmptyStatTrigger.targetManager, FullEmptyUpdateModifier.Initialize, FullEmptyUpdateModifier.TickDown, FullnessStat.Applied, HungerStat.Applied, HungerStat.GetSets, IncrementingStat.Applied, IndirectStatModifier.Clean, IndirectStatModifier.IndirectStatModifier, IndirectStatModifier.manager, IndirectStatModifier.Update, LevelStatModifier.targetManager, MaxStatDefinition.Applied, MaxStatDefinition.endOfFrameBuffer, MaxStatDefinition.IsValidForManager, MaxStatDefinition.OnReset, MaxStatDefinition.UpdateManager, Modifier.Initialize, Modifier.Manager, ModifierSet.Manager, ModifierSet.ModifierSet, OnFullAffectTargetStatDefinition.Applied, PlayerWeightStat.Applied, PoisonStat.Applied, StatAffectAccessor.IsAffecting, StatAffectAccessor.manager, StatAppliedHandler.BeginInvoke, StatAppliedHandler.Invoke, StatBarVisibilityGroup.StatManager, StatDefinition.Applied, StatModifier.Initialize, StatTrigger.targetManager, ThresholdStat.Applied, ThresholdStat.GetSets, ThresholdStat.ThresholdLevel.GetSet, UpdateModifier.Update, UpdateModifier.UpdateModifier, ConsumptionManager.statManager, CrippleStatDefinition.Applied, HealthObject.statManager, HealthObject.StatManager, PlayerEffectManager.statManager, PlayerHealthVisualEffectsSettings.SetupNightmare, PlayerHealthVisualEffectsSettings.NightmareEffectController.SetupNightmare.