lua-users home
lua-l archive

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


Hi Rodrigo,

this should work (Pseudocode):


lua_newtable (L); -- push a new table

lua_pushstring (L, "User");
lua_newtable (L);
lua_pushstring (L, "Name");
lua_pushstring (L, "Rodrigo");
lua_settable (L, -3); -- sets Name = Rodrigo into table User
		      -- and pops both the key and the value from the stack

lua_pushstring (L, "Age");
lua_pushnumber (L, "99");
lua_settable (L, -3); -- sets Age = 99 into table User
		      -- and pops both the key and the value from the stack

lua_pushstring (L, "Status");
lua_pushnumber (L, "Management");
lua_settable (L, -3);

lua_settable (L, -3); -- put table User into new table

.... do the same for table Var, Position and Bin


-- register the newtable containing the subtables to the global environment and -- name it "Settings"
lua_setglobal (L, "Settings"); -

As you can see: It's always the same mechanism, so using Macros can shorten your Code enormously.

You can push this as your own library into lua with using luaL_register. Look into the Lua code to find out more ...

Regards,
Eva


Rodrigo Araújo wrote:
Hi there,

A lua user will be able to access a structure like Settings.User.Name <http://Settings.User.Name> inside a Lua file at any moment. This said, these information are only available at C++ files, so I have 2 problems:

1. I need to define in C++ a table (Settings) that has four other tables inside (User, Var, Position and Bin) and these have the attributes (User: name, age, status, Var: int1, int2, int3, Position: int4, int5, int6, Bin: char1, char2, char3) 2. I need to make these tables available for Lua users at any moment of the code through their names, like Settings.User.name <http://Settings.User.name> or Settings.Var.int1.

I know that lua_newtable(L) creates a new table on top of the stack, but that doesn't make it available through her own name (like Settings), the user would must have to receive this table as a return statement from some kind of function... Is there anyway to do it?

Thanks in advance,
Rodrigo Araujo