Topic was automatically imported from the old Question2Answer platform.
Asked By
jiwooyun_
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.
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
The second function was exactly what I was trying to figure out. Thank you!