0 votes

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?!

in Engine by (151 points)

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

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

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

1 Answer

0 votes

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!!

by (151 points)
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 webmaster@godotengine.org with your username.