0 votes

Is there a way to match an array, even if it's not in a specific order?
Just as long as the variables are in there? Or is there a better method to do that?

For example:

func _my_func():
     var potential_array: []
     # statements that add to 'potential_array' based off of circumstances.
     _matching(potential_array)

func _matching(new_array):
     match new_array: #hypothetically its ["test1","test4","test3","test5"]
          ["test1","test3","test4,"test5"]:
               #Not in the same order, but I'd like for it to simply check all the variables and match anyways.

ANY help would be appreciated, to keep me from writing every single possible outcome order.
I'm still new to this.

in Engine by (293 points)

1 Answer

+1 vote
Best answer

Something like this should work:

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

Pass in 2 arrays, and get back either True if the arrays have the same items or False if they do not...

EDITED to handle differing numbers of duplicated elements (mentioned below)

by (10,856 points)
selected by

As an explanation, it basically does this:

  • Verify that both arrays have the same number of elements. If they do not, they can't have the same elements, so return false
  • Otherwise, iterate through every element in array1. If array2 does not have that element, return false.
  • Next, verify that both arrays contain the same number of the current element (in case the arrays contain duplicate elements). If they don't have the same number, return false.
  • Finally, if we made it through all the elements without having returned a false along the way, both arrays have the same elements, so return true

Note, I've modified the posted script so the below "issue" is properly handled, So, the below comments are no longer accurate...

Also, note this is only 100% accurate if each array contains unique items. If you pass in 2 arrays that contain the same number of elements, but different quantities of common elements, it'll get fooled.

For instance, it'll return true on these 2 arrays...

var a1 = [1, 1, 1, 2]
var a2 = [2, 2, 2, 1]

Because they both have 4 elements and the elements consist of only the integer values 1 and 2. It will not detect the differing quantities of duplicated elements.

But, if you don't have duplicate items in your arrays, you should be fine.

If you do, the algorithm would need to be improved to account for that.

Note, I just modified the originally posted function to handle the "differing number of duplicate elements" issue I mentioned in my previous comment. So, it should work for that case now too...

Jgodfrey, thank you for time, effort and consideration put into answering my question. You opening my mind to the idea of using the Array.has(variant) and comparing the array.size, is exactly what I needed to work around having to match all the variables in order.

Thanks again!

One follow up question: Is there a way to extract original text from a string?
I'd like to pass a string to another script, but for that script to take the name in the string as if it were one of it's global variables.

Example;

Script 1:

func set_parameters():
     var outcome
     if test = true:
            outcome = "alive"
     else:
          outcome = "dead"
     $other_script._testing_for(outcome)

Script 2:

var alive = "I live!"
var dead = "I'm not alive!"

func _testing_for(incoming):
     print(incoming)

This way I don't have to assign each possible string to a variable.

I'm not sure how this relates to the original question. Assuming it doesn't, you're probably better off asking a completely new question (with an appropriate title, ...). That way, someone with the same question in the future will be able to find an answer.

If it does relate, I'm just missing it. Feel free to add another comment...

Gotcha, better for the community, good looking out.

Posted new question here.

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 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 webmaster@godotengine.org with your username.