In this quickstart guide, you’ll create a simple dialogue system from scratch. We’ll start with a basic conversation, then add player choices to make it interactive.
In Godot, create a new scene with a Node as the root. Save it as my_dialogue.tscn.Add a RichTextLabel node as a child to display the dialogue text.Your scene tree should look like:
Node (my_dialogue)└── RichTextLabel
2
Create the Dialogue Script
Create a new GDScript that extends DialogueEngine. This script will define your dialogue tree.Create my_dialogue_data.gd:
extends DialogueEnginefunc _setup() -> void: add_text_entry("Hey...") add_text_entry("Have you seen the code for this sample?") add_text_entry("It's beautiful!") add_text_entry("You won't believe it!") add_text_entry("Press <Enter> to continue.")
The _setup() function is called automatically when the DialogueEngine is initialized. This is where you build your dialogue tree.
3
Create the Display Script
Now create a script for your scene that will display the dialogue.Attach this script to the root Node:
extends Node@onready var text_label : RichTextLabel = $RichTextLabelvar dialogue_engine : DialogueEnginefunc _ready() -> void: # Create the dialogue engine instance dialogue_engine = preload("res://my_dialogue_data.gd").new() # Connect to the dialogue_continued signal dialogue_engine.dialogue_continued.connect(_on_dialogue_continued) # Connect to the dialogue_finished signal dialogue_engine.dialogue_finished.connect(_on_dialogue_finished) # Start the dialogue dialogue_engine.advance()func _on_dialogue_continued(entry: DialogueEntry) -> void: # Display the dialogue text text_label.text = entry.get_text()func _on_dialogue_finished() -> void: # Handle dialogue completion text_label.text = "[center]Dialogue finished![/center]"func _unhandled_input(event: InputEvent) -> void: # Advance dialogue on Enter or Space if event.is_action_pressed("ui_accept") or event.is_action_pressed("ui_select"): dialogue_engine.advance()
4
Run Your Scene
Run the scene (F6). You should see the first line of dialogue appear. Press Enter or Space to advance through the conversation.Congratulations! You’ve created your first dialogue with Dialogue Engine!
Now let’s make the dialogue interactive with player choices!
1
Update the Dialogue Data
Modify my_dialogue_data.gd to include player choices:
extends DialogueEngineenum { DEFAULT_BRANCH = 0, SLEEP_BRANCH = 1, WORK_BRANCH = 2,}func _setup() -> void: var entry: DialogueEntry = add_text_entry("The storm rages outside. I should...") # Add player options var option_sleep: int = entry.add_option("Go back to sleep.") var option_work: int = entry.add_option("Get back to work.") # Create responses for each choice var sleep_response: DialogueEntry = add_text_entry( "That's right, sleep is for the strong!", SLEEP_BRANCH ) var work_response: DialogueEntry = add_text_entry( "That's right, let's get back to work!", WORK_BRANCH ) # Link options to their responses entry.set_option_goto_id(option_sleep, sleep_response.get_id()) entry.set_option_goto_id(option_work, work_response.get_id()) # Merge branches back together var continue_entry: DialogueEntry = add_text_entry("Some time passes...") sleep_response.set_goto_id(continue_entry.get_id()) work_response.set_goto_id(continue_entry.get_id()) add_text_entry("The end.")
2
Update the Display Script
Add UI elements to display options. Update your scene to include buttons:
# Emitted when the first entry is readdialogue_engine.dialogue_started.connect(_on_dialogue_started)func _on_dialogue_started() -> void: print("Dialogue has started!")