how do i script this if-statement?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By pixeljp

Im working on a quiz game and I’m trying to make an if statement that goes like this: if a button is pressed and it’s not the right answer to the question, display “wrong answer” (label)… but im not sure how to do that. would i use if Button.pressed() != get_node("correct answer button") or something similar?

:bust_in_silhouette: 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():

    answers.append(true)#button B correct answer

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]

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]
        yourLabel.text = "correct"
        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]
        yourLabel.text = "correct"
        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
    #only add to the above list called correctButtonNames
    #when you want to add a question
    #names of the buttons for later lookup
    #populate the button node list
    for bName in buttonNodeNames:
    #connect to the buttons' signals
    for btnIx in range(buttonNodeNames.size()):
        var bName=buttonNodeNames[btnIx]
        var button  =get_node(bName)

    #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
                buttonCorrectnessFlags.append(false) #incorrect answer button
    #an example of what answerLists would look like given
    #the correct asnwers in this example (based on the content
    #of correctButtonNames) would be
    #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]
        yourLabel.text = "correct"
        yourLabel.text = "incorrect" 

godot_dev_ | 2023-06-16 00:27