How to compare an array?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ismaelgame7
:warning: Old Version Published before Godot 3 was released.

Hello people,

How do I compare one array with another?
Example:

var array1 = [0,1,0]
var array2 = [0,1,0]

if (array1 == array2):

Do some

Can someone help me please?!

your example should work, do you have any issues with that?

eons | 2017-05-17 17:51

var a1 = [0,1,0]
var a2 = [0,1,0]
print(a1 == a2)  # prints True

volzhs | 2017-05-17 19:05

I’ve just stumbled with this ‘equality by value’ behavior.

Even though for most cases this is just what is needed, there are some scenarios where one would like to actually compare arrays by reference instead. I haven’t yet found a builtint way to do this, so I resorted to create a Wrapper script in order to wrap values that ‘compare by value’, so that you can compare (the wrapper) by reference instead. Here it is in case anyone needs it

extends Reference
class_name Wrapper
var value setget set_value, get_value
func set_value(v):
	value=v
func get_value():
	return value
func _init(v=null):
	value = v

gnumaru | 2021-12-21 15:19

:bust_in_silhouette: Reply From: ismaelgame7

I thought I needed to use a “for” to loop through an array. kkkkk
I did not know that my example was right.

Thank you so much!!