Skeletonsweeper
Overview
This component tracks player-placed skeletons on the master simulation, maintains them in age-descending order, and enforces a maximum cap (TUNING.MAX_PLAYER_SKELETONS) by removing or decaying the oldest skeletons when the threshold is exceeded. It responds to skeleton spawn events, enables/disables itself dynamically, and performs cleanup during post-initialization.
Dependencies & Tags
TheWorld.ismastersim— Enforces exclusive existence on the master simulation (clients cannot instantiate this component).- Listens to events:
ms_skeletonspawn— Broadcast when a new skeleton is placed.ms_enableskeletonsweeper— Broadcast to toggle sweeping behavior.
- Removes event callbacks for
onremoveon skeleton entities it has previously tracked.
Properties
| Property | Type | Default Value | Description |
|---|---|---|---|
inst | Entity | N/A | Reference to the owning entity (typically the player). |
_enabled | boolean | true | Controls whether the sweeper actively manages skeletons. |
_nosweep | boolean | true initially, then nil | Flag preventing sweeps during initialization; cleared on OnPostInit. |
_skeletons | table | {} | Ordered list of tracked skeleton entities (newest at end, oldest at index 1). |
Main Functions
Sweep(max_to_keep, no_decay)
- Description: Removes or decays skeletons until the collection size is ≤
max_to_keep. Skeletons are removed in age-descending order (oldest first). Ifno_decayis true or the skeleton lacks aDecaymethod, the skeleton is removed directly; otherwise,Decay()is invoked. - Parameters:
max_to_keep(number?): Maximum number of skeletons to retain. Defaults toTUNING.MAX_PLAYER_SKELETONS.no_decay(boolean?): Iftrue, skeletons are removed outright instead of decaying.
OnPostInit()
- Description: Called after full initialization. Disables the
_nosweeprestriction and triggers a sweep if enabled, ensuring the skeleton count complies with the cap.
Events & Listeners
- Listens to
ms_skeletonspawn(inst, skeleton)→ Adds skeleton to_skeletons, subscribes to itsonremoveevent, and triggers a sweep if enabled and not post-init-blocked. - Listens to
ms_enableskeletonsweeper(inst, enable)→ Updates_enabled, and if newly enabled, triggers a sweep. - Listens to
onremove(skeleton)on each tracked skeleton → Removes the skeleton from_skeletonswhen it is destroyed externally.