On 18.07.2025 14:58, 'Stefano' via lua-l wrote:
> So, the equality operator behaves as I expect, but I can't look up the
> table using that object. I was under the impression that table key
> access works by using the equality operator by default. Is there more
> to it?
Lua tables are hash maps. While your two different userdata objects are
equal under the equality operator, they are not the same object, thus
they have different identites. The same issue arises when using tables,
but not with any primitive values like strings etc.
You could create an identifying string though and store your data under
that key instead. i.e. add a method :identity(), which returns a unique
string for which the following condition holds:
a:identity() == b:identity() if and only if a == b.
Instead of a string you can return any primitive value like numbers,
light userdata etc. Composing strings is probably the easiest solution
though.
Another option is to return the same userdata if it was already created
once. Create a hash from your object and use that to store the object in
an hashmap (either in C or use Lua tables). Then whenever you create an
object twice, you can check if your hashmap already contains that object
and return the original.