Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

quasty

Lua and Lists

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, does LUA support some sort of lists with (fast) sort and search algorithms? thanx

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!