5

Here's what I have so far... It creates global table called "mod", but I can't seem to add indexes to the table...

lua_newtable(L);
lua_setglobal(L,"mod");

1 Answer 1

8

The manual says:

void lua_setfield (lua_State *L, int index, const char *k);

Does the equivalent to t[k] = v, where t is the value at the given valid index and v is the value at the top of the stack.

This function pops the value from the stack.

So, more precisely: Push whatever you want to add onto the stack, then call lua_setfield. For example:

lua_pushnumber( L, 42 );
lua_setfield( L, -2, "answer_to_life_universe_and_rest" )

This inserts the field "answer_to_life..." with value 42 into the table.

Sign up to request clarification or add additional context in comments.

1 Comment

nvm :P I had the lines mixed up

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.