Storyteller
Overview
This component enables an entity to deliver storylines through scripted dialogue sequences. It handles story setup, line-by-line speech via the talker component, periodic tick callbacks during storytelling, and proper event listening/cleanup. It supports custom story definitions via user-provided functions and ensures robust termination of active stories (e.g., on entity removal or manual abort).
Dependencies & Tags
- Requires the
talkercomponent (used to speak story lines). - Automatically adds the
"storyteller"tag to the entity on initialization and removes it on removal from entity. - Listens for
donetalkingandonremoveevents. - Registers a removal listener on the
story.propproperty if provided.
Properties
| Property | Type | Default Value | Description |
|---|---|---|---|
storytelling_dist | number | 10 | Not used in current implementation; possibly legacy or reserved for future use. |
storytelling_ticktime | number | 2.5 | Default tick interval (in seconds) between ontickfn callbacks if not overridden per story. |
storytotellfn | function? | nil | User-defined function `(inst, storyprop) → story |
onstorybeginfn | function? | nil | Callback invoked when a story starts: (inst, story) → nil. |
onstoryoverfn | function? | nil | Callback invoked when a story completes (either finishes or is aborted): (inst, story) → nil. |
story | table? | nil | Active story data object, populated only while storytelling is in progress. Contains lines, ontickfn, ticktime, and prop keys. |
storyprop_onremove | function | (prop) → self:AbortStory() | Internal callback used to abort the story if the story.prop object is removed from the world. |
Main Functions
SetStoryToTellFn(fn)
- Description: Sets the function responsible for generating the story data when
TellStoryis called. - Parameters:
fn: A function accepting(inst, storyprop)and returning either:- A valid story table (with
lines, optionalontickfn, and optionalticktime), nil, or- A string error message.
- A valid story table (with
SetOnStoryBeginFn(fn)
- Description: Registers a callback to be invoked immediately after story lines begin speaking.
- Parameters:
fn: A function(inst, story) → nil.
SetOnStoryOverFn(fn)
- Description: Registers a callback to be invoked when a story ends (either normally or via abort). The
storyobject may benilif aborted before completion. - Parameters:
fn: A function(inst, story) → nil.
IsTellingStory()
- Description: Returns whether a story is currently in progress.
- Returns:
boolean—trueifself.story ~= nil, otherwisefalse.
AbortStory(reason)
- Description: Immediately terminates the active story, optionally speaking a final line via
talker. Cleans up state and triggersonstoryoverfn. - Parameters:
reason: Optional string — if provided, the storyteller entity will speak it before stopping; otherwise, it stops silently.
OnDone()
- Description: Internal cleanup function that cancels pending tasks, removes event listeners, invokes
onstoryoverfn, and clearsself.story. Called byAbortStoryand automatically when the last line finishes speaking. - Parameters: None.
OnStoryTick()
- Description: Invokes the story’s
ontickfncallback (if defined) at the configured interval (story.ticktimeorstorytelling_ticktime). - Parameters: None.
TellStory(storyprop)
- Description: Initiates a storytelling sequence. Validates and processes the story definition, speaks the lines, sets up tick callbacks and event listeners, and triggers
onstorybeginfn. - Parameters:
storyprop: A prop object passed tostorytotellfnto generate the story.
- Returns:
boolean, string?—trueon success;false, reason(string) on failure (e.g., missingstorytotellfn, story generation returnednilor error string).
Events & Listeners
- Listens to:
"donetalking"— triggerson_done_talking, which callsOnDone()to finish the story after all lines have spoken."onremove"— on the entity itself, triggersself.storyprop_onremove(i.e.,AbortStory())."onremove"— on thestory.propobject (if provided), also triggersself.storyprop_onremove.
- Triggers:
- None directly (events are consumed internally).