lua-users home
lua-l archive

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


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.