lua-users home
lua-l archive

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


On 09/11/2014 12:39 AM, Flávio Alberto wrote:
I did this code ando for this lua code :

print(">> type(mastertable)="..type(mastertable))
print(">> type(mastertable.table1)="..type(mastertable.table1))
print(">> type(mastertable.table2)="..type(mastertable.table2))
print(">> type(mastertable.table3)="..type(mastertable.table1))

mastertable.table2.last = 2
mastertable.table1.a = 7

mastertable.table3.b = 8

mastertable.table3.name = "Test"

print(">> mastertable.table3.name="..mastertable.table3.name)
print(">> mastertable.table3.b="..mastertable.table3.b)
print(">> mastertable.table2.last="..mastertable.table2.last)
print(">> mastertable.table1.a="..mastertable.table1.a)

And get this :

type(mastertable)=table
type(mastertable.table1)=table
type(mastertable.table2)=table
type(mastertable.table3)=table
IN : l_newindex
mastertable.table3.name=Teste
mastertable.table3.b=8
mastertable.table2.last=2
IN : l_index
LuaCode.cpp : load : 825 - ERROR: /opt/trunk/nclua/script.lua:26:
attempt to concatenate field 'a' (a nil value)"


Apparently table1 was not created,
`mastertabletable1` is there, but `mastertable.table1.a` is not,
because you didn't do anything in the __newindex metamethod.
see the documentation for `lua_rawset`, `lua_rawget`

and to me appears be
counterintuitive (for a newbie point of view) this code

luaL_newmetatable(L, "luasystem.mastertable");
luaL_register(L, NULL, methods);

after two lua_newtable(L)'s,

for me appears that creating a metatable for mastertable.table1, but I
try put this code after the 1st lua_newtable to try associate this
metatable to main table but doesn't works...
this shoud work. but if you associate the metatable with `mastertable`,
the following code would not behave as you expected:
`lua_setfield` would trigger the `__newindex` metamethod, which did
nothing, thus resulting an empty `mastertable`.
when the Lua code access `mastertable.table1` (same for `table2` & `table3`) ,
the `__index` metamethod would be triggered, which did nothing either.
thus giving : ``>> type(mastertable1) = nil``.
also the following lines (firstly `mastertable.table2.last = 2`) throw errors.

Thanks


I felt like you were not very familiar to Lua's semantic.
I suggest reading the manual and PiL first.
also there are plenty of useful resouces in the lua-users wiki.

best regards.