How do I check/do something if 6 specific buttons (out of 12) in a grid container are toggled "pressed"?

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

I am creating a “game within a game” that gives you 12 choices (in GridContainer, w/child texture toggle buttons). You have to click/choose the 6 that are correct.

I have it as RR1ChallengeScene (Control Node root), the screenshot shows my tree.
Scene Tree
Goal: When user clicks any of the 12, they switch to the “highlighted” (pressed) texture and send toggled on signal (that’s done). User must choose only the ones with “hard G or C sound” (there are 6/12). Once user selects those, they click the Check button.
Here’s a screenshot showing what it looks like when the correct 6 are toggled on:
Preview Scene with Scene Tree on left

I cannot figure out how to a) have the “Submit” button check which are selected, and b) send a signal that "if these 6 are toggled on, and the other 6 are toggled off, you got it right (if you get it right, you see a pop up “Good Job!” and add 100 points to score–I got that part).

I got each button to “print” and send a “right” or “wrong” signal but I realized that if the user toggles one on/off/on (more than once) the # of signals won’t match the final number of toggled on/off. So I don’t think signals will work.

Below is the script I have but I did it for every button! I was hoping to somehow have the submit button check (or count) the correct on/off ones, returning “Try again” or “Great Job” depending. I am brand new at this and I am pretty sure there’s a “bulk process” I should be using.

I tried “Button Groups” but they only allow one button to be toggled on so that won’t work. I keep thinking “array” might somehow be the answer but the only tutorials on array involve text boxes and way more complicated stuff and I cannot figure out how it would apply here.

Basically: "if (on clicking Submit1) buttons 1, 2, 6, 9, 11, 12 are toggled on and 3, 4, 5, 6, 7, 10 are toggled off, show “Great Job” popup. And if else, show “Try Again” (and reset toggles).

Here is the code I have for a right/wrong button.
code for right/wrong buttons
I connected the ‘node signals’ for toggled (button pressed) on each button.

I swear I have looked for 3 days on how to manage this scene. I feel like it should be super simple but I’m too far gone to see the simple solution.

thanks to all in advance, and please be gentle with me, I have been using Godot for about 2 months (and yes I did all 3 “your first game tutorials” dodge the creeps/the platformer coin one, and a 3d one, and successfully built those which is how I already have a lot of other stuff in this game I’m working on! But I am truly lost here and in future parts of my game I’ll have even more complex scenes like this one so I want to do this right from the ground up.

I wanted to post how I tried to follow Noddy’s suggestions, and it works!!! I think I should have been able to do it all in array functions without so much stuff in each Toggle Button (like batch process all “right” buttons one way and “wrong” another way). BUT, this works so I am posting in case anyone wants to revise to make it cleaner and/or to help Noobs like me trying to decipher what Noddy wrote :slight_smile:
Side Note: Extra Stuff showing
I’ve left the signals in but we don’t need “right/wrong” ones so you can take those out.
Leaving in the rrchallenge_collected but you don’t need that either. It’s for the 100 points going into the main GUI control when they complete rrchallenge2 (ie so I’ll remember it needs to be in there but it won’t be in this challenge scene since they have to finish both to get points).

Top code:

extends Control

export var value = 100 # export is for scoring stuff I’ll use later so not important to this Q&A

var array1 = [1, 2, 3, 4, 5, 6]
var array2 = []
var length = array2.size() # I don't know if I actually need this part

signal rrchallenge_collected
signal rrchallenge_right
signal rrchallenge_wrong

func _ready(): #not sure I need these either?

Toggle Buttons Code
#When they toggle a correct button (I didn’t list all 12 buttons, just 1 right/1wrong)

func _on_cardGET_toggled(button_pressed):
	if button_pressed==true:
		array2.insert(0, "1") # insert lets me put the right value text "1" at right position [0]
		array2.erase("1") # untoggled so take out element 1...erase works, remove doesn't, long story why

Result: array2 now shows 1 :smiley:

#When they toggle a “wrong” button

func _on_cardAGE_toggled(button_pressed):
	if button_pressed==true:
		array2.append("A") #appending A adds it to end so it will make size wrong (we want this, otherwise it can replace a right answer in its index position)

Result: if they untoggle it removes A from end, or if they left it untoggled array2 stays as it was before

array2 now shows [1, 7] if cardGET and card AGE were both toggled on-- if they toggled cardAGE back off, it shows 1 again! This updates as such for each toggle added/removed.

NOTE: I did the other 5 correct and other 5 incorrect buttons like above, then, at the end under my “submit” button code

End: Submit/Check Code

func _on_Submit1_pressed(): #the button they press when they've toggled all their toggles on/off to signify they're done and ready to check answers
	if array2.size() > 5:
	if array1.size() == array2.size():
		$ #this is the label saying "woohoo you got it"
		$ #this is a new button that takes them to the next challenge
		$ #label saying "try again"
		$ #button to restart scene 

Note on restart scene for Try Again: I haven’t connected it and made that happen but hoping I can just mimic my “get_scene_tree” change scene thingie from start menu lol). I think just restarting the scene will be easier than resetting both arrays and hiding stuff and all that.

CCLaw | 2022-02-13 16:59

