Skip to main content
Version: 1.3

Using Dialogues

Once you have created a dialogue, you can use it in different ways, as we have already seen in the create a dialogue section, but there are more details you should keep in mind.

How to use a dialogue​


To use the dialogues from Sprouty Dialogs in your game, as we have already seen in the create a dialogue section, we have the two following options.

The recommended way to run your dialogues is using dialog players. To use a DialogPlayer node, first you need to add it to the scene where you want to run the dialogue.

Add Dialog Player

Then, you must assign the Dialog Data or dialogue data file where your dialogue is.

Select Dialogue Data

When you select a file, the inspector will automatically update and now you will see some settings for the DialogPlayer.

You must choose the dialogue tree ID of the dialogue you want to play from the Start ID dropdown; otherwise, the player will not know which dialogue tree to run.

Select Start ID

Now that you have your dialog player configured, you can call the dialogue from code, using the play() method (or start() on older versions) from the DialogPlayer node.

For example, we can add a script to the main node and add a reference to the DialogPlayer that we create before to then call the play() method where we want in the script.

main.gd
extends Node2D

@onready var dialog_player : DialogPlayer = $DialogPlayer

func _ready() -> void:
dialog_player.play()

In this case, we run the dialog in the _ready() method of the main node or when the scene begins again, so when you run the scene you will see the dialogue.

Play from Code

And that's it! So easy right? 🥳

You can also create a dialog player node from code and use it in the same way, but you must first add it to the tree and configure the dialogue data file and start ID before running it.

main.gd
extends Node2D

var dialog_data := load("res://dialogues/my_dialogue.tres")

func _ready() -> void:
var dialog_player = DialogPlayer.new()
add_child(dialog_player)

dialog_player.set_dialog(dialog_data, "FIRST")
dialog_player.play()

Using SproutyDialogs autoload​

Otherwise, instead of create a DialogPlayer node in the scene, you can play a dialogue using the Sprouty Dialogs Autoload. You can call the play() method (or start_dialog() on older versions) from the SproutyDialogs autoload passing the dialogue data resource and the ID of the dialogue tree you want to run.

main.gd
extends Node2D

var dialog_data := load("res://dialogues/my_dialogue.tres")

func _ready() -> void:
SproutyDialogs.play(dialog_data, "FIRST")

And in this way you can also see the dialogue when you run the scene.

Play from Autoload

Important

Both options run the dialogues by loading all the resources needed at once when the dialogue is set: with the set_dialog() method when creating a dialog player from code, or with the play() method of the Sprouty Dialogs autoload. This can cause slowdowns if large resources need to be loaded.

For more details see the about dialogue resources section.

About dialogue resources​


When you set a DialogPlayer node in a scene to run a dialogue, this node will load all the resources needed (dialog boxes, characters, portraits, etc) on ready, using the Resource Manager singleton.

The Resource Manager is going to keep track of the resources required for all the dialogues that will run in the current scene, which means, the resources needed by all the dialog players in the scene. The resource manager ensures that each resource is only loaded once, even if more than one dialog player uses it.

This way, resources are loaded only once at the start of the scene and then instantiated when needed in a dialog. Furthermore, they are destroyed when they are no longer needed.

For this reason, care must be taken when loading large resources, such as complex portrait scenes. If you set the dialog players from the code or use the Sprouty Dialogs autoload, the resources will be loaded when you call the set_dialog() or start_dialog() methods.

If you call these methods during runtime, there will be a slowdown due to resource loading. However, if you have set the DialogPlayer node directly in the scene, you will ensure that resources are loaded when the scene starts.

Pausing, resuming and stopping dialogues​


Sometimes you need more control over the dialogue execution, for example, to pause the dialogue while a menu is open, or to stop it when the player leaves the interaction area. For this, the DialogPlayer node provides the following methods:

  • pause(): Pauses the processing of the dialogue tree, which also pauses the typing animation of the current DialogBox if there is one being displayed, and emits the dialog_paused signal.
  • resume(): Resumes the processing of the dialogue tree from the exact point where it was paused (including its typing animation), and emits the dialog_resumed signal.
  • stop(): Stops the dialogue, exiting all the active portraits, freeing the displayed dialog boxes and portraits, and emitting the dialog_ended and dialog_player_stop signals. If the player has the free_on_end property enabled, it will also release the dialogue resources and free itself.
main.gd
extends Node2D

@onready var dialog_player : DialogPlayer = $DialogPlayer

func _on_menu_opened() -> void:
dialog_player.pause()

func _on_menu_closed() -> void:
dialog_player.resume()

func _on_player_left_shop_area() -> void:
dialog_player.stop()

You can also check if a dialogue is currently running using the is_running() method.

Important

The dialog player is the node that handles the execution of the dialogue, not the dialog box, so you must call these methods on the DialogPlayer node, not on its dialog box.

The DialogBox is only the visual component that displays the current text, while the DialogPlayer is the one processing the dialogue tree and deciding what is displayed next.

This also means that pausing or resuming the player already takes care of pausing or resuming its current dialog box, so there is no need to handle the dialog box separately.

Using DialogPlayer signals​


The DialogPlayer nodes have some signals that you can use to handle the state of the dialogue. These signals are the following:

## Emitted when the dialog starts.
signal dialog_started()
## Emitted when the dialog is paused.
signal dialog_paused()
## Emitted when the dialog is resumed.
signal dialog_resumed()
## Emitted when the dialog is ended.
signal dialog_ended()
## Emitted when the dialog player stops.
signal dialog_player_stop()

The dialog_paused and dialog_resumed signals are emitted when you call the pause() and resume() methods, and the dialog_ended and dialog_player_stop signals when the dialogue ends or is stopped. Check the pausing, resuming and stopping dialogues section for more details.

Also, there are two special signals:

## Emitted when a dialog option is selected.
signal option_selected(option_index: int, option_dialog: Dictionary)
## Emitted when a signal event is emitted.
signal signal_event(signal_id: String, args: Array)

First, the option_selected signal is emitted when an option is selected in the dialogue and returns the selected option index (starting from 1) and a dictionary with the dialogue information and the translations of the selected option in the following format:

{
"en" : "This is the option in English",
"es" : "Esta es la opción en Español"
}

On the other hand, the signal_event signal is emitted when a Signal Node is triggered in the dialogue tree. This signal returns the argument that you give to the event node to do whatever you need with it.

You can also handle the same signals from the Sprouty Dialogs autoload.

main.gd
extends Node2D

var dialog_data := load("res://dialogues/example.tres")

func _ready() -> void:
SproutyDialogs.dialog_ended.connect(_on_dialog_ended)
SproutyDialogs.play(dialog_data, "EXAMPLE")

func _on_dialog_ended() -> void:
SproutyDialogs.dialog_ended.disconnect(_on_dialog_ended)
print("The dialogue has ended!")