Reference dialouge by index

Godot Version

4.3

Question

this is my code in my dialogue script, what I want to do is have a system where when the static text shows, it moves to the next one, but sometimes there will be conditional text, like tutorial text which has a timer and shows UI, right now it’s working halfway, my last index text takes all the logic to itself, logic works but it can’t reference correctly to the right index, so my tutorial is index 1 in the array, how can I reference it so that the logic will work on that only and not on the last index

func show_ui_prompt_callback() -> void:
	if current_dialogue_index == 1:  # Check if it's the tutorial text (second dialogue)
		hide_tutorial_text(9.0)  
		show_ui_prompt()  

func hide_tutorial_text(delay: float) -> void:
	await get_tree().create_timer(delay).timeout
	text_label.visible = false


func print_completion_message() -> void:
	print("Dialogue complete. Ready for the next phase!")

# Array of dialogues
var dialogues = [
	{
		"text": "Welcome! My name is the one. I'm glad you have arrived.\nI'm going to teach you the basics of this world and how to survive.",
		"on_finish": null  # No special action on finish
	},
	{
		"text": "Press any key to move.",
		"on_finish": show_ui_prompt_callback  # Reference to the prompt callback
	},
	{
		"text": "Great! You now know how to move. Let's proceed with the adventure.",
		"on_finish": print_completion_message  # Reference to the print callback
	}
]

If you want to reference index 1 in the dialogues array, that would be dialogues[1]

So if you do
print(dialogues[1])
it would print:

	{
		"text": "Press any key to move.",
		"on_finish": show_ui_prompt_callback  # Reference to the prompt callback
	},

There are two things unclear about your post.

  1. What do you mean by

And 2, what do you mean by

Some more information regarding this would be helpful.

my show_ui_prompt suppose to work on index 1 text: “press any key”
right now it cant reference to it, so the last index text: “text”: “Great! You now know how to move. Let’s proceed with the adventure.” takes the logic to itself, but I give a shot to your way

So this call to the show_ui_prompt() should show the text of the dialogue at index 1? Can you show me the show_ui_prompt function?

When show_ui_prompt is called, you can do this:
$myLabel.text = dialogues[1]["text"] to show the text from index 1

Or alternatively:
$myLabel.text = dialogues[1].get("text","")

To be honest, I think I’d suggest changing “dialogues” from an Array to a Dictionary. Since you want to find a specific set of values, and you’re having issues with getting the correct values, it might be worth changing over. This is achieved at a minimal level like below:

var dialogues: Dictionary = {
	# We now have each "text"/"on_finish" dictionary under an identifiable key
	"welcome": {
		"text": "Welcome! My name is the one. I'm glad you have arrived.\nI'm going to teach you the basics of this world and how to survive.",
		"on_finish": null  # No special action on finish
	},
	# other dictionaries
}

The above is accessed in the forms,

  • dialogues.get("welcome", {})
    Returns an empty Dictionary if there’s no matching key

  • dialogues["welcome"]
    Throws an error if there’s no matching key

If you add and use an enum alongside this, it actually makes life a LOT easier organizationally. When we do it this way, we’re avoiding any typos in keys, as well as no longer needing to worry as much about Array order.

# The keys for the "dialogues" Dictionary
enum TutorialText {WELCOME, TEACH_MOVE, PROCEED} 

var dialogues: Dictionary = {
	TutorialText.WELCOME: {"text": ..., "on_finish": ...},
	TutorialText.TEACH_MOVE: {...},
	TutorialText.PROCEED: {...}
} # i.e. "dialogues.get(TutorialText.WELCOME, {})"

We can then set up an Array of TutorialText enum values and create a sequence of dialogue like the following:

var dialogue_1: Array[int] = [
	TutorialText.WELCOME,
	TutorialText.TEACH_MOVE, 
	TutorialText.PROCEED]

We then give it to get_conversation(...) like below to create the Array

func _ready() -> void: # Or whatever function you need this in
	var conversation: Array[Dictionary] = get_conversation(dialogue_1)
	for main_line in conversation: # Print all text from conversation
		print(main_line.get("text", "no main text!"))


# This sequence can be any length and contain repeats
func get_conversation(sequence: Array[int]) -> Array[Dictionary]:
	var dialogue_info: Array[Dictionary]
	for code in sequence:
		dialogue_info.append(dialogues.get(code, {}))
	return dialogue_info

This prints the sequence, and can super easily be changed to incorporate that other bit of functionality you’re talking about. I hope that all helps!! :slight_smile:

1 Like

i will try it and see what i get

the show_ui_prompt is a func that simply has ui.visible = true and timer to hide tutorial text after timer run out

Your method worked better for me, i implemented a dictionary with enum, not only its more organized, but I can reference things better, thx

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.