Need help figuring out how to use custom resources to make a quiz game

Godot Version

4.5.1

Question

Hello! I’m rather new to Godot and programming in general, recently a professor showed me how to make custom resources to help me make a quiz game, despite his efforts, I still don’t quite get it and I need some extra help.

So far, I’ve managed to get the question and answers to show up, but I have no idea how to make each possible answer react depending if it’s correct or not, either have I figured out how to pass to the next question if the answer is correct. My professor wants me to figure it out but I’ve looking for a way to do these two things for about 2 hours and still can’t figure it out.

This is what I got so far:

extends Node2D

@onready var DisplayText = $computer/VBoxContainer/question/Label
@onready var ListItem = $computer/VBoxContainer/answers/VBoxContainer
@export var items :Array[questionressource]
var item: Dictionary
var index_item : int = 0

var correct : float = 0

func _ready():
$computer/VBoxContainer/question/Label.text=items[1].question
var nombre=0
for i in $computer/VBoxContainer/answers/VBoxContainer.get_children():
i.text=items[1].reponse[nombre]
nombre+=1

func _on_question_1_button_down() → void:

func _on_question_2_button_down() → void:

func _on_question_3_button_down() → void:

Thank you in advance for any advice given :folded_hands:

You need to create a custom resource for the answers and another custom resource for the questions.

Each question should have an export var called correctAnswer, which should be of type Answer.

You can then have a master array in an autoload that holds all the answers.

Your custom Answer resource should include the following variables:

answer: String

uniqueID: int

Make sure no two answers share the same ID.

When the player selects an answer, check if that answer matches the question’s correctAnswer resource by comparing either the string or the ID values.

You can also randomize which answers appear by pulling them randomly and re-rolling if the ID matches another.

Hope this helps

1 Like

I don’t think two resources are necessary. One is enough, but it does needs to indicate which of the answers is correct for the question it contains. It could be an @export correct_answer: int and you set it to the number of the correct answer.

Imagine your code is a person, and your question resource is a card. The person pulls one card, reads you the question and possible answers off of it. When you pick an answer the person checks if it is the one the card lists as correct. The person then adds you a point if you picked correctly, or no points if you picked wrong. That is what your script should do.

1 Like

just make [0] always correct and mix what order they are applied to be shown if you just want to use an answer array for each question

1 Like

Alright, thank you! I’ll try this out!

1 Like

I see! Now I understand a bit better, thank you!

1 Like