Skip to main content
gamedev.net
🔒 Locked

Lua and Lists

Started by quasty May 28, 2004 at 2:34 AM 3 replies 1k views
Original Post
quasty
quasty
Hi, does LUA support some sort of lists with (fast) sort and search algorithms? thanx
quasty
quasty
Hi,

I''ve found out that the lua-table is the list of choice

Does anyone know a way to convert in C a std::list to a table?

thanx
trolocsis
trolocsis
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.

ktw6675
ktw6675
If you use Luabind it''s easy.

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.
quasty
quasty
wow - thanks a lot!

That works great

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.