lua-users home
lua-l archive

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


Based on Peter Shooks example, I am creating a library to register my C++ classes.
Functions work, but I am not able to find a way to access variable members
of the LUA - class, like "clss.x = 10" in the example below.
I also tried to create a index - event like the "gettable"-code below and pushing x as a number. This works. But there is no trigger when x is changed or read then.
Can anyone help me with what code I could add to interact with x ?
Many thanks,

               Richard van Zon.

source.c ------------------------------------------------ snippet --------

lua_newtable (LUA);
lua_pushstring (LUA, "gc");             // the destructor, gc is a function
lua_pushcfunction (LUA, gc);
lua_settable (LUA, -3);

lua_newtable (LUA);
lua_pushstring (LUA, "CreateClass");
lua_pushvalue (LUA, -2);
lua_pushcclosure (LUA, creator, 1); // the constructor, creator is a function
lua_settable (LUA, LUA_GLOBALSINDEX);

lua_newtable (LUA);
lua_pushstring (LUA, "gettable");
lua_pushvalue (LUA, -2); lua_settable (LUA, -4);
lua_pushvalue (LUA, -2);
lua_pushcclosure (LUA, setX, 1);     // setX is a function
lua_pushstring (LUA, "setX");
lua_insert (LUA, -2);
lua_settable (LUA, -3);

lua_pop (LUA, 2);

test.lua --------------------------------------------------------------------

clss = CreateClass();
clss:setX (50);
cls.x = 10; -- gives error : attempt to index global `clss' (a userdata value)