Sign in to follow this  

Luabind, raw policy and member functions

This topic is 2956 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

I'm trying to bind a member function of a C++ class to a LUA class using the raw policy (I want to return an array, and that's apparently the best way). Anyway, here's the code I've got:
class player
{
private:
    luabind::object findunits(lua_State *L);

public:
    void init();
};

void player::init()
{
    luabind::module(L)
    [
        luabind::class_<player>("player")
          .def("findunits", &player::findunits, luabind::raw(_2))
    ];
    luabind::globals(L)["self"] = this;
}

luabind::object player::findunits(lua_State *L)
{
  luabind::object options(luabind::from_stack(L, -1));
  luabind::object result = luabind::maketable(L);

  std::vector<entity> entities = do_findunits(options);
  int index = 1;
  BOOST_FOREACH(entity ent, entities)
  {
    result[index++] = lua_wrapper(ent);
  }

  return result;
}
The lua_wrapper class is just a wrapper I used to expose functions of the entities to script. Not really important... anyway, in the LUA code, I do this:
local units = self:findunits({ unit_type="factory" })
for u in units do
    log.write(u:kind())
end
It all compiles, but at runtime, I get the following error: lua runtime error No matching overload found, candidates: luabind::object findunits(player&,lua_State*) I've tried a few different things (e.g. making my findunits method static and taking a player reference as the first parameter, which is what that error would suggest, but that didn't change anything...) Anybody have any experience with this?

Share this post


Link to post
Share on other sites
After some more trial-and-error, I've figured out how to do it. For anybody that's interested, here's the C++ side:

class player
{
private:
luabind::object findunits(luabind::object options, lua_State *L);

public:
void init();
};

void player::init()
{
luabind::module(L)
[
luabind::class_<player>("player")
.def("findunits", &player::findunits, luabind::raw(_3))
];
luabind::globals(L)["self"] = this;
}

luabind::object player::findunits(luabind::object options, lua_State *L)
{
luabind::object result = luabind::maketable(L);

std::vector<entity> entities = do_findunits(options);
int index = 1;
BOOST_FOREACH(entity ent, entities)
{
result[index++] = lua_wrapper(ent);
}

return result;
}

And here's the LUA side:

local units = self:findunits({ unit_type="factory" })
for _,u in ipairs(units) do
log.write(u:kind())
end

The problem was that luabind::raw policy has no effect on the parameters to functions, only the return type. So you still have to specify all of the formal parameters in the method signature and it still matches those parameters when you call the function from LUA. The difference is that you get access to the lua_State and can therefore create LUA tables.

Pretty neat, once you get the hang of it. Too bad I couldn't find any examples which took things this far: there were lots of examples where a luabind::raw was used with a function that took no parameters, however!

Share this post


Link to post
Share on other sites

This topic is 2956 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this