Skip to main content

HealthObject

A class in Root.Township, the global namespace.

Gives an entity health. Listens to its impact detectors, applies damage on the server through the health stat of its StatManager, and syncs death to clients. A component on creatures, players and breakable objects.

Constructors​

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

Fields​

MemberWhat it is
protected Stat healthStatCached health stat backing HealthStat.
protected List<ImpactDetector> impactDetectorsSerialized list of impact detectors behind ImpactDetectors.
14 private and internal fields
MemberWhat it is
private Transform coreBacking field for Core. Falls back to the object's own transform if empty.
private Stat crippleDamageProtectionStatStat that divides incoming cripple damage, if the stat manager has it.
private Stat crippleStatBacking field for CrippleStat.
private Stat damageProtectionStatStat that divides incoming health damage, if the stat manager has it.
private HealthObjectEvent destroyedBacking field for Destroyed, the event invoked when the object is killed.
private EffectHandler effectHandlerBacking field for EffectHandler.
private bool hasCrippleDamageProtectionTrue if the stat manager has the cripple damage protection stat.
private bool hasDamageProtectionTrue if the stat manager has the damage protection stat.
private Dictionary<ImpactType, float> impactTypeMultipliersDamage multiplier per impact type, built from multipliers in Start.
private bool isCombatantBacking field for IsCombatant. Player hits on a combatant mark the player as in combat.
private AttackTypeMultiplier[] multipliersBacking field for Multipliers: damage multipliers per impact type.
private PositionFromOriginHelper positionHelperHelper passed to the PositionHandler created in Awake.
private StatManager statManagerBacking field for StatManager.
private MethodSync syncDeathMethod sync the server uses to tell clients the object died.

Properties​

MemberWhat it is
Transform Core { get; }The central transform of the object, used as its main position.
Stat CrippleStat { get; }The cripple health stat, if the stat manager has one.
HealthObjectEvent Destroyed { get; }Unity event invoked when the object is killed, on both sides.
EffectHandler EffectHandler { get; }The alchemy effect handler for this object, if set.
bool HasCrippleStat { get; private set; }True if a cripple health stat was found on initialize.
float Health { get; set; }Current health value. Setting it sets the stat's base value.
IEnumerable ImpactDetectors { get; }The impact detectors that feed hits into this object.
bool IsCombatant { get; }If true, a player hitting this object is marked as having caused combat.
DamageData LastHit { get; protected set; }The most recent damage data, used to work out who or what killed it.
float MaxHealth { get; set; }Maximum health. Setting it keeps the object at full health if it was full.
AttackTypeMultiplier[] Multipliers { get; }Damage multipliers per attack type (blunt, slash and so on).
PositionFromOriginHandler PositionHandler { get; private set; }Gives the object's position relative to the world origin.
StatManager StatManager { get; }The stat manager that holds health and related stats.
protected virtual HealthObject.DeathAnalytics AnalyticsRecord { get; }Death analytics data (victim, killer name, position) built from the last hit.
protected Stat HealthStat { get; }The health stat. If there is no stat manager or no such stat, a temporary stat with max 1 is made.

Events​

MemberWhat it is
event HealthObject.HealthChangedHandler HealthChangedRaised when health changes, with the change amount (negative for damage) and the last hit.
event Action<DamageData> KilledRaised when the object dies, with the killing hit.
event ImpactHandler OnHitRaised after each valid hit has been applied.

Methods​

MemberWhat it is
void DetectImpact(ImpactHit impact)Handles a valid hit from a detector: applies attack type and armour multipliers, then ReceiveDamage, then raises OnHit.
void Die()Sets health to zero, which kills the object on the server.
override void Initialize()Sets up death sync and looks up the cripple and damage protection stats.
void OnKilled()Raises Killed. On the server also fires the console kill event, counts a task kill for the wielder and sends death to clients.
virtual void OnValidate2()Empty validation hook for subclasses.
virtual void ReceiveDamage(float healthDamage, float crippleDamage, DamageData impact = null)Server: lowers health and cripple health, divided by any damage protection stats. Does nothing once health is zero.
protected virtual void Awake()Points each listed impact detector's HealthTarget at this object.
protected virtual void HandleObjectReachedZeroHealth()Server: called when health hits zero. Calls OnKilled by default; subclasses override it (for example to down a player).
protected void MarkAssailant(NetworkEntity assailant, float damage)Tells the attacking player's character they caused combat, with the distance and damage.
protected void OnHealthChanged(float changeAmount, ImpactHit impact)Raises HealthChanged with a given amount and hit.
protected virtual void Start()Builds the attack type multiplier table and starts listening to the health stat.
5 private and internal methods
MemberWhat it is
private void MarkAssailant(PlayerController assailant, float damage)Tells the attacking player's character they caused combat, with the distance and damage.
private void OnDisable()Unhooks DetectImpact from the impact detectors' hit and particle hit events.
private void OnEnable()Hooks DetectImpact to every listed impact detector's hit and particle hit events.
private void StatChanged(Stat stat)Health stat handler: records the last hit, raises HealthChanged, and on the server handles reaching 0 health.
private void SyncDeath(IPlayer player, Stream stream)Method sync handler. Clients run OnKilled when the server reports the death.

Used in​

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