Healthsyncer
Overview
The HealthSyncer component tracks and synchronizes a player's health percentage and current health status (e.g., healing, taking damage, starving) across server and client. It maintains networked state variables for health percentage and status, and updates them based on gameplay events and component states (e.g., freezing, overheating, hunger, regenerative debuffs). It is server-authoritative for status computation and client-bound for synchronization.
Dependencies & Tags
- Requires the
healthcomponent for status tracking (e.g.,inst.components.health:GetPercent(),health.takingfiredamage,health:IsHurt()). - Uses the
hungercomponent to detect starvation. - Uses
IsFreezing()andIsOverheating()methods, implying interaction with temperature-related logic. - Adds networked variables:
_status(net_tinybyte) and_healthpct(net_float). - Adds listeners for events like
healthdelta,startcorrosivedebuff,starthealthregen,startsmallhealthregen,stopsmallhealthregen, andonremove. - Does not directly add or remove entity tags.
Properties
| Property | Type | Default Value | Description |
|---|---|---|---|
_status | net_tinybyte | 0 | Networked status value encoding health trend: 0=none, 1=small heal, 2=large heal, 3=damage/down; server-only, mapped to GetOverTime() range [-2,2]. |
_healthpct | net_float | 1 | Networked health percentage (0.0–1.0), updated on server via healthdelta and sent to clients. |
corrosives | table | {} | Server-side set of active corrosive debuffs; used to track health-damaging over-time effects. |
hots | table | {} | Server-side set of active large healing-over-time (HoT) debuffs. |
small_hots | table | {} | Server-side set of active small healing-over-time debuffs. |
Main Functions
GetPercent()
- Description: Returns the current health percentage (as a float between 0.0 and 1.0), read from the networked
_healthpctvariable. - Parameters: None.
GetOverTime()
- Description: Decodes the
_statusvalue into a canonical health trend indicator: -2 (large damage), -1 (small damage), 0 (no change), 1 (small heal), 2 (large heal). Values outside [1,4] map to 0. - Parameters: None.
OnUpdate(dt) (server only)
- Description: Periodically recalculates health status based on various debuffs (freezing, overheating, starvation, corrosives, fire damage) and buffs (sleep healing, HoTs, regen-equipment). Updates the
_statusnetwork variable if changed and triggershealthstatusdirtyfor client sync. - Parameters:
dt— time delta since last update (unused, but passed by updater).
Events & Listeners
- Listens to (server-side):
healthdelta: Updates health percentage on damage/heal.startcorrosivedebuff: Registers corrosive debuff and subscribes to itsonremove.starthealthregen: Registers large HoT debuff and subscribes toonremove.startsmallhealthregen: Registers small HoT debuff and subscribes toonremove.stopsmallhealthregen: Immediately unregisters small HoT debuff.
- Listens to (client-side):
healthstatusdirty: TriggersOnStatusDirtyto notify UI of status change.healthpctdirty: TriggersOnHealthPctDirtyto notify UI of percentage change.
- Pushes (server-side):
healthdelta: On initialization (InitServer) to set initial health percentage.
- Pushes (client/server-side):
clienthealthstatusdirty: ViaOnStatusDirty— internal indicator for client health status update.clienthealthdirty: ViaOnHealthPctDirty, with payload{ percent = <float> }— indicates new health percentage for client.