Accessing embedded lua from C++?

This topic is 3621 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

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 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 on other sites
Thanks, that helped immensely. I didn't know the scripting language section included the actual embedding of it, I thought it was only for the actual use of it :o