|
|
|
 |
Reply From: |
godot_dev_ |
You could have something like this, where you store a list of buttons, and a list of booleans/flags to indicated whether the button is correct or not
var buttons = []
var answers = []
Now suppose you have 3 buttons, named "buttonA"
, "buttonB"
, and "buttonC"
in the scene tree. For sake of example, lets assume buttonB is the correct answer. Then in your _ready
function you could script as:
func _ready():
buttons.append(get_node("buttonA"))
buttons.append(get_node("buttonB"))
buttons.append(get_node("buttonC"))
answers.append(false)
answers.append(true)#button B correct answer
answers.append(false)
Then when a player presses the button, we want to make sure to connect to the signal to handle it (I believe the signal is “pressed”), as follows, still in your ready function
for btnIx in range(buttons.size()):
var button = buttons[btnIx]
button.connect("pressed",self,"_on_button_pressed",[btnIx])
Then we handle the button press using the _on_button_pressed
function
func _on_button_pressed(btnIx):
var button = buttons[btnIx]
var correctAnswer = answers[btnIx]
if(correctAnswer):
yourLabel.text = "correct"
else:
yourLabel.text = "incorrect"
, where yourLabel
is a Label
node from your scene.
You could also accomplish something similar by defining a script to attach to the button, then exporting a flag called correctAnswer
as follows
#button script
export (bool) var correctAnswer = false
and enabling the button with the correct answer in the editor. Then instead of maintaining a answers
array like I suggested above, the above_on_button_pressed
function could be simplified as
func _on_button_pressed(btnIx):
var button = buttons[btnIx]
if(button.correctAnswer):
yourLabel.text = "correct"
else:
yourLabel.text = "incorrect"
B) thanks!! what does [btnIx]
mean? is that short for index
pixeljp | 2023-06-09 15:45
btnIx
is the named of the variable that stores the button’s index/position in the buttons
array. It’s short for buttonIndex
godot_dev_ | 2023-06-09 18:25
is there a way for me to change the index of correctAnswer
each time it’s pressed? for example, the first question’s answer is buttonB
, and for the second question, it’s answer is buttonA
.
pixeljp | 2023-06-15 21:35
I haven’t test the below code, but the core ideas should be observable. The main thing is to keep a list of list of answers (a matrix of correct answer) flags tied to each button, where each row depends on the question’s index
#list of button nodes
var buttons = []
# a 2D array container the flags indicating
#which button is correct for each question
var answerLists = []
#names of the button nodes
var buttonNodeNames=[]
#the list you change to indicate what button is correct
var correctButtonNames =[]
var currentQuestionIx= 0 #the index of the current question
func _ready():
#for the first question, suppose button B is the answer
#next question, C is correct, next question B is correct
#next question, A is correct and so on
correctButtonNames.append("buttonB")
correctButtonNames.append("buttonC")
correctButtonNames.append("buttonB")
correctButtonNames.append("buttonA")
#only add to the above list called correctButtonNames
#when you want to add a question
#names of the buttons for later lookup
buttonNodeNames=["buttonA","buttonB","buttonC"]
#populate the button node list
for bName in buttonNodeNames:
buttons.append(get_node(bName))
#connect to the buttons' signals
for btnIx in range(buttonNodeNames.size()):
var bName=buttonNodeNames[btnIx]
var button =get_node(bName)
buttons.append(button)
button.connect("pressed",self,"_on_button_pressed",[btnIx])
#populate the answers list
#iterate over every question that will be asked
for correctButtonName in correctButtonNames:
var buttonCorrectnessFlags = []
#make sure the only the correct answer button's flag is set to true
for btnName in buttonNodeNames:
#correct button for this question?
if btnName==correctButtonName:
buttonCorrectnessFlags.append(true) #correct answer button
else:
buttonCorrectnessFlags.append(false) #incorrect answer button
answerLists.append(buttonCorrectnessFlags)
#an example of what answerLists would look like given
#the correct asnwers in this example (based on the content
#of correctButtonNames) would be
#[false,true,true]
#[false,false,true]
#[false,true,false]
#[true,false,false]
#where the row's index is specified by currentQuestionIx
func _on_button_pressed(btnIx):
#we get the list of flags that indicate
#the correct answers based on current question
var answers = answerLists[currentQuestionIx]
#we lookup whether the button pressed was correct
var correctAnswer = answers[btnIx]
if(correctAnswer):
yourLabel.text = "correct"
else:
yourLabel.text = "incorrect"
godot_dev_ | 2023-06-16 00:27