Hunger Replica
Overview
The Hunger component acts as a lightweight, network-safe wrapper for hunger data, primarily used on clients or non-master simulations. It retrieves or attaches to a player_classified or pet_hunger_classified object to mirror the authoritative hunger state (current/max values, starvation status) from the server without directly accessing the master-holding hunger component.
Dependencies & Tags
- Component Dependency: Relies on the presence of either
inst.components.hunger(master simulation) or aclassifiedobject (e.g.,player_classified,pet_hunger_classified) on the entity for replication. - Event Listeners: Attaches an
"onremove"event listener to the classified object to clean up references on removal. - Tags: None identified.
Properties
| Property | Type | Default Value | Description |
|---|---|---|---|
inst | Entity | (none) | Reference to the owning entity. |
classified | Classified? | nil | Reference to the player_classified or pet_hunger_classified object used for data replication. |
Main Functions
AttachClassified(classified)
- Description: Attaches to a classified object and registers an
"onremove"event listener to detect when the classified object is removed, ensuring proper cleanup. Stores a local callback to avoid closure issues. - Parameters:
classified(Classified): The classified object to attach to (typically containscurrenthungerandmaxhungerfields).
DetachClassified()
- Description: Detaches from the classified object by clearing internal references and removing the
"onremove"listener. Should be called automatically via the event system. - Parameters: None.
SetCurrent(current)
- Description: Updates the
"currenthunger"value in the attached classified object. Intended for use on the server to push hunger updates to clients. - Parameters:
current(number): The new current hunger value.
SetMax(max)
- Description: Updates the
"maxhunger"value in the attached classified object. Used to propagate maximum hunger changes (e.g., due to effects or items). - Parameters:
max(number): The new maximum hunger value.
Max()
- Description: Returns the current maximum hunger value. Prioritizes the local
hungercomponent (master) if present; otherwise, reads fromclassified, and defaults to100if neither is available. - Parameters: None.
GetPercent()
- Description: Returns the hunger level as a normalized value between
0and1. Delegates tohunger:GetPercent()if available, otherwise computes from classified values; defaults to1if no hunger data exists. - Parameters: None.
GetCurrent()
- Description: Returns the current hunger value. Uses the local
hungercomponent on the master, or falls back to the classified object, or100if unavailable. - Parameters: None.
IsStarving()
- Description: Returns
trueif the character is starving. Delegates tohunger:IsStarving()on master; otherwise, checks ifcurrenthungerin classified is≤ 0. - Parameters: None.
Events & Listeners
- Listens for:
"onremove"event on the attachedclassifiedobject. TriggersDetachClassified()when fired to prevent stale references. - Does not push or trigger any custom events.