Attach custom data to dialogue entries for character names, emotions, animations, and more
This example demonstrates how to use metadata to attach custom data to dialogue entries. Metadata allows you to store information like character names, emotions, animations, voice lines, and any other custom data your game needs.
Let’s start with a simple example that adds an author name to dialogue:
extends DialogueEnginefunc _setup() -> void: # Use DialogueEntry.set_metadata for data that must be available through DialogueEngine.dialogue_continued signal. # The metadata handling per DialogueEntry must be implemented by the user. add_text_entry("[i]We won! Let's goooo!![/i]").set_metadata("author", "Gary") add_text_entry("Press <Enter> or <Space> to exit.")
set_metadata() returns the DialogueEntry, allowing you to chain it directly after add_text_entry().
Here’s a more advanced example that uses metadata for getting player input:
extends DialogueEnginevar player_name: String # will be set by the UI codefunc _setup() -> void: add_text_entry("Welcome adventurer. May I know you name?").set_metadata(&"get_player_name", "The UI code will act accordingly and inject player_name into DialogueEngine.") add_text_entry("The legendary %s!? Please, follow me this way. I will personally show you our guild.").set_format([get.bind("player_name")], DialogueEntry.FORMAT_OPERATOR) add_text_entry("Press <Enter> or <Space> to exit.").set_name("Exit")
This example uses set_format() with a callable to dynamically insert the player name. The metadata get_player_name signals to the UI that it should prompt for the player’s name.
add_text_entry("Thank you for helping me!") \ .set_metadata("complete_quest", "help_villager") \ .set_metadata("give_reward", {"gold": 100, "xp": 50})
In your UI:
func __on_dialogue_continued(entry: DialogueEntry) -> void: if entry.has_metadata("complete_quest"): var quest_id = entry.get_metadata("complete_quest") quest_manager.complete_quest(quest_id) if entry.has_metadata("give_reward"): var rewards = entry.get_metadata("give_reward") if rewards.has("gold"): player.gold += rewards["gold"] if rewards.has("xp"): player.add_xp(rewards["xp"])
Metadata can control UI behavior, like hiding certain options:
func _setup() -> void: var entry: DialogueEntry = add_text_entry("The storm rages right outside the window. I should...") var option_id_1: int = entry.add_option("Wait for storm to finish.") var option_id_2: int = entry.add_option("Go back to sleep.") var option_id_3: int = entry.add_option("Get back to work.") var option_id_4: int = entry.add_option("Hidden option -- this should not be shown on the UI") entry.set_metadata("dont_show_options", [option_id_4]) entry.set_metadata("auto_choose", option_id_4)
In your UI:
func __on_dialogue_continued(entry: DialogueEntry) -> void: if entry.has_options(): var dont_show: Array = entry.get_metadata("dont_show_options", []) for option_id in range(entry.get_option_count()): if option_id in dont_show: continue # Show this option button create_option_button(option_id)