Create dynamic dialogue that changes based on game state
This example demonstrates how to create dialogue that branches based on conditions using conditional entries. The dialogue will change depending on whether the player has talked to the NPC before.
Branch IDs are used to organize different dialogue paths. While not strictly necessary in this simple example, they help keep dialogue organized in more complex trees.
Branch ID 0 is the default branch (DEFAULT_BRANCH_ID). All entries without an explicit branch ID are added to this branch.
True branch: “Hey! We meet again!” (shown if condition is true)
False branch: “It’s nice to meet you!” (shown if condition is false)
Set goto IDs: Links the condition to the appropriate branches
Final text: Exit message (always shown after either branch)
The set_condition_goto_ids() method takes two parameters: the entry ID for when the condition returns true, and the entry ID for when it returns false.
You can chain multiple conditional entries for complex branching:
func _setup() -> void: add_text_entry("Hello, adventurer!") # First condition: check if player is high level var level_check: DialogueEntry = add_conditional_entry(__is_high_level) # High level branch var high_level_entry: DialogueEntry = add_text_entry("A powerful warrior!", branch.HIGH_LEVEL) # Low level branch - check if player has quest item var item_check: DialogueEntry = add_conditional_entry(__has_special_item, branch.LOW_LEVEL) var has_item: DialogueEntry = add_text_entry("I see you have the amulet!", branch.HAS_ITEM) var no_item: DialogueEntry = add_text_entry("Come back when you're stronger.", branch.NO_ITEM) # Set up the condition branches level_check.set_condition_goto_ids(high_level_entry.get_id(), item_check.get_id()) item_check.set_condition_goto_ids(has_item.get_id(), no_item.get_id()) # Merge back to common dialogue var common_end: DialogueEntry = add_text_entry("Farewell!") high_level_entry.set_goto_id(common_end.get_id()) has_item.set_goto_id(common_end.get_id()) no_item.set_goto_id(common_end.get_id())func __is_high_level() -> bool: return player.level >= 10func __has_special_item() -> bool: return player.inventory.has("ancient_amulet")