NarrativeInterpreter class
NarrativeInterpreter - Core logic engine for sentence execution
A service responsible for interpreting and executing individual BaseNarrativeModel instructions
during in-game conversations.
It decouples the engine's core loop from UI and system concerns (e.g., TTS, navigation),
and instead delegates execution logic via injected handlers such as onRead, onWrite, etc.
Key Responsibilities:
- Parse and interpret the
instructionfield from aBaseNarrativeModel - Trigger one or multiple handlers based on the instruction contents:
read- Read aloud via TTSwrite- Display message in chatask- Ask the player a question and wait for answernavigate- Forward to a new screen and waitwait- Await user input (e.g. "Tap to continue")
- Support chaining or combining instructions (e.g.
read + write) - Expose a fallback
onUnhandledif none of the above matched
Usage Example:
final interpreter = NarrativeInterpreter(
engine: sentenceEngine,
onRead: (text) => ttsService.speak(text),
onWrite: (s) => chatBox.write(s),
onAsk: (s) => ui.askQuestion(s),
);
await interpreter.interpret(sentence);
Constructors
-
NarrativeInterpreter({required NarrativeEngine engine, Future<
void> onRead(String text)?, Future<void> onWrite(BaseNarrativeModel sentence)?, Future<void> onAsk(BaseNarrativeModel sentence)?, Future<void> onWait(BaseNarrativeModel sentence)?, Future<void> onUnhandled(BaseNarrativeModel sentence)?}) - Constructor with injected behavior handlers.
Properties
- currentPhase ↔ String
-
Tracks the current phase for navigation control.
getter/setter pair
- engine → NarrativeEngine
-
Reference to the parent engine for control flow operations.
final
- hashCode → int
-
The hash code for this object.
no setterinherited
-
onAsk
→ Future<
void> Function(BaseNarrativeModel sentence)? -
Callback to show a question prompt and await a response.
final
-
Callback to trigger navigation and wait for resume.
final
-
onRead
→ Future<
void> Function(String text)? -
Callback to read the sentence text using TTS.
final
-
onUnhandled
→ Future<
void> Function(BaseNarrativeModel sentence)? -
Called when the sentence instruction does not match any known type.
final
-
onWait
→ Future<
void> Function(BaseNarrativeModel sentence)? -
Callback to pause until the user taps or resumes manually.
final
-
onWrite
→ Future<
void> Function(BaseNarrativeModel sentence)? -
Callback to write the sentence to the chat UI.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
interpret(
BaseNarrativeModel sentence) → Future< void> - Interprets a single BaseNarrativeModel and executes the defined handlers
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited