Topic was automatically imported from the old Question2Answer platform.
Asked By
nwgdusr999
Say I have an object with 3 integers which I’d like to use as a Dict key. Is there a ‘custom’ way of comparing these objects? Like overriding an equals method in Java or defining a custom comparator which would for example check 2 out of 3 int for equality?
Actually, I’m not even sure how does Godot even deal with object equality… Is this documented somewhere? Could not find it if it is…
By default objects and dictionaries are compared by reference, not by what they contains (which can be quite a heavy operation). However, the Array type exceptionally is compared by contents. So you might be able to use an array as key. However if it contains objects they will in turn be compared by reference.
I don’t see any mention of equals in the doc, so you probably can’t override that behavior, unless you call it yourself explicitely. There is a hash function @GDScript — Godot Engine (3.1) documentation in English but I think it won’t take object contents into account.
Note, using complex objects as dictionary key may be slow depending on what you are doing (I havent seen that in ages in games even in other languages so maybe there is another way?). Maybe you could use some sort of ID or name as a workaround?