Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot execute an object member function call from Lua #465

Closed
kornerr opened this issue Aug 1, 2017 · 2 comments
Closed

Cannot execute an object member function call from Lua #465

kornerr opened this issue Aug 1, 2017 · 2 comments

Comments

@kornerr
Copy link

kornerr commented Aug 1, 2017

Hi.
I have exported a class, its methods and the class' instance to Lua using the following example: http://sol2.readthedocs.io/en/latest/tutorial/cxx-in-lua.html

Here is how I did it: https://github.com/kornerr/cpp-callback-script/blob/master/main.cpp#L58
The script is a single line: env:call("module", {"One", "Two"})

However, I only get "Segmentation fault".

Any idea what I'm doing wrong?

@ThePhD
Copy link
Owner

ThePhD commented Aug 1, 2017

{"One", "Two"} is a table. const Strings& is a std::vector<std::string>. C++ containers are not automatically serialized as tables unless explicitly requested, due to Long Standing Issues and Lots of Bikeshedding™.

If you would like sol2 to recognize something as a table, you need to wrap the arguments with sol::as_table/sol::as_table_t or sol::nested, as demonstrated in this example.

Specifically, you can add a call to your binding that looks like:

void runSol(Environment *env, const char *fileName)
{
    sol::state lua;

    // Register environment instance.
    lua["env"] = env;
    // Register environment class.
    lua.new_usertype<Environment>(
        "Environment",
        "addClient", &Environment::addClient,
        "call", [](Environment& env, const String &key, sol::as_nested<Strings> strings) {
            const Strings& values = strings;
            env.call(key, values);
        }
    );
    // Load and execute script.
    lua.script_file(fileName);
}

@kornerr
Copy link
Author

kornerr commented Aug 2, 2017

Thanks. That worked fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants