Once you’ve written your dialogue, you’ll need to integrate it into your game. Dialogue Manager provides the DialogueManager singleton and DialogueLine class to request and display dialogue lines programmatically.
~ startNathan: Hi! I'm Nathan.Nathan: Here are some options.- First one Nathan: You picked the first one.- Second one Nathan: You picked the second one.
The first call to get_next_dialogue_line(resource, "start") would return a DialogueLine containing Nathan: Hi! I'm Nathan.
To continue the dialogue, use the next_id property from the current line:
# Get the first linevar dialogue_line = await DialogueManager.get_next_dialogue_line(resource, "start")# Get the next linedialogue_line = await DialogueManager.get_next_dialogue_line(resource, dialogue_line.next_id)
When a line contains response options, each option has its own next_id property to continue along that branch:
if dialogue_line.responses.size() > 0: # Show response options to the player for response in dialogue_line.responses: print(response.text) # After player selects a response: var selected_response = dialogue_line.responses[0] dialogue_line = await resource.get_next_dialogue_line(selected_response.next_id)
Use the helper methods to check for and retrieve tag values:
# Check if a line has a tagif dialogue_line.has_tag("voice"): var voice_file = dialogue_line.get_tag_value("voice") audio_player.stream = load(voice_file) audio_player.play()
@onready var dialogue_resource = preload("res://dialogue/npc_conversation.dialogue")
2
Request the first line
func start_dialogue(): var line = await DialogueManager.get_next_dialogue_line(dialogue_resource, "greeting") show_dialogue(line)
3
Handle dialogue display and advancement
func show_dialogue(line: DialogueLine): if line == null: # Dialogue ended end_dialogue() return # Display character name and text character_label.text = line.character dialogue_label.text = line.text # Check for responses if line.responses.size() > 0: show_response_options(line.responses) else: # Wait for player input to continue await advance_button.pressed var next_line = await dialogue_resource.get_next_dialogue_line(line.next_id) show_dialogue(next_line)
Pass game state objects to make them available in dialogue conditions:
var player = get_node("/root/Player")var inventory = get_node("/root/Inventory")var line = await DialogueManager.get_next_dialogue_line( dialogue_resource, "start", [player, inventory])
In your dialogue, you can now reference these objects:
~ startif player.level >= 5 Nathan: You're quite experienced!else Nathan: You're still learning.if inventory.has_item("key") Nathan: I see you found the key!
When a line contains response options, present them to the player:
func show_response_options(responses: Array): # Clear previous options for child in response_container.get_children(): child.queue_free() # Create buttons for each response for response in responses: var button = Button.new() button.text = response.text button.pressed.connect(func(): on_response_selected(response)) response_container.add_child(button)func on_response_selected(response: DialogueResponse): var next_line = await dialogue_resource.get_next_dialogue_line(response.next_id) show_dialogue(next_line)
Create dialogue resources dynamically using create_resource_from_text():
var dialogue_text = """~ greetingMerchant: Welcome to my shop!- What do you sell? Merchant: I have potions and weapons.- Goodbye Merchant: Come back soon!"""var resource = DialogueManager.create_resource_from_text(dialogue_text)if resource: var line = await resource.get_next_dialogue_line("greeting") show_dialogue(line)
The method will fail if there are syntax errors in the dialogue text. The resource is ephemeral and won’t be saved to disk.