lua-users home
lua-l archive

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


At 12:36 13/09/2000 -0400, you wrote:
Is there any way to set the value of a global variable from C by using its lua_Object instead of its name? To make a long story short, I have a class that contains a lua_Object (a table), and methods for reading and writing data in that table. When the class is created, it loads a file that contains the definition of one table, and the filename matches the name of the table inside. Then it gets the lua_Object by passing the filename string to lua_getglobal(). Before, I had the class set up so that it stored the name of the global, but that was a horrible waste, because the object would then be cloned, and the name string would not be needed. Anyway, I need to somehow set the value of the global variable with the lua_Object, so I can have it garbage-collected when the class is deleted. Any way to do this? If not, how can I write a function to do this?

=^_^=
----------------------
Do you do Linux? :)
Get your FREE @linuxstart.com email address at: http://www.linuxstart.com

Hi Jason,

You can use something like that:

------------------------------------------------------------------------------------------------------
-- In your Lua file

return {field1 = "some value", field2 = "other value"}

-------------------------------------------------------------------------------------------------------
// In your C++ code

X::X(char* file_name)
{
   if (lua_dofile(file_name) ==0)
   {
       lua_Object l_obj = lua_lua2C(1);
       if (lua_istable(l_obj))
       {
           // do what you want
          ...
       }
       else
       {
          // ERROR
         ...
       }
   }
   else
   {
       // ERROR
       ....
   }
}

Regards,
Renato

-----------------------------------------------
Renato Cerqueira, Ph.D.
Research Staff Member, Tecgraf/PUC-Rio
email: rcerq@tecgraf.puc-rio.br
http://www.tecgraf.puc-rio.br/~rcerq