3

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.

MahanGM
  • 2,352
  • 5
  • 32
  • 45
  • Put the full error message as you see it, in your question please. – greatwolf Jul 10 '13 at 19:38
  • @greatwolf I've put it. – MahanGM Jul 10 '13 at 21:15
  • The error should show what the candidate functions are. – greatwolf Jul 10 '13 at 21:37
  • 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 Jul 10 '13 at 21:48
  • What does `FakeScript::GetMeta` and `FakeScript::SetMeta` look like? Those aren't functions you're trying to bind, they're class methods. – greatwolf Jul 10 '13 at 22:19
  • They're static class methods. There is no difference If I place functions instead of them. I've put their definitions. – MahanGM Jul 10 '13 at 22:59

1 Answers1

5

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.

Oberon
  • 3,219
  • 15
  • 30
  • Actually I tried that before I come here. I couldn't get it to work because of overload's corruption so I dumped it away. Now you helped me all the way! I'm going to try that, but I came up with another approach to use function overloads to achieve my need. – MahanGM Jul 18 '13 at 20:27