3
votes

I'm trying to expose my std::map<std::string, std::string> as a class property to Lua. I've set this method for my getter and setter:

luabind::object FakeScript::GetSetProperties()
{
    luabind::object table = luabind::newtable(L);
    luabind::object metatable = luabind::newtable(L);

    metatable["__index"] = &this->GetMeta;
    metatable["__newindex"] = &this->SetMeta;

    luabind::setmetatable<luabind::object, luabind::object>(table, metatable);

    return table;
}

This way it makes me able to do something like this in Lua:

player.scripts["movement"].properties["stat"] = "idle"
print(player.scripts["movement"].properties["stat"])

However, the code I've provided in C++ doesn't getting compiled. It tells me there is an ambiguous call to overloaded function at this line metatable["__index"] = &this->GetMeta; and the line after it. I'm not sure that I'm doing this correctly.

Error message:

error C2668: 'luabind::detail::check_const_pointer' : 
ambiguous call to overloaded function
c:\libraries\luabind-0.9.1\references\luabind\include\luabind\detail\instance_holder.hpp    75

These are SetMeta and GetMeta in FakeScript:

static void GetMeta();
static void SetMeta();

Previously I was doing this for getter method:

luabind::object FakeScript::getProp()
{
    luabind::object obj = luabind::newtable(L);

    for(auto i = this->properties.begin(); i != this->properties.end(); i++)
    {
        obj[i->first] = i->second;
    }

    return obj;
}

This works fine, but it's not letting me to use setter method. For example:

player.scripts["movement"].properties["stat"] = "idle"
print(player.scripts["movement"].properties["stat"])

In this code it just going to trigger getter method in both lines. Although if it was letting me to use setter, I wouldn't be able to get key from properties which it is ["stat"] right here.

Is there any expert on LuaBind here? I've seen most of people say they've never worked with it before.

1
Put the full error message as you see it, in your question please. - greatwolf
@greatwolf I've put it. - MahanGM
The error should show what the candidate functions are. - greatwolf
Well this is all I get. Actually I need someone to tell me how to assign functions for metatable fields in LuaBind way. See, I can change this to use a function to access my map instead of a property, but I'm concerned about user's programming style. - MahanGM
What does FakeScript::GetMeta and FakeScript::SetMeta look like? Those aren't functions you're trying to bind, they're class methods. - greatwolf

1 Answers

5
votes

You need to use the (undocumented) make_function() to make objects from functions.

metatable["__index"] = luabind::make_function(L, &this->GetMeta);
metatable["__newindex"] = luabind::make_function(L, &this->GetMeta);

Unfortunately, this (the simplest) overload of make_function is broken, but you just need to insert f as the second parameter in make_function.hpp.