OBJ DialogueSystem
-signed Macha-latte
Overview
-
The dialogue system is a single
DialogueBoxscene that gets created whenstart_dialoguenode is called and destroyed when the dialogue ends. -
The dialogue system shows conversations written in Yarn Spinner. Any script in the game starts one with a single call
-
It is made of three parts:
- Yarn Spinner (the addon) reads the
.yarnfiles and decides which line or choice comes next. - DialogueManager (an autoload) is the only way to open dialogue. It makes sure only one dialogue box is open at a time.
- DialogueBox (a scene) is created fresh for every conversation and deletes itself when the conversation ends. It holds the Yarn runner, the presenters and the visuals.
- Yarn Spinner (the addon) reads the
Requirements
- Open and close
- Typewriter text
- Advance with Z
- Show questions
- Pick answers with the keyboard only
- No mouse input for questions
- Delete the box when dialogue ends
- Only one box at a time
- Reopen fresh every time
- Start from a function call with a Yarn node name
Setup
- The addon is in
addons/yarn_spinner/. - Compiling
.yarnfiles needs the .NET SDK and theYarnSpinner.Consoletool (ysc) installed globally. Restart Godot after installing them. - All
.yarnfiles are collected by dialogue/Project.yarnproject. A.yarnfile that isn't included in the project won't compile. DialogueManagermust be registered as an autoload in Project Settings → Globals → Autoload, with the nameDialogueManager.
Files
Structure
DialogueBox (CanvasLayer)
|--YarnDialogueRunner
| |-YarnLinePresenter
| |_YarnOptionsPresenter
|
|__PortraitObject
The presenters wont draw anything themselves, they are pointed at nodes inside PortraitObject
| Presenter setting | Points at |
| --- | --- |
| Line presenter → text_label | PortraitObject/Box/TextLabel |
| Line presenter → character_label | PortraitObject/NameTag/CharacterLabel |
| Line presenter → character_container | PortraitObject/NameTag |
| Line presenter → continue_indicator | PortraitObject/Box/ContinueIndicator |
| Options presenter → options_container | PortraitObject/Box/OptionsContainer |
Renaming any of these nodes WILL break the box. Update the paths to the presenters before renaming
Starting dialogue
DialogueManager has two functions:
Example: starting dialogue when the player talks to someone:
func interact() -> void:
DialogueManager.start_dialogue("Start")
Example, stopping the player from moving during dialogue:
func _physics_process(delta: float) -> void:
if DialogueManager.is_dialogue_active():
return
# movement code
start_dialogue does nothing if:
- a box is already open, or
- a box closed less than 200 ms ago (see Flow).
Flow
What happens from the call to the box disappearing:
-
Something calls
DialogueManager.start_dialogue("Start"). -
The manager checks that no box is open and that the last one closed more than 200 ms ago. If either check fails, it stops here.
-
The manager creates a new
DialogueBox, adds it as its own child and callsstart()on it. -
The box tells
YarnDialogueRunnerto start the node. -
For each line, the runner sends the line to
YarnLinePresenter, which types it out at 15 words per second. Pressing Z while it types finishes the line. Pressing Z again goes to the next line. -
At the same time, the box hears the line presenter's
line_startedsignal and tells PortraitObject who is talking, so it can show the right portrait and name. (See OBJ PortraitObject.) -
For choices, the runner sends them to
YarnOptionsPresenter, which creates one option button per choice. The player moves with up/down and picks with Z. -
When the Yarn node ends, the runner fires
dialogue_completed(ordialogue_cancelledon an error). The box deletes itself withqueue_free(). -
The manager notices the box left the tree, clears its reference and remembers the time.
Why are we waiting for 200 ms: the same Z press that closes the last line could also count as "talk to the NPC" and open the dialogue again straight away. The short wait stops that.
Why a new box every time: every conversation starts clean, with no leftover text or buttons from the last one.
Input
dialogue_acceptis set as the line presenter'scontinue_actionand is used by the option button script.- It's a separate action from
ui_acceptbecause other menus useui_accept(Enter/Space), and dialogue shouldn't react to those. - The mouse is ignored: the line presenter has
click_anywhere_to_continueoff, and the option buttons have their mouse filter set to Ignore. - The option button (
dialogue_option_button.gd) reads the input itself: Z presses the focused button, and up/down move focus to the button above or below.
Writing dialogue
Dialogue lives in .yarn files inside dialogue/. for example referring to test_talk.yarn:
title: Start
---
Dyno: Wazzap?!
Dyno: Do you have some kind of explanation.
-> I opened an exe file.
<<jump Beginning_Truth>>
-> I walked right on in.
<<jump Beginning_Lie>>
===
title:is the node name. This is what you pass tostart_dialogue.Name: textis one line. The name before the colon is the speaker. It must match a character'syarn_namefor the portrait and name tag to show (see OBJ PortraitObject).->is a choice. The indented lines under it run when it's picked.<<jump NodeName>>goes to another node.===ends the node.
For everything else, see the Yarn Spinner docs.
Testing
- Open dialogue/dialogue_test.tscn and run it with.
- Dialogue starts on its own from the node set in the test node's
start_node. - When it ends, press T to start it again.