Luabind & stl

Started by
1 comment, last by thelovegoose 14 years, 5 months ago
I want to be able to do the following: Pass a container to a lua function Return the container to the c++ calling code Pass the container to another lua function Add to and remove from the container in lua Iterate over the container in lua Edit a particular element from the container in lua So far I've been trying to achieve this through combinations of exposing or wrapping stl containers, using return_stl_iterator, using luabind::newTable to create a table from a container. Can anyone reccomend a way to achieve the above requirements - or advise if I'm attacking this in the wrong way? If I can provide more info please let me know, Many thanks in advance,
Advertisement
Hello,

Having looked at several different ways of achieving what I want, the standout option by far (provided I can get this to work) is to be able to use at least
push_back, push_front, pop_back, pop_front, back, front and at where applicable from the stl containers in LUA. I don't particularly need iterators as I can get by with the above functions.

Push & pop are simple enough to expose and use, but I'm struggling with back.

Heres my code to expose vector<T>::back :

luabind::module(mLuaState) [			luabind::class_<std::vector<Gaia::Vector2>>("V2vector")				.def(luabind::constructor<>())				.def("push_back",&std::vector<Gaia::Vector2>::push_back)				.def("back",(Gaia::Vector2&(std::vector<Gaia::Vector2>::*)(void))&std::vector<Gaia::Vector2>::back, luabind::dependency(luabind::result, _1))		];luabind::module(mLuaState) [			luabind::class_<Gaia::Vector2>("Vector2")				.def(luabind::constructor<>())				.def_readwrite("x", &Gaia::Vector2::x)				.def_readwrite("y", &Gaia::Vector2::y)		];


and here is the attempt to call in lua:

path = BehaviourManager:getPath(currentPos, AIArgs.targetPoint, unitSize);	testz = path:back();


(BehaviourManager is correctly defined as a global, getPath definitely returns a std::vector<Vector2>)

the (custom) lua error is :

[LUA] Error : ..\Data\media\scripting\lua\testIdle.lua:43: attempt to call method 'back' (a nil value) source : @..\Data\media\scripting\lua\testIdle.lua what : Lua line : 8

I expect that its something to do with the return type of back - ie that its a reference, or that there are 2 overloads of back (returning reference or const reference) but I don't see why thats a problem?

Is anyone kind hearted enough to help? This is really holding me up so any suggestions would be very welcome :)


[Edited by - thelovegoose on November 5, 2009 5:59:48 PM]
Ugh - please ignore I was returning a std::list from getPath - what a gigantic waste of time!

This topic is closed to new replies.

Advertisement