Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't compare arrays by reference #33627

Closed
rokasv opened this issue Nov 14, 2019 · 5 comments
Closed

Can't compare arrays by reference #33627

rokasv opened this issue Nov 14, 2019 · 5 comments

Comments

@rokasv
Copy link

rokasv commented Nov 14, 2019

Godot 3.1.1

I want to compare the arrays for uniqueness. I can't do it using array comparison.

var test = [1, 2]
var test2 = test
var test3 = [1, 2]
print(test == test)
print(test == test2)
print(test == test3)

The output is:

True 
True 
True

I expect:

True 
True
False

I want to be able to tell the difference between test2 and test3 as appending new values to test2 will not have them appended to test3.

i.e.

var test = [1, 2]
var test3 = [1, 2]
print(test == test3)
test3.append(4)
print(test == test3)

The output is:

True 
False

I need a way to compare the arrays by reference. There is no way to do it (?).
In other words, how do I get the pre-"fix" #1485 results?

@charliewhitfield
Copy link

I would like a solution to this problem too. Note that dictionaries act "correctly" (imho):

print({a=1} == {a=1})
print([1] == [1])

prints...

False
True

I'd much prefer to see arrays work as dictionaries. Otherwise, we need some way to test whether two arrays are different arrays, as stated by OP.

@tygree
Copy link
Contributor

tygree commented Nov 20, 2019

I'll look into this. Would you like this to be a change to the "==" operator or should an additional function be added to compare two arrays by reference?

@charliewhitfield
Copy link

charliewhitfield commented Nov 20, 2019

I think arrays should work like dictionaries and objects, where "==" tests identity not content. I believe we are currently lacking any way to do an identity test for arrays.

I can certainly see the convenience value of one or more array (& dictionary) functions that compare content. But these should not be "==".

Edit: My need for a way to test array identity is in this procedural save/load system (submitted to Godot Asset Library a few days ago). Presently, we have to work with the warning: "A single array persisted in two places will become two on load". If I could test array identity, then I could remove that warning.

@charliewhitfield
Copy link

charliewhitfield commented May 23, 2020

I opened a feature request to ask for ability to test array identity, whether it be == or some other way. If you need this functionality, or have an opinion on implementation, please go over there to comment.

@akien-mga
Copy link
Member

Superseded by godotengine/godot-proposals#874.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants