![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | WellStacked |
var array = [null, "", {}, []]
print("{}? ", array.find({}))
print("[]? ", array.find([]))
Output:
{}? -1
? 3
![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | WellStacked |
var array = [null, "", {}, []]
print("{}? ", array.find({}))
print("[]? ", array.find([]))
Output:
{}? -1
? 3
![]() |
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 Dictionary
s 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 Dictionary
s!
timothybrentwood | 2021-07-06 22:46