Yotb Stagemanager
Overview
This component acts as the central coordinator for stage-based contests within the Yotb mod. It tracks registered stages, controls the activation and deactivation of contest logic per stage, manages wandering trader visibility, and handles saving/loading of contest state across game sessions. It operates on the world entity (TheWorld) and integrates with the DST world state and event systems.
Dependencies & Tags
- Relies on
yotb_stagercomponent being present on stage entities (used inOnStageBuiltviav.components.yotb_stager:EnableContest()). - Listens to and dispatches custom mod events (e.g.,
yotb_onstagebuilt,yotb_contestenabled,yotb_conteststarted,yotb_contestfinished,wanderingtrader_created,onremove,yotb_onabortcontest,yotb_oncontestfinshed). - Watches the
cyclesworld state to trigger daily logic. - Registers callbacks on stage entities for
conteststarted,contestcheckpoint,onremove, andcontestfinished(currently commented out). - No tags are added or removed by this component.
Properties
| Property | Type | Default Value | Description |
|---|---|---|---|
inst | Entity | — | Reference to the world entity the component is attached to. |
stages | table | {} | List of registered stage entities. |
wanderingtraders | table | {} | Map of registered wandering trader entities. |
contest_enabled | boolean | false | Whether contest logic is currently enabled globally. |
contest_active | boolean | false | Whether a contest is currently in progress. |
save_contest | boolean | false | Flag indicating whether the current contest state should be persisted on save. |
active_stage | Entity? | nil | The stage currently hosting the active contest. |
host_visible | boolean? | nil | Controls wandering trader visibility state (only set via SetHostVisible). |
Main Functions
:RegisterWanderingTrader(wanderingtrader)
- Description: Registers a wandering trader entity for visibility management and sets up a listener to automatically unregister it when the trader is removed.
- Parameters:
wanderingtrader(Entity): The wandering trader entity to register.
:UnregisterWanderingTrader(wanderingtrader)
- Description: Removes a wandering trader from the registry, typically invoked automatically via
onremoveevent. - Parameters:
wanderingtrader(Entity): The wandering trader entity to unregister.
:OnNewDay()
- Description: Called at the start of each new day. If neither contest nor contest enabling is active, it automatically calls
EnableContest(). - Parameters: None.
:OnStageBuilt(stage)
- Description: Registers a new stage, adds it to the internal stages list, and sets up event listeners for contest lifecycle events (
conteststarted,contestcheckpoint,onremove). If contests are enabled, it immediately activates contest mode on the stage’syotb_stagercomponent. - Parameters:
stage(Entity): The newly built stage entity.
:OnStageDestroyed(stage)
- Description: Cleans up stage tracking and removes all event callbacks associated with the stage. Removes the stage from the internal
stageslist. - Parameters:
stage(Entity): The destroyed stage entity.
:OnContestCheckPoint(stage)
- Description: Sets the
save_contestflag totrue, ensuring the current contest state will be persisted during save. - Parameters:
stage(Entity): The stage that triggered the contest checkpoint.
:SetHostVisible(visible)
- Description: Updates and synchronizes the visibility of all registered wandering traders. Pushes
wanderingtrader_hideorwanderingtrader_showevents to them as appropriate. - Parameters:
visible(boolean):trueto show traders,falseto hide them.
:EnableContest()
- Description: Enables contest mode globally across all valid registered stages. Has no effect if a contest is already active. Fires the
yotb_contestenabledevent upon success. - Parameters: None.
:OnContestBegun(active_stage)
- Description: Marks a contest as active, stores the current stage, disables contest mode on all other stages, and fires the
yotb_conteststartedevent. - Parameters:
active_stage(Entity): The stage where the contest has begun.
:OnContestEnded()
- Description: Resets the active contest state, clears
save_contest, unsets the active stage, and fires theyotb_contestfinishedevent. - Parameters: None.
:OnSave()
- Description: Prepares and returns save data for the component, including stage GUIDs, contest status, and whether the contest should be persisted. Returns two values: the data table and a list of entity GUIDs to persist.
- Parameters: None.
- Returns:
data(table): Containscontest_enabled,contest_active, andstages(array of GUIDs).ents(table): List of GUIDs for stages to persist.
:LoadPostPass(newents, savedata)
- Description: Reconstructs the component state after a save is loaded. Re-registers stages using
OnStageBuilt, restores contest flags, and re-enables contests if needed. - Parameters:
newents(table): Map of GUIDs to loaded entities.savedata(table): Saved component data (fromOnSave).
Events & Listeners
- Listens for:
yotb_onstagebuilt→ callsOnStageBuilt(stage)(via anonymous callback).yotb_onabortcontest→ resetscontest_activeandactive_stage.yotb_oncontestfinshed→ callsOnContestEnded().wanderingtrader_created→ callsRegisterWanderingTrader(wanderingtrader).
- Listens per-stage (in
OnStageBuilt):conteststarted→ callsconteststarted(stage)(local hook →OnContestBegun).contestcheckpoint→ callscontestcheckpoint(stage)(local hook →OnContestCheckPoint).onremove→ callsstageremoved(stage)(local hook →OnStageDestroyed).
- Listens to world state:
cycles→ callsOnNewDay()(viaWatchWorldState).
- Pushes:
yotb_contestenabledyotb_conteststartedyotb_contestfinishedwanderingtrader_hide/wanderingtrader_show(on wandering traders when visibility changes).