+1 vote

I was wondering if there is a specific function that checks if an array contains all elements of another array in GDScript. Is there any function like that in GDScript? Or do I code a function for it? Can anyone show me how to do so.

Godot version 3.5.1
in Engine by (27 points)
edited by

1 Answer

0 votes
Best answer

So would you be interested in an array containing the other like a superset? Or if 2 arrays are exactly the same?

Not sure if Godot has any specific function for it but the code is quite simple

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

This function checks if the 2 arrays are exactly the same. In case you need to only check if array1 contains all the elements of array2 you could reaarange the code like this

func arrays_contains_array(array1, array2):

for item in array2:
    if !array1.has(item):
         return false
    if array2.count(item) != array1.count(item):
         return false
return true
by (46 points)
selected by

The second function was exactly what I was trying to figure out. Thank you!

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.