lua-users home
lua-l archive

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



> Can you use userdata as a table key?

yes, the Wiki Tables Tutorial is absolutely correct.

> When I do this, and then say
>         print(string.format("Size of CtrlTable = %d",
> table.getn(gCtrlTable )))

> The result is 0.


table.getn() does not return the size of the table; it
only concerns itself with entries whose keys are positive
integers. (In fact, it is simply, in effect, an attribute
of the table, and can be set to an arbitrary value with
table.setn(), but if that hasn't been done table.getn()
searches for the largest integer key.)
 
>  Nor can I index the table using the newId key.  Where has
> the data gone?

Perhaps newId no longer has the same value as it had before.

For example, it might be a different userdata with the same
contents; that is a different object as far as tables are
concerned. (This issue is common with bindings that used boxed
pointers to make userdatas out of internal objects.)