Welp, I spoke too soon. My “if” statements work if they have ANY 6 elements-not 1, 2, 3, 4, 5, 6 for example [A, 2, 3, 4, 5, 6] counts as correct too. I see I’ve basically asked “are there 6?” and then “do the sizes match?” and that’s basically asking the same thing. Now I am going to open a new question because I don’t see how to match the VALUES not just the sizes. Sigh. 3-4 days on one thing that I think should be easy :-/

CCLaw | 2022-02-13 20:18

:bust_in_silhouette: Reply From: Noddy

One idea would be to have an array of values that signify what combination you want. The array should just hold the values you want to compare. Then after 6 buttons are pressed have the keypad auto enter the result. Check the values in the array against the values in the buttons. If the button’s text is 1 and the desired value is 1 then that is true. To set this up you’ll want 2 variables, the array I mentioned before and an array that stores the values of the currently pressed buttons. The array is filled by you anyway you please(in this case it sounds like you can manually enter the values you want). We’ll call this array btn_value_arr in this hypothetical. The other variable, which will be an array, can be called curr_btn_pressed. Every time one of the buttons is pressed, it will append it’s text to the the curr_btn_pressed array. In the _physics_process() function or _process() function add a check that looks to see if curr_btn_pressed.size() is greater than 5(as to make sure you end up with 6 elements and not 7). If the check returns true compare the curr_btn_pressed array and the btn_value_arr against each other, if they are equal add your success code and if it fails add your failure code. Here is some documentation on Godot arrays.

As a bit of continuation, if you want players to be able to remove a value, then when the button is unpressed or in some way toggled off then have the function handling that signal look for the button text in the curr_btn_pressed and remove it. Or have a dedicated backspace button that allows a user to remove one element from the end of the array every time they press it.

Noddy | 2022-02-11 05:33

Okay this is SO helpful! Thank you SO much for your time to help. I am starting to see how arrays work (despite watching two 20 min youtube videos and reading all the documentation, I didn’t see how to apply them here).

So is this right–> Array 1: btn_value_arrEDIT: ended up calling this array1 I am assigning a number (0-5) called the “button text” (EDIT: nope, this was the index position not text) to each button. I am also assigning a value to each button (like in an excel formula where 1=true type thing??) and for each button text 0-5 is true/pressed they get 1 “point” (value) each and are added to the array when toggled “on”–ie when they are true? EDIT: technically each “correct” button had if/else so if toggled on was add it, else off was remove it I only add the 6 “correct” toggle buttons to the array but couldn’t the user check them all true and get it right in that case? (I clearly say “choose the 6 correct ones” but these are 9 yr olds lol EDIT: yes they could that’s why I had to make the “wrong” buttons append themselves instead of insert so they A) made the size wrong and B) didn’t overwrite any correct buttons gotten by index and string). So when the “submit” button is pressed, does it look for exactly 6 must be pressed(EDIT: it looks for the array2 size and if the values match the array1)? Is that the “size” you refer to? EDIT: It is the size, see my code in comment!

Array 2: curr_btn_pressed has appended (EDIT: I called it array2 and note code above append and insert do dif things so use insert for “correct” and append for wrong toggles) with each toggle on of the correct buttons we have called 0-5 EDIT: Didn’t “call them” 0-5, those are index positions, they are called 1-6 with quotes around them in positions 0, 1, 2, 3, 4, and 5 of the array… and wrong ones are A-F with quotes around them tacked onto end of array1…per Godot Array string vs. position documentation).

Then I use the “submit” button function to check curr_btn_pressed.size() >5? I check the “size” which tells me there are 6 things in my array (what keeps someone clicking all 12 buttons from being right? EDIT: I used append for all wrong buttons toggled which added to the array2, and so size does matter HA) and if that’s 5 (you said >5 but isn’t it =5 ? if not, why not? EDIT: it’s >5 because you need at least 6 things in there to be right–once you know that’s true…), I compare the values (that these 6 buttons were added and match Array 1) and that proves it’s the correct 6 buttons (success), else if (fail)?

When you say “have the keypad enter your result” is the keypad some kind of function or script I haven’t heard of? *I’ll look it up but I want to confirm it’s not the keypad on the user’s (or my) computer keyboard lol. EDIT: Never did figure this out --nothing googleable shows “keypad” with Godot other than your actual keyboard and my user will only use mouse to click buttons…I did find that some Python peeps call array items “keys” and you can actually use a “dictionary array” in Godot which will get “keys,” or array elements, like this. But dictionary arrays won’t compare to each other so I didn’t use them. They’d be good for shuffling stuff and “getting” stuff but not for comparing to see “if all elements are there & the same in both”. I didn’t use “compare array” or “match” with “x” because I got it to work without those (and when I tried them I couldn’t get it to work kept getting “method not called” errors and no one will just put example code of an actual sample thing that uses compare/match so it’s all out of context and I don’t get how each element of script goes in or where it does…plus why add more stuff to the top code if I don’t have to).

Side note: In your continuation it hit me that “text” or “entering text” “button text” doesn’t mean “words on the button” or a text box where user types something, it means the text inside the script for the button part…is that right? That was why I thought those tutorials i read didn’t apply bc mine aren’t “text buttons” they’re pictures lol. Wow. Feeling blond. Again, thank you so much for taking the time to give me that clear and thorough information!!! EDIT: those tutorials WERE about using arrays to change text in the game, after all… (like changing speech bubbles according to what has happened in game or what shows when certain things are clicked). Super handy but doesn’t relate at all to my toggle many at once to be "correct " issue.

CCLaw | 2022-02-11 14:11