Godot Version
4.7.2
Question
im triying to make a list of questions with a dictionary the question should have an answer and a topic to create other options based on the topic.
example
"question": "answer,"topic"
"who are you" : "juan", "name"
is there a way to do it or should i find another method?
Dictionary values can be arrays or objects.
Ye, the value can be another dictionary, so each question carries both:
var questions = {
"who are you": {"answer": "juan", "topic": "name"},
"where do you live": {"answer": "madrid", "topic": "place"},
}
func _ready() -> void:
var q = questions["who are you"]
print(q.answer) # juan
print(q.topic) # name
For the other options you can collect every answer with the same topic:
func answers_for_topic(topic: String) -> Array:
var out = []
for question in questions:
if questions[question].topic == topic:
out.append(questions[question].answer)
return out
Once you have a lot of questions I’d move them into a JSON file so you’re not editing a huge dictionary in code, but for starting out this works fine