lua-users home
lua-l archive

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

Thanks for the reply. I knew it probably is something like this and thanks for confirming me with that. But it seems a little bit hard, given many of this kind of tables need to be imported to C. I like tolua, which exposes C structures automaticlly to Lua. Is there a equivalent to do the reverse? Or everyone use this direct importing function?

Luiz Henrique de Figueiredo <> wrote:
> 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);
lua_pushliteral(L,"width"); lua_gettable(L,1);
lua_pushliteral(L,"red"); lua_gettable(L,1);,-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.

Do You Yahoo!?