lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



The only way I know for this, is making sure you only create _one_ userdata for each value combination (you should use a lookup in the creation function for that). Otherwise, if you end up having multiple userdata they aren't usable as table keys. Period. No way I know for changing that (think strings, and the way they are immutable; these are very profound Lua assumptions that in the end boil down to runtime efficiency).

One thing you could do, since coordinates are numbers, is tbl[1000*x +y] ? Maybe?

-asko


Niklas Frykholm kirjoitti 16.10.2006 kello 19.27:

In my Lua application I have userdata objects with value semantics (position coordinates) that I sometimes want to use as table keys. I.e., I want table lookup based on equality rather than identity.

Currently, my best idea for doing this is to have a function key() in the userdata that returns a string representation of the data and use that string as the table key. This has the disadvantage of 1) not being very elegant and 2) creating a lot of unnecessary strings.

Is there a better way of doing this?

// Niklas