BGE/Story

Alias: BGE.Story


Static Methods

evaluateConditions( conditions: Array.<StoryCondition>, state: StoryState, ): boolean

static

True when every condition passes. An empty or missing list passes.

Parameters

Returns

  • boolean

conditionPasses( condition: StoryCondition, state: StoryState, ): boolean

static

Never compares a stored flag with = directly - a missing one is invalid, and invalid = 2 can crash.

Parameters

Returns

  • boolean

flagEquals( flags: StoryFlags, key: string, expected: boolean | integer | string, ): boolean

static

Parameters

  • flags (StoryFlags)
  • key (string)
  • expected (boolean | integer | string)

Returns

  • boolean

isBuiltInConditionKey(key: string): boolean

static

The keys that name a built-in condition (modifiers like eq and gte don't count).

Parameters

  • key (string)

Returns

  • boolean

applyDebugState( state: StoryState, flags?: dynamic, itemCounts?: dynamic, ): StoryResult

static

Sets flags and gives items for a debug launch (deep link) or a test, then settles the rules so derived flags exist on arrival.

flags can be a string ("gate.open,rats.kills=4,name=Bram"), an array of flag names (set to true) or an AA of name -> value. In a string, a value reads as an integer, float, true/false, or else a string. itemCounts can be a string ("smallKey:2,throneKey"), an array of item ids (one each), or an AA of id -> count. invalid skips either.

Parameters

  • state (StoryState)
  • flags (dynamic, optional, default: "invalid")
  • itemCounts (dynamic, optional, default: "invalid")

Returns

parseDebugFlags(flags: dynamic): roAssociativeArray

static

Flags for applyDebugState(), as name -> value.

Parameters

  • flags (dynamic) — see applyDebugState()

Returns

  • roAssociativeArray

parseDebugItems(itemCounts: dynamic): roAssociativeArray

static

Items for applyDebugState(), as id -> count.

Parameters

  • itemCounts (dynamic) — see applyDebugState()

Returns

  • roAssociativeArray

debugListParts(value: dynamic): Array.<string>

static

A comma-separated string or an array of strings, as trimmed, non-empty strings.

Parameters

  • value (dynamic)

Returns

  • Array.<string>

parseDebugValue(text: string): dynamic

static

"4" -> 4, "1.5" -> 1.5, "true"/"false" -> booleans, anything else as written.

Parameters

  • text (string)

Returns

  • dynamic

newStoryResult(): StoryResult

static

Returns

mergeStoryResult(into: StoryResult, source: StoryResult): void

static

Adds source's changes, posts and messages to into.

Parameters

Returns

  • void

applyEffects( effects: Array.<StoryEffect>, state: StoryState, ): StoryResult

static

Applies effects in order.

Parameters

Returns

applyEffect( effect: StoryEffect, state: StoryState, result: StoryResult, ): boolean

static

Parameters

Returns

  • boolean — true if story state changed

isBuiltInEffectKey(key: string): boolean

static

The keys that name a built-in effect (modifiers like value and amount don't count).

Parameters

  • key (string)

Returns

  • boolean

runEventRules( rules: Array.<EventRule>, eventName: string, data: roAssociativeArray, state: StoryState, ): StoryResult

static

Runs every rule for this event whose match and when pass, in order.

Parameters

  • rules (Array.<EventRule>)
  • eventName (string)
  • data (roAssociativeArray) — the event's data
  • state (StoryState)

Returns

ruleMatches( match: roAssociativeArray, data: roAssociativeArray, ): boolean

static

Parameters

  • match (roAssociativeArray)
  • data (roAssociativeArray)

Returns

  • boolean — true if every key of match equals the same field of data (match values must be scalars)

settleRules( rules: Array.<EventRule>, state: StoryState, ): StoryResult

static

Runs "storyChanged" rules until nothing more changes, at most STORY_SETTLE_MAX_PASSES times.

Parameters

Returns

getCandidates(entry: dynamic): dynamic

static

Parameters

  • entry (dynamic) — a story.nodes value

Returns

  • dynamic — its candidate list, or invalid for a single node

getSelectMode(entry: dynamic): string

static

Parameters

  • entry (dynamic)

Returns

  • string — "first" (the default), "random", whatever string was written, or "" for a non-string

isEligibleNode( node: dynamic, key: string, state: StoryState, ): boolean

static

Parameters

Returns

  • boolean

selectNode( id: string, story: StoryData, state: StoryState, random: function, ): SelectedNode

static

Picks the node to play for an id: the node itself, or one of its candidates.

Parameters

Returns

resolveNodeKey(key: string, story: StoryData): StoryNode

static

Parameters

Returns

  • StoryNode — the node a visit key names, or invalid

defaultRandom(count: integer): integer

static

Parameters

  • count (integer)

Returns

  • integer — a random index from 0 to count - 1

newFrame(key: string, node: StoryNode): RunnerFrame

static

Parameters

Returns

lineCount(node: StoryNode): integer

static

Parameters

Returns

  • integer

isLabelled(choice: StoryChoice): boolean

static

Parameters

Returns

  • boolean

choiceKey( nodeKey: string, choice: StoryChoice, index: integer, ): string

static

The visit key for a choice, used by once.

Parameters

  • nodeKey (string)
  • choice (StoryChoice)
  • index (integer)

Returns

  • string

loadStory(path: string): StoryData

static

Reads a story from a JSON file. Node and item ids are case-insensitive. Check it with validateStory() once your game's own conditions and effects are registered.

Parameters

  • path (string) — e.g. "pkg:/story/jokes.json"

Returns

  • StoryData — the story, or invalid (with a warning) if it can't be read or has no nodes

validateStory(story: dynamic, state: StoryState): Array.<string>

static

Checks a story for mistakes: jumps to nodes that don't exist, conditions and effects nothing handles (or with two keys, of which only one would run), items the story doesn't declare, mistyped fields, empty or malformed nodes. Run it in a test over your real story data.

Parameters

  • story (dynamic)
  • state (StoryState) — with your game's conditions and effects registered

Returns

  • Array.<string> — one message per problem; empty when the story is fine

validateStoryNode( where: string, node: dynamic, story: StoryData, state: StoryState, errors: Array.<string>, ): void

static

Parameters

Returns

  • void

validateStoryChoiceFields( where: string, choice: roAssociativeArray, errors: Array.<string>, ): void

static

Parameters

  • where (string)
  • choice (roAssociativeArray)
  • errors (Array.<string>)

Returns

  • void

validateStoryBooleanField( where: string, item: roAssociativeArray, key: string, errors: Array.<string>, ): void

static

Reports item[key] if it's present but not true or false.

Parameters

  • where (string)
  • item (roAssociativeArray)
  • key (string)
  • errors (Array.<string>)

Returns

  • void

validateStoryTarget( where: string, target: dynamic, story: StoryData, errors: Array.<string>, ): void

static

Parameters

  • where (string)
  • target (dynamic)
  • story (StoryData)
  • errors (Array.<string>)

Returns

  • void

validateStoryConditions( where: string, conditions: dynamic, story: StoryData, state: StoryState, errors: Array.<string>, ): void

static

Parameters

  • where (string)
  • conditions (dynamic)
  • story (StoryData)
  • state (StoryState)
  • errors (Array.<string>)

Returns

  • void

validateStoryEffects( where: string, effects: dynamic, story: StoryData, state: StoryState, errors: Array.<string>, ): void

static

Parameters

  • where (string)
  • effects (dynamic)
  • story (StoryData)
  • state (StoryState)
  • errors (Array.<string>)

Returns

  • void

validateBuiltInCondition( where: string, condition: roAssociativeArray, story: StoryData, errors: Array.<string>, ): void

static

Built-in conditions read these fields directly, so a mistyped one fails (or crashes) at runtime.

Parameters

  • where (string)
  • condition (roAssociativeArray)
  • story (StoryData)
  • errors (Array.<string>)

Returns

  • void

validateBuiltInEffect( where: string, effect: roAssociativeArray, story: StoryData, errors: Array.<string>, ): void

static

Parameters

  • where (string)
  • effect (roAssociativeArray)
  • story (StoryData)
  • errors (Array.<string>)

Returns

  • void

validateStoryItemId( where: string, item: roAssociativeArray, key: string, story: StoryData, errors: Array.<string>, ): void

static

Reports item[key] if it's a string naming an item the story's items don't declare. (A non-string id is reported by validateStoryFieldKind, and malformed items by validateStory.)

Parameters

  • where (string)
  • item (roAssociativeArray)
  • key (string)
  • story (StoryData)
  • errors (Array.<string>)

Returns

  • void

validateStoryFieldKind( where: string, what: string, item: roAssociativeArray, key: string, kinds: Array.<string>, kindText: string, errors: Array.<string>, ): void

static

Reports item[key] if it's present but not one of the kinds.

Parameters

  • where (string)
  • what (string)
  • item (roAssociativeArray)
  • key (string)
  • kinds (Array.<string>)
  • kindText (string)
  • errors (Array.<string>)

Returns

  • void

validateStoryRuleMatch( where: string, match: dynamic, errors: Array.<string>, ): void

static

Parameters

  • where (string)
  • match (dynamic)
  • errors (Array.<string>)

Returns

  • void

interpolateFlags(text: string, flags: StoryFlags): string

static

Replaces each ${key} in text with that flag's value. A missing flag reads as "", a boolean as true/false, and a ${ with no closing } is left as written.

Parameters

Returns

  • string

valueKind(value: dynamic): string

static

The kind of a story value, the same on a real Roku and the simulator (which name boxed types differently, e.g. roInt vs roInteger).

Parameters

  • value (dynamic)

Returns

  • string — "integer", "float", "boolean", "string", "invalid", or the lowercased type name

isScalar(value: dynamic): boolean

static

True for a value that can be compared with = and <>: an integer, float, boolean, string or invalid.

Parameters

  • value (dynamic)

Returns

  • boolean

isTrue(value: dynamic): boolean

static

True only for a boolean true - safe on a missing (invalid) or non-boolean value.

Parameters

  • value (dynamic)

Returns

  • boolean

Other

RUNNER_MAX_STEPS

staticreadonly

Stops a story whose nodes keep jumping without ever showing a line or a choice.

Default: 200

DialogueRunner

static

Plays a story's nodes - lines, then choices or jumps - for your dialogue UI to show. It draws nothing: ask it for currentLine() or choices(), and tell it advance() or choose(index). Each of start/advance/choose returns a StoryResult: settle the story (StoryState.settle()), save and act on its posts and messages as your game needs.

Parameters

Properties

  • backlog (Array.<StoryLineView>) — Lines shown so far, oldest first, with each choice taken (isChoice true).
  • backlogLimit (integer) — The most entries kept in the backlog.
  • random (function) — Picks the index (0 to count - 1) of a random candidate. Replace it for repeatable tests.

Returns

  • BGE.Story.DialogueRunner

DialogueView

static

Extends: BGE.UI.UiContainer

A dialogue box for a BGE.Story.StorySession: the speaker's name, the current line wrapped and split into pages, and the choices as buttons (disabled ones dimmed).

  • OK turns the page, then moves the conversation on. Up/down move between choices and OK picks one. Back leaves (closeOnBack).
  • It takes all input while it's open, opens a modal (Game.pushModal()) unless modal is false, and closes itself when the conversation ends.
  • Steps go through the session, so posts, messages and saving all happen there.

Colors come from the theme (panelBackgroundColor/Image, panelTitleColor for the speaker, panelTextColor), fonts from speakerFontName/textFontName. It sits along the bottom of the title-safe area unless you set customX/customY/width.

Choices: choiceLayout picks "column" (buttons above the box), "row" (buttons along its bottom) or "list" (lines of text in the box, under the line that asked - see the list* fields). Anything else is yours: override createChoiceButton()/createChoiceRow() for your own widgets and layoutChoices() to place them, or onLine()/onEnded() (or set listener) to react. It's only one way to show a StorySession - a game can drive the session from its own UI instead.

Parameters

  • game (BGE.Game)
  • session (BGE.Story.StorySession)

Properties

  • session (BGE.Story.StorySession)
  • linesPerPage (integer) — Wrapped lines shown at once.
  • boxPadding (float)
  • speakerGap (float) — Space between the speaker and the text.
  • speakerFontName (string)
  • textFontName (string)
  • moreText (string) — Shown in the bottom right while there's another page.
  • choiceLayout (string) — "column": buttons stacked above the box. "row": buttons side by side along the bottom of
  • listKeepsLine (boolean) — "list": keep the speaker and line showing above the choices?
  • listDivider (boolean) — "list": a rule between the line and the choices? Its color; 0 = the theme's panelTitleColor.
  • listDividerColor (integer)
  • listMarker (string) — "list": the focused choice's marker (the others are indented to match).
  • listTextColor (integer) — "list": choice colors; 0 = the theme's panelTextColor / panelTitleColor.
  • listFocusedColor (integer)
  • listDisabledColor (integer)
  • listRowHeight (float) — "list": height of each choice line; 0 = the text font's line height plus 6.
  • choiceHeight (float)
  • choiceGap (float)
  • choiceWidth (float) — Column choices' width; 0 = 60% of the box.
  • showDisabledChoices (boolean) — List choices the story marks disabled (dimmed, not pickable)?
  • closeOnBack (boolean)
  • advanceButton (string) — Buttons (GameInput.isButton() names) that turn the page / leave.
  • cancelButton (string)
  • advanceAction (string) — A Game.controls action (bindAction() name) that also turns the page and picks the
  • choicesWithLastLine (boolean) — Do a node's choices come up with its last page (true), or after one more press of
  • modal (boolean) — Pause the game while open?
  • backgroundImage (dynamic) — Drawn behind the box instead of the theme's panel background, when set (a
  • listener (dynamic) — Optional object told about the conversation: onDialogueLine(view, line) and
  • choiceButtons (Array.<BGE.UI.Button>) — The choice buttons on screen, in story order.
  • speakerLabel (BGE.UI.Label)
  • textLabel (BGE.UI.Label)
  • moreLabel (BGE.UI.Label)
  • pages (Array.<string>)
  • pageIndex (integer)

Returns

  • BGE.Story.DialogueView

Example

CODE
view = new BGE.Story.DialogueView(game, session)
view.start("bram") ' opens if the node has something to say

STORY_SETTLE_MAX_PASSES

staticreadonly

Default: 4

Inventory

static

What the player is carrying: item id -> count, capped by each item's definition.

Parameters

  • definitions (roAssociativeArray) — item id -> ItemDefinition (at least a max)

Returns

  • BGE.Story.Inventory

StoryFlags

static

The story's blackboard: named values (booleans, integers, floats, strings) that story data reads with conditions and changes with effects - quest stages, who you've talked to, what you chose.

Returns

  • BGE.Story.StoryFlags

StorySessionSave

static

Extends: roAssociativeArray

StorySession.toSaveData(): plain values, safe to write with BGE.registryWrite()/SaveSlot.

Properties

  • state (StorySaveData)
  • position (RunnerPosition) — where the conversation is, when one is active
  • backlog (Array.<roAssociativeArray>) — {text, speaker, isChoice} per line, when saved with the backlog

StorySession

static

Extends: BGE.GameEntity

A persistent entity that owns a story's StoryState and DialogueRunner and does the bookkeeping around them, so every story game handles results the same way:

  • apply(result) settles the event rules, sends each post out as a game event, queues messages (takeMessages()), posts changedEventName and calls onStoryChanged() when the state changed - override that (or set onChanged) to autosave.
  • listenTo(eventNames) forwards those game events to the story's event rules.
  • start()/advance()/choose()/runEvent()/applyEffects() run the story and apply() the result for you.
  • toSaveData()/loadSaveData() save the state, position and (optionally) the backlog.

Parameters

  • game (BGE.Game)
  • story (StoryData)
  • args (object, optional, default: "{}") — GameEntity args

Properties

  • story (StoryData)
  • state (StoryState)
  • runner (DialogueRunner)
  • postEvents (boolean) — Send each post effect out with game.postGameEvent()?
  • changedEventName (string) — Game event posted whenever the state changes. "" for none.
  • onChanged (dynamic) — Optional function(session as object) called when the state changes, after
  • pendingMessages (Array.<StoryMessage>)
  • posting (boolean) — set while this session posts events, so it doesn't feed its own posts back in

Returns

  • BGE.Story.StorySession

Example

CODE
session = new BGE.Story.StorySession(game, BGE.Story.loadStory("pkg:/story/main.json"))
session.listenTo(["enemyKilled", "itemFound"])
game.addEntity(session)

StoryState

static

A story's state - flags, the inventory and how often each node was visited - and the conditions and effects its data can use. Built-in vocabulary is in the Story guide; add your game's own with registerCondition() and registerEffect().

Parameters

  • story (StoryData, optional, default: "invalid") — its items and rules are used; may be invalid

Properties

  • flags (StoryFlags)
  • inventory (Inventory)
  • items (roAssociativeArray) — item id -> ItemDefinition, from the story's items
  • rules (Array.<EventRule>)

Returns

  • BGE.Story.StoryState

StoryCondition

static

Extends: roAssociativeArray

One entry in a when list. Identified by its first known key: flag (optionally with eq, gte or lt), notFlag, hasItem, notItem, visited (optionally with gte), notVisited, any - or a key your game registered with StoryState.registerCondition().

Properties

  • flag (string)
  • eq (boolean | integer | string)
  • gte (integer)
  • lt (integer)
  • notFlag (string)
  • hasItem (string)
  • notItem (string)
  • visited (string)
  • notVisited (string)
  • any (Array.<StoryCondition>)

StoryEffect

static

Extends: roAssociativeArray

One entry in an effects list: set (with value), add (with amount), giveItem, takeItem, post, message (with an optional speaker) - or a key your game registered with registerEffect().

Properties

  • set (string)
  • value (boolean | integer | float | string)
  • add (string)
  • amount (integer)
  • giveItem (string)
  • takeItem (string)
  • post (string)
  • message (string)
  • speaker (string)

ItemDefinition

static

Extends: roAssociativeArray

An item the inventory can hold. Add any fields your game needs (name, icon) alongside these.

Properties

  • max (integer) — how many can be carried
  • onAcquire (Array.<StoryEffect>) — effects run each time one is gained

EventRule

static

Extends: roAssociativeArray

A story change that happens on a game event. Every rule whose event, match and when pass runs.

Properties

  • event (string)
  • match (roAssociativeArray) — each key must equal the same field of the event's data
  • when (Array.<StoryCondition>)
  • effects (Array.<StoryEffect>)

StoryLine

static

Extends: roAssociativeArray

A line written as an object. Any other keys (a portrait, a background) are left for your game.

Properties

  • text (string)
  • speaker (string)

StoryChoice

static

Extends: roAssociativeArray

A button at the end of a node. A choice with no label is a fallback, taken automatically when no labelled choice is available. It may also have "goto" or "call" (read with brackets - they're reserved words) and any keys your game reads itself.

Properties

  • label (string)
  • id (string) — names the choice for once (defaults to its label)
  • when (Array.<StoryCondition>)
  • once (boolean)
  • showDisabled (boolean) — listed but not selectable when when fails, instead of hidden
  • effects (Array.<StoryEffect>)

StoryNode

static

Extends: roAssociativeArray

One node: lines to show, then effects, then choices, "call", "goto" or "end" (bracket keys).

Properties

  • when (Array.<StoryCondition>)
  • once (boolean)
  • speaker (string) — the speaker of lines that don't name their own
  • lines (Array.<dynamic>) — each a string or a StoryLine
  • effects (Array.<StoryEffect>)
  • choices (Array.<StoryChoice>)

StoryData

static

Extends: roAssociativeArray

A whole story. Each node id maps to a StoryNode, a list of candidate StoryNodes (the first whose when passes is used), or {select: "first" | "random", candidates: StoryNode[]}.

Properties

  • nodes (roAssociativeArray)
  • rules (Array.<EventRule>)
  • items (roAssociativeArray) — item id -> ItemDefinition

StoryMessage

static

A message effect: text to show, and who says it ("" for nobody).

Properties

  • text (string)
  • speaker (string)

StoryResult

static

What a batch of effects or rules did.

Properties

  • changed (boolean) — true if any story state changed (save, and settle the rules)
  • posts (Array.<string>) — event names from post effects, in order
  • messages (Array.<StoryMessage>)

StorySaveData

static

Extends: roAssociativeArray

StoryState.toSaveData(): plain values, safe to write with BGE.registryWrite().

Properties

  • flags (roAssociativeArray)
  • inventory (roAssociativeArray)
  • visits (roAssociativeArray)

StoryLineView

static

A line as DialogueRunner shows it.

Properties

  • text (string) — with ${flags} filled in
  • speaker (string)
  • line (roAssociativeArray) — the line as written ({text} for a plain string), for your game's own keys
  • isChoice (boolean) — true for a choice the player took (in the backlog)

ChoiceView

static

A choice as DialogueRunner lists it.

Properties

  • label (string) — with ${flags} filled in
  • enabled (boolean) — false for a showDisabled choice whose when fails
  • choice (StoryChoice) — the choice as written, for your game's own keys
  • index (integer) — its index in the node's choices

StoryHandler

static

A registered condition or effect.

Properties

  • fn (function)
  • context (dynamic)

RunnerFrame

static

One node in the runner's stack (internal).

Properties

  • key (string) — the node's visit key: an id, or "#" for a candidate ("" for startNode())
  • node (StoryNode)
  • line (integer)
  • shownLine (integer) — the last line added to the backlog
  • phase (string) — lines, choices, choosing, choiceCall, call, afterCall, runOut

RunnerFrameData

static

A saved RunnerFrame.

Properties

  • key (string)
  • line (integer)
  • phase (string)

RunnerPosition

static

Extends: roAssociativeArray

DialogueRunner.getPosition(): plain values, safe to save.

Properties

SelectedNode

static

A node picked by selectNode().

Properties