lua-users home
lua-l archive

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


> What's the best way to fill a userdata with a table?

You can set metamethods for this. But if you're using "config{...}" in Lua,
then it is easier to write a C function exported as "config" that does the job.

It would be something like this (untested):

 static CConfig myconfig;

 static int do_config(lua_State *L)
 {
  lua_pushliteral(L,"length");	lua_gettable(L,1);
  myconfig.level=lua_tonumber(L,-1);
  lua_pushliteral(L,"width");	lua_gettable(L,1);
  myconfig.width=lua_tonumber(L,-1);
  lua_pushliteral(L,"red");	lua_gettable(L,1);
  myconfig.red=strdup(lua_tonumber(L,-1));
  return 0;
 }

I'm not sure what you need to do to copy the value of "red"; I've just used
strdup, which I'm not sure works for C++ strings.

Also, there is no error handling, but I hope you get the idea.
--lhf