Original Post
🔒 Locked
Lua and Lists
Advertisement
What you need to do is iterate through the list and push the key and value onto the table. Indexes start at 1 in lua, so something like this should work (pseudocode wise):
lua_State *L
lua_newtable(L);
int i = 1;
for(somelist it=begin; it!=end; ++it)
lua_pushnumber(i); // Push index of element
lua_pushstring(it); // Or whatever datatype you want to push
lua_settable(L, -3); // Set the key/value pair to the table located three positions down from the top of the stack.
lua_State *L
lua_newtable(L);
int i = 1;
for(somelist it=begin; it!=end; ++it)
lua_pushnumber(i); // Push index of element
lua_pushstring(it); // Or whatever datatype you want to push
lua_settable(L, -3); // Set the key/value pair to the table located three positions down from the top of the stack.
If you use Luabind it''s easy.
Say you have a class A with a list member.
Then just compile this code:
Then your Lua code could look like this:
Now t contains all the values of names.
Say you have a class A with a list member.
class A {
list<string> values;
};
Then just compile this code:
lua_State* L = lua_open();
luabind::open(L);
module(L)
[
class_<A>("A")
.def(constructor<>())
.def_readwrite("values",&A:values,return_stl_iterator)
];
Then your Lua code could look like this:
local obj = A()
local t = {}
for v in obj.values do
table.insert(t,v)
end
Now t contains all the values of names.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement