1

Let's say I want to set a value (e.g. a function) inside a nested table from the Lua C API.

-- lua.lua
glob = { 
  nest = { 
    -- set value in here
  }
}

How would I have to set the stack to access the inner table?

Is it just calling gettable multiple times and then a settop, like in the following code?

lua_pushstring(state, "glob");
lua_gettable(state, -1);
lua_pushstring(state, "nest");
lua_gettable(state, -1);

lua_pushcclosure(state, &func, 0);
lua_settop(state, "funkyfunc");

lua_pop(state, 2);
2
  • 2
    You need lua_getglobal and then lua_gettable Commented Apr 28, 2014 at 21:16
  • What do you intend to do with lua_settop(state, "funkyfunc") ? Commented Apr 28, 2014 at 23:57

1 Answer 1

1

This code sets glob.nest.name to a C function:

lua_getglobal(state, "glob");
lua_getfield(state, -1, "nest");
lua_pushcclosure(state, &func, 0);
lua_setfield(state, -1, "name");

To add others field to glob.nest, just keep going:

...
lua_pushcclosure(state, &anotherfunc, 0);
lua_setfield(state, -1, "anothername");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.