Hallucinations
Overview
This component governs the lifecycle of hallucination entities—temporary visual disturbances spawned near the player based on their sanity, ambient light levels, and time of day. It dynamically spawns, tracks, and removes hallucinations by monitoring player state, lighting, and night cycles.
Dependencies & Tags
- Components Required via
inst: None explicitly added by this component. - Tags Used Internally:
fire(must-tag forshadowwatchersearch)_equippable(prohibited-tag forshadowwatchersearch)- All
FUELTYPEvalues appended with"_fueled"(e.g.,"wood_fueled","greengas_fueled") to identify active fire sources forshadowwatcherplacement logic.
- Watched World States:
isnight,isfullmoon - Listened Events:
playeractivated,playerdeactivated,onremove(attached to hallucination entities), andnightvision(attached per-player)
Properties
| Property | Type | Default Value | Description |
|---|---|---|---|
inst | Entity | nil (assigned via constructor) | The root entity to which this component is attached (typically the world or a manager). |
_player | Entity? | nil | Reference to the currently active player. |
_hallucinations | table | {} | Internal registry of hallucination types, each containing name, params, and count. |
_fueltags | table<string> | {} | List of fuel-related tags used to detect burning fire sources. Populated from FUELTYPE. |
Main Functions
RestartHallucination(hallucination)
- Description: Starts or restarts a hallucination task after an initial random delay (
initial_variance * random()), ensuring the first spawn is randomized. - Parameters:
hallucination— A hallucination entry from_hallucinations, containing.params(withinitial_variance) and.task.
RepeatHallucination(hallucination, delay)
- Description: Schedules the next spawn of a hallucination. Uses either a provided
delayor computes a random delay withininterval ± variance. - Parameters:
hallucination— A hallucination entry.
delay(optional) — A numeric delay override; if omitted, computed asinterval + variance * math.random().
Start(nightonly)
- Description: Starts all hallucination tasks (or only those matching
nightonlyif specified) that are currently inactive (.task == nil). - Parameters:
nightonly(optional,boolean?) — Iftrue/false, only starts hallucinations whereparams.nightonlymatches; ifnil, starts all inactive hallucinations.
Stop(nightonly)
- Description: Cancels all currently scheduled hallucination tasks (or only those matching
nightonly) by setting.task = nil. - Parameters:
nightonly(optional,boolean?) — Filters which hallucinations to stop, mirroringStart.
self:GetDebugString()
- Description: Returns a comma-separated string listing each hallucination type and its current spawn count for debugging.
- Parameters: None.
- Returns:
string?— E.g.," 0 creepyeyes, 2 shadowwatcher, 4 shadowskittish"ornilif all counts are zero.
Events & Listeners
- Listens for
playeractivated→OnPlayerActivated(inst, player)
Switches the active player context, sets up sanity/lighting observers, and starts hallucinations. - Listens for
playerdeactivated→OnPlayerDeactivated(inst, player)
Cleans up the active player reference, cancels tasks, and stops all hallucinations. - Listens for
nightvisionon_player→OnIsNight()
Re-evaluates night-specific hallucinations when the player gains/loses night vision. - Watches
isnightandisfullmoon→OnIsNight()
Reacts to day/night/full-moon transitions to control night-only hallucinations. - Listens for
onremoveon hallucination entities →StopTracking(ent)
Decrements the current count for the hallucination type when an entity is removed, allowing new spawns. - Triggers
RepeatHallucination(...)calls
Internally viaspawnfns to reschedule the next spawn after each successful or failed spawn attempt.