Calling C++ class function from LUA script

Ian Young

I'm attempting to learn how to use lua/luabridge to call member function of a class, but I'm having some trouble:

Here is the simple test class:

class proxy
{
public:
    void doSomething(char* str)
    {
        std::cout << "doDomething called!: " << str << std::endl;
    }
};

And the code that uses it:

int main()
{
    lua_State* L = luaL_newstate();
    luaL_openlibs(L);
    proxy p;
    luabridge::getGlobalNamespace(L)
        .beginClass<proxy>("proxy")
        .addFunction("doSomething", &proxy::doSomething)
        .endClass();

    std::string filename("test.lua");
    if (luaL_dofile(L, filename.c_str()) || lua_pcall(L, 0, 0, 0)) {
        std::cout << "Error: script not loaded (" << filename << ")" << std::endl;
        L = 0;
        return -1;
    }

    return 0;
}

And finally, the lua script:

proxy:doSomething("calling a function!")

There is probably several errors here, but specifically what I want to do, is call the the member function for the proxy instance, from the lua script, as if I was calling:

p.doSomething("calling a function!");

I'm aware that there are a lot of similar questions, but none I have found so far directly answers my question.

Currently the script does not even load/execute so I'm a bit puzzled.

Ian Young

As it turned out, I had to change the code to:

int main()
{
    lua_State* L = luaL_newstate();
    luaL_openlibs(L);
    proxy p;
    luabridge::getGlobalNamespace(L)
        .beginClass<proxy>("proxy")
        .addFunction("doSomething", &proxy::doSomething)
        .endClass();

    std::string filename("test.lua");
    if (luaL_dofile(L, filename.c_str())) {
        std::cout << "Error: script not loaded (" << filename << ")" << std::endl;
        L = 0;
        return -1;
    }
    // new code
    auto doSomething = luabridge::getGlobal(L, "something");
    doSomething(p);
    return 0;
}

And change the script:

function something(e)
    e:doSomething("something")
end

This actually works better for me. The script wouldn't work because the lua stack didn't know anything about the proxy instance, and I had to call a lua function directly, which, in turn, called the class member function.

I don't know if there is an easier way to do it, but this is good enough for me.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Call a function returned by a lua script from C

C calling Lua function in a for loop

c++ Calling a function from a class pointer

Calling Lua Function from C: Minimal example results in LUA_ERRRUN

passing Lua script from C++ to Lua

Lua registered C function calling another one

Returning output from bash script to calling C++ function

Calling base function from variadic template class in c++14

Calling a function from another class in C#/.NET MVC App

C++ Calling overwritten function in derived from base class

Calling a virtual function from a destructor of a derived class in c++

Displaying message by calling function from class in c#

C++ Calling set function from another class in file reader

C# calling function in class from other namespace

Calling a function in the main Form from another Class in C#

SpriteKit calling function from class

Calling a function from an extended class

Calling a function from a class method

Calling a function from another class?

Calling a function from a class in main

calling a Redis function(loaded Lua script) using Lettuce library

How to make table and variable values to persist when calling a lua function from c++?

Calling class from script generated from CoffeeScript

How to access variables of the class that runs the Lua script from Lua

Calling outer class function from inner class

Calling function from one class in another class

Calling lua script from redigo throwing error wrong number of args

Calling a class function from within another function

Python calling a function from another function in a class