Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a Lua Table in C++, and pass it to a Lua function?

In C++, I have a map<string, string>, containing an unknown number of entries. How can I pass this to a Lua function, so that the Lua function can use the data as a table?

like image 598
Rocketmagnet Avatar asked Jan 17 '09 18:01

Rocketmagnet


1 Answers

if you want a real lua table:

lua_newtable(L);
int top = lua_gettop(L);

for (std::map::iterator it = mymap.begin(); it != mymap.end(); ++it) {
    const char* key = it->first.c_str();
    const char* value = it->second.c_str();
    lua_pushlstring(L, key, it->first.size());
    lua_pushlstring(L, value, it->second.size());
    lua_settable(L, top);
}

with the right types for your map substituted in..

like image 167
sean riley Avatar answered Oct 27 '22 14:10

sean riley