How do I handle Questions and Answers for a simple Trivia Game?

Godot Version

4.7

Question

Hi everyone,

I was just wondering if anyone could help me figure out the best way to handle implementing questions and answers into a very simple trivia game. I have basically no experience with Arrays and thought it could be a good way to learn the ins and outs, but I’m stumped.

Currently the setup is just 4 buttons for the different answers and then a randomized question in a Label.

How could I make it so the answers would come in with the corresponding randomized questions? Can I attach some sort of identifier to the question and its answers to tie them together? And how would I go about assigning a different answer to each button automatically? Would separating the questions into categories/topics be difficult?

I can mentally picture how everything should fit together, but ADHD and chronic anxiety has been making it almost impossible lately to put some things into practice.

Well the way I approach things is, get something working first and then you can improve/refactor it.

I would create a class or file or even have it straight in the script managing the question and answers UI nodes with something like:

const QUESTIONS = {
	1: "How do I handle Questions and Answers for a simple Trivia Game?",
	2: "What is life?",
}

const POSSIBLE_ANSWERS = {
	1: ["Do this simple thing", "Do another thing", "I don't know", "Go to the shop"],
	2: ["Turtles are life", "Yellow?!", "Love", "EVERYTHING IS LIFE!"],
}

const ANSWERS = {
	1: "Do this simple thing",
	2: "Turtles are life",
}


class Response:
	var question: String
	var poss_answers: Array[String]
	var answer: String


	func _init(que: String, pos_answers: Array[String], answ: String):
		self.question = que
		self.poss_answers = pos_answers
		self.answer = answ


func get_q_and_a(question_num: int) -> Response:
	return Response.new(
		QUESTIONS.get(question_num),
		POSSIBLE_ANSWERS.get(question_num),
		ANSWERS.get(question_num),
	)

Then you can decide if this is how you want to structure your data or if you need it a different way.

Wonderful, thanks so much! That’s actually quite close to what I had, but my formatting was slightly off! That makes me feel a bit better.

This is just one way of doing it, as long as your approach worked then that is valid too.