The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

0 votes

What I'm Doing: I am using 2 arrays to show if the correct buttons (6/12) are toggled on... (this was my first question where Noddy helped me choose to use arrays original question)
array1 is the base array of [1, 2, 3, 4, 5, 6] and array2 starts as [] and grows/shrinks as user toggles switches on/off.
Why: user needs to choose the 6 correct words out of the 12 listed

So far, each button correctly adds to array2, but when user clicks "submit" after choosing, I am only getting a size match, not a content match.
So, I want to know how to also check if my array2 content matches array1 exactly [1, 2, 3, 4, 5, 6].
If size and content match, "success" or if not, "try again"

What I've Tried: I read this answer but I don't see where to put that func arrays_same_content in my code--or how to say "if all those return true, then do the success stuff under submit button, if false do the other stuff". i tried adding it instead of my current "size" stuff (below submit button pressed) but it didn't work ("method not called" error) and I tried to make a method/function for it and failed in every way. I only want it to check those on submit_pressed and I can't figure out how to do that--or how the "return true" causes the other stuff to happen (and the else/false stuff to happen if it doesn't return true).

Note: rn my toggle button code is a messy workaround since I can only seem to compare size not content... I just appended two things when a wrong one is pressed (instead of one) so it makes the array too big when any of the 6 pressed are not "correct." It's still not foolproof bc you can select 4 and end up with an array of 6 things.

Here's my current code:

extends Control
export var value = 100
var array1 = [1, 2, 3, 4, 5, 6]
var array2 = []
var length = array2.size() #I don't even remember where I got this--needed??

func _ready():
    array1.size() #do I even need these? Are they feeding Submit> if #2 ?

Button Codes (Correct button then Incorrect button)

func _on_cardGIVE_toggled(button_pressed):
    if button_pressed==true:
        array2.insert(1, "2")

func _on_cardAGE_toggled(button_pressed):
    if button_pressed==true:

Then once user is done toggling/choosing, they click CHECK (Submit button) and...

func _on_Submit1_pressed():
    if array2.size() > 5: #Is this one better or is the == one right?
        print(true) #print should be return? print not feeding info I know, what good is returning true here if idk how to say "if true, do this"?
    if array1.size() == array2.size():
        $ #label "good job!"
        $ #button loads to next challenge scene
        $ #label saying try again
        $ #narration audio
        $ #button that reloads scene

I am totally new to coding and have done the "first 2d" and 3d game tutorials all the way through (successfully made those and dodged creeps etc). I watched all this guy's stuff on arrays: and went through everything I can find. I know my "size" is basically checked 2 ways lol, I don't know what I can take out on that (the function ready stuff? Line 1 or 2 check under if on Submit button??) i'm just lost on how to also check/return true for matching content. I didn't do insert on wrong buttons because I didn't want to overwrite the correct answer index positions.

I'm so grateful for any help anyone might have. Thanks so much (and please be gentle with me-tryin to learn here)

Godot version 3.3.2
in Engine by (20 points)

1 Answer

+1 vote
Best answer

Just noticed the post and code you mention is mine, so I'll offer some input...

First, I think the arrays_have_same_content() method I posted in the linked message is useful here. Really, it should be about as simple as:

  1. Add the arrays_have_same_content() method to the above script
  2. Use it in your on_Submit1_pressed() method, something like:


if arrays_have_same_content(array1, array2):
    # do success stuff
    # do failure stuff
by (22,674 points)
selected by

Can you make the entire project available for examination? It's not possible to run your scene based on the content you posted above. For example, there's not project.godot file in posted folder...

Related to this:

Now, I'm getting SHADOWEDVARIABLE errors...

First, that's a WARNING, not an ERROR. It's telling you that you're doing something that possibly confusing / dangerous, but it won't cause any actual issues. That said, you should probably fix it for clarity.

The problem is that you have defined global copies of array1 and array2 (at the top of your script and you have a local copy of the same variable names defined in the arrays_have_same_content() function.

The easy fix is to rename the variables in the function to something different. For example, this would fix the warning:

func arrays_have_same_content(a1, a2):
    if a1.size() != a2.size(): return false
    for item in a1:
        if !a2.has(item): return false
        if a1.count(item) != a2.count(item): return false
    return true

Note, array1 and array2 have been renamed to a1 and a2 in the function.

You are too kind have gone WAY above and beyond on helping me and I know I will get it all figured out if I go back to the basics on each thing I want to add. I really think you have done more than anyone ever should! Can I donate to something or give back in some way?

Entire Godot Game "Owen"

It is still uploading everything as I post it but should be done in 5 minutes :)
(I had a couple unused/try out things in there so ignore anything about "cards" and scenechanger and main--MainScene is the main scene lol)

I will adjust the function to use a1/a2! Thank you for explaining what was causing that!

Just took a look at your project. The main problem is that your initializing your "answer" array with int values (1, 2, 3, 4, 5, 6), but you're appending string values to the array that you're comparing ("1", "2", "3", "4", "5", "6"). When you eventually call arrays_have_same_content, that method is returning false because the int values in the answer array don't match the string values in the match array.

To fix that, you need to use the same data type in both arrays. Either strings or ints will work, but they should be the same.

Since you're using "letters" for incorrect choices (which can't be an int), I'd recommend that you just use strings everywhere.

So, at the top, initialize your arrays like this:

var array1 = ["1", "2", "3", "4", "5", "6"]
var array2 = []

(notice, the 6 values added to array1 are string, not int)

And, notice that array2 is simply empty.

Then, you can code each *_toggled function like this, although obviously, append and erase the correct character in each function.

func _on_cardAGE_toggled(button_pressed):
    if button_pressed==true:

Above, the given character is added to the array when the button is pressed and erased when the button is unpressed. That ensures that the array will always have only 0 or 1 copy of the character no matter how many times the button is pressed - which is important when you finally compare it to the answer array.

If you code all of the button handling functions like the above, your scene should work as you expect.

I would also say that, while what you have here will work, it's laborious to create, error-prone to code, and just requires a lot of work to manage a single "quiz". There are certainly better ways to do this kind of thing, but the above should get you working.

Let us know how you get along.

You are the best. Thank you so much. I did it!! It works perfectly! The 0/1 copy was a stroke of genius, and of course works with strings in both arrays... exactly what I wanted to do in the first place. THANK you!!

The int/string thing makes so much sense now. I noticed it when I tried append vs. insert (how you need the index and the string), but kept focusing on array2 strings and completely spaced that there's no reason to have index positions in array 1 instead of just strings!! Thank you so so much for everything you've done.

I agree wholly this is way too complex doing every button individually. I know there must be a much more cohesive way (like some sort of batch process for a quiz) and I will find it! At least it works for now!! My next "challenge" will be more complicated (JSON import for arrays), so I will find a better "base" for a quiz type thing. In my forum/video/tutorial googling, I found multiple choice quizzes (one answer is correct) and memory card games (flip/match one pair of cards) and couldn't figure out how to adapt them to multiple "correct answers." But that's okay, because I am learning how to conceptualize my "learning checks" to fit better code practices! I am so happy because I can tell I am growing and learning by how much I go back and fix a parent scene (I've streamlined like 5 things in MainScene since I uploaded that project for you yesterday!).

Your willingness to help me understand has given me the determination that I will get there! Thank you again, very much. All the best to you and yours.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.