Attention | Topic was automatically imported from the old Question2Answer platform. | |
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.
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:
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.
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
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?
array1.size()
array2.size()
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]
print(array2)
emit_signal("rrchallenge_right")
else:
array2.erase("1") # untoggled so take out element 1...erase works, remove doesn't, long story why
print(array2)
Result: array2 now shows 1
#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)
print(array2)
emit_signal("rrchallenge_wrong")
else:
array2.erase("A")
print(array2)
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:
print(true)
if array1.size() == array2.size():
$Success.show() #this is the label saying "woohoo you got it"
$GotoChal2.show() #this is a new button that takes them to the next challenge
else:
$Fail.show() #label saying "try again"
$TryAgainChal1.show() #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