Why Godot can't see an empty dictionary?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By WellStacked
var array = [null, "", {}, []]
print("{}? ", array.find({}))
print("[]? ", array.find([]))

{}? -1
? 3

:bust_in_silhouette: Reply From: exuin

It doesn’t matter that the dictionary is empty. If you put a key in there Godot wouldn’t be able to find it either. I think this is explained in the docs.

Note: Unlike Arrays, you can’t compare dictionaries directly:
You need to first calculate the dictionary’s hash with hash before you can compare them:

Thank you, that’s very helpful.

WellStacked | 2021-07-06 14:56

var array = [{}, [], {}, []]
var empty_item = hash(177591) # {}

var id = 0
for item in array:
	if item.hash() == empty_item:
		print("id: ", id)
	id += 1

WellStacked | 2021-07-06 15:00

Another thing of note about Dictionarys in Godot is they’re passed by reference so there’s only ever one copy of the Dictionary in memory unless you use the duplicate() function. That has bitten me more than once so I figured I’d share while you’re asking questions about Dictionarys!

timothybrentwood | 2021-07-06 22:46