I’m trying to make a puzzle game where the player has to pick the correct answers on multiple drop down menus and then clicks a “check” button to see if they got it right.
I am able to get it to read one drop down by having an “answer” variable that is changed by the ID of the correct option. But I don’t know how to get the check to read multiple dropdowns simultaneously. I’m hoping to be able to show different messages for how many are incorrect (ie. 4/5 tells them they’re close, 1/5 will tell them they’re way off from the right solution.)
I’m still fairly new and have been experimenting with groups and signals, but unfortunately most guides only cover standard buttons and not dropdowns.
See the below for the current code I’m using for it to read one Option Button:
First of all the operator is wrong.
The operator should be +=. When you write x =+ 1 you are actually writing x = +1
ie x = positive 1.
It is not necessary to use the item_selected signal since you only want to know what is selected after the separate check button is selected.
Instead, poll the option button values in the _on_check_pressed() function.
(If you have a large amount of option buttons then look into using a node group however I present another way that is suitable for small amount of option buttons)
Drag each of the option buttons into your code window and press the ctrl key and drop them. That will give you a variable definition for each of them like this: @onready var option_button1: OptionButton = $OptionButton1
Take all of these option buttons and put them in an array: @onready var option_buttons:Array[OptionButton] = [ option_button1, option_button2, etc]
You will need a way to store the correct answer. You can either make another array or dictionary or just use the item id.
When you add items to the option button you will notice that each item has properties and one of them is an ID. In this case I use the id 99 to signal the correct id. Change the id for each of the correct answers to 99.
In the _on_check_pressed() function loop through the array and get your results:
func _on_check_pressed() -> void
var answers:int = 0
for ob:OptionButton in option_buttons:
if ob.get_item_id(ob.selected) == 99:
answer += 1
print(str(answer) + " correct")
Thank you so much for your detailed explanation- I’d toyed with arrays before but struggled with the logic of them until now!
That has helped some of my issue, but I’m running into the issue that the result doesn’t update accurately- if the player closes out of the popup telling them how many are correct and immediately puts in new answers, it tells them the first outcome again.
How would you go about “resetting” the count after closing out the popups?
EDIT- It’s because it only seems to count things when the dropdowns are empty to start with and items are entered. Is there a way to reset dropdowns to their default? Or preferably, a way to get it to count items that aren’t changed between pressing the check button?