lua-users home
lua-l archive

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


On Fri, Jul 10, 2015 at 11:57 AM, Brigham Toskin <brighamtoskin@gmail.com> wrote:
On Fri, Jul 10, 2015 at 10:24 AM, Rena <hyperhacker@gmail.com> wrote:

On Jul 10, 2015 12:19 PM, "Борис Александров" <boriscool007@gmail.com> wrote:
>
> why addresses of two userdata's should be equal? they are dynamically allocated using ( lua_newuserdata ).
> Also rawequal works until i'm commented __tostring and __equal metamethods
>
> 2015-07-11 2:10 GMT+10:00 Choonster TheMage <choonster.2010@gmail.com>:
>>
>> On 11 July 2015 at 02:08, Choonster TheMage <choonster.2010@gmail.com> wrote:
>> > On 11 July 2015 at 01:59, Борис Александров <boriscool007@gmail.com> wrote:
>> >> Player(0)Player(0)
>> >>
>> >> 2015-07-11 1:44 GMT+10:00 Rob Kendrick <rjek@rjek.com>:
>> >>>
>> >>> On Sat, Jul 11, 2015 at 01:41:44AM +1000, Борис Александров wrote:
>> >>> > Example code:
>> >>> >
>> >>> > " temp = {}
>> >>> >   temp['string'] = 'foobar'
>> >>> >   temp[GetPlayer(0)] = 'player'
>> >>> >   temp[GetEntity(0)] = 'entity'
>> >>> >
>> >>> >   print(temp['string']) -- foobar
>> >>> >   print(temp[GetPlayer(0)]) -- nil
>> >>> >   print(temp[GetEntity(0)])  -- nil
>> >>>
>> >>>
>> >>> What does the following display?
>> >>>         print(GetPlayer(0), GetPlayer(0))
>> >>>
>> >>> B.
>> >>>
>> >>
>> >
>> > Can you remove the __eq and __tostring metamethods from the userdata
>> > and run this code?
>> >
>> > print("Equal?", GetPlayer(0) == GetPlayer(0))
>> > print("Addresses", GetPlayer(0), GetPlayer(0))
>> >
>> > If GetPlayer always returns the same userdata for a given player ID,
>> > the first line should be "Equal? true" and the second should have the
>> > same hexadecimal digit twice.
>> >
>> > If GetPlayer returns a different userdata each time, you won't be able
>> > to use it as a table key reliably (since the userdata objects won't be
>> > raw equal to each other).
>> >
>> > Regards,
>> > Choonster
>>
>> You could also just use rawequal to check if they're equal (I forgot
>> about it before):
>>
>> print("Equal?", rawequal(GetPlayer(0), GetPlayer(0)))
>>
>

We don't know the implementation of GetPlayer(). Guessing from its name I would expect it to return the same object every time given the same input. If that's not the case then no, you won't be able to use it as a table key, since you get back a different key each time.

--
Sent from my Game Boy.


A reasonable alternative might be to use a string like "player0" as the key, if it's not too expensive to generate such a string.

--
Brigham Toskin

Quick addendum to my last message:

If you know you want player 0 specifically, you can just use the literal string "player0", which is cheap in Lua.

If you need to choose the player by ID parametrically, you could do something like temp[players[id]], where players is something like:

    players = { [0] = "player0", "player1", "player2", ... }

It's a double table lookup, which adds some overhead, but one of them is probably an array lookup (except for 0), and I'd imagine it's still faster than trying to use...

    "player" + id

...all over the place.

--
Brigham Toskin