For ordered data which you know you will be iterating through, an Array is appropriate. In this case, your dictionary key can be the name of the speaker, and the dictionary’s value is the Array containing the lines of dialog.
var dialog_dictionary := {
"Marle": [
"Crono?",
"What a nice name!",
"Pleased to meet you!",
#And so on and so forth...
],
}
In ordering your data in this way, you can access lines of dialog using the syntax dialog_dictionary["Marle"][1]
, with the number being the line number of dialog you’d like to use.
In doing so, you can use something like the following for loop to iterate through the data.
for line in dialog_dictionary["Marle"]:
print(line) #This will print all of Marle's lines.
# Alternatively, if you want to print out Marle's name with each line:
for line in dialog_dictionary["Marle"]:
print("{character_name}: {dialog}".format({
"character_name": "Marle",
"dialog": line,
}))
# Of course, if we want to substitute the speaker's name with a variable
var speaker = "Marle"
for line in dialog_dictionary[speaker]:
print("{character_name}: {dialog}".format({
"character_name": speaker,
"dialog": line,
}))
These are all ways to access the contents of a dictionary containing an array using various levels of indirection.
With all of that being said, for the purposes of dialog, using a dictionary containing the names of characters as keys and their dialog as values is a naive implementation, but it may suit your particular use case, depending on what you need. If you need something more robust, I would recommend creating a class with the speaker’s name and dialog as attributes.
class CharacterDialog:
var speaker: String
var dialog: Array[String]
func _init(p_speaker: String, p_dialog: Array[String]) -> void:
speaker = p_speaker
dialog = p_dialog
func print_lines() -> void:
for line in dialog:
print("{speaker}: {line}".format({
"speaker": speaker,
"line": line,
}))
The idea above could be abstracted even further out into a resource which could be modified in the editor using @export
, but this is beyond the scope of the example; left as an exercise for the reader.
I hope that these examples help to inform you in achieving what you seek to do with your dialog system.
Regards,