Sign in to follow this  

Accessing embedded lua from C++?

Recommended Posts

I'm trying to figure out how to do certain things to lua from C++, but I'm having a bit of trouble. Such as creating a table in lua and adding a variable to it, the equivalent to below from C++
t = {}
t[1] = 1
just the basic creation of a table and adding a variable too it. So far, I've only figured out how to do this with the global table, _G. I appreciate any help with this.

Share this post

Link to post
Share on other sites
Basically, you must:
- push the table onto the stack (lua_newtable(L);)
- push the key onto the stack (lua_pushnumber(L, 1);)
- push the value onto the stack (lua_pushnumber(L, 1);)
- move the key and value into the table (lua_settable(L, -3))

You need section 3 of the Lua manual, especially section 3.11, and it won't hurt to take a look at the Programming In Lua book too, eg. this bit on table manipulation.

Note that the example I've given doesn't name your table 't'. To name it t, would mean inserting it into the global namespace associated with the name 't'. Since the global namespace is just another table, doing this shouldn't be too hard.

You might also want to search the Scripting Languages and Mods forum, where lots of Lua questions and answers come up.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this