Dialogue Engine structures conversations as dialogue trees - sequences of dialogue entries that flow from one to another. Each tree is composed of individual entries that can be text-based or conditional, allowing you to create both linear and branching conversations.
Text entries are the most common type of dialogue entry:
var dialogue_engine = DialogueEngine.new()# Add a simple text entrydialogue_engine.add_text_entry("Hello, traveler!")dialogue_engine.add_text_entry("Welcome to our village.")dialogue_engine.add_text_entry("Press <Enter> to continue.")
# Get entry by IDvar entry = dialogue_engine.get_entry(0)# Get entry by namevar named_entry = dialogue_engine.add_text_entry("Important dialogue")named_entry.set_name("greeting")var retrieved = dialogue_engine.get_entry_with_name("greeting")
Dialogue trees are stored internally as an array of dictionaries, with each entry accessible through its unique ID. The engine maintains a read needle that tracks the current position in the tree.
var entry = dialogue_engine.add_text_entry("Sample text")# Get the entry's unique IDvar id = entry.get_id()# Get the entry's branch IDvar branch_id = entry.get_branch_id()# Get the entry's textvar text = entry.get_text()# Check if entry has textif entry.has_text(): print(text)
The engine walks through the dialogue tree using the advance() method:
var dialogue_engine = DialogueEngine.new()dialogue_engine.add_text_entry("First line")dialogue_engine.add_text_entry("Second line")# Connect to the signal to receive dialoguevar print_dialogue = func(dialogue_entry: DialogueEntry) -> void: print(dialogue_entry.get_text())dialogue_engine.dialogue_continued.connect(print_dialogue)# Advance through the dialoguedialogue_engine.advance() # prints "First line"dialogue_engine.advance() # prints "Second line"dialogue_engine.advance() # dialogue finishes, resets automatically
The engine provides several utility methods for managing the dialogue tree:
# Check tree sizevar entry_count = dialogue_engine.size()# Check if tree is emptyif dialogue_engine.is_empty(): print("No dialogue entries")# Reset the read positiondialogue_engine.reset()# Clear all entriesdialogue_engine.clear()# Get current entryvar current = dialogue_engine.get_current_entry()if current: print(current.get_text())
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> or <Space> to exit.")
Always connect to dialogue_continued or entry_visited signals before calling advance() to ensure you receive the dialogue content.
The dialogue tree automatically resets when it finishes. If you need to restart dialogue, simply call advance() again - it will start from the beginning.