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

Comments

Projects
None yet
2 participants
@kornerr

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

This comment has been minimized.

Show comment
Hide comment
@ThePhD

ThePhD Aug 1, 2017

Owner

{"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);
}
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);
}

@ThePhD ThePhD self-assigned this Aug 1, 2017

@ThePhD ThePhD closed this Aug 1, 2017

@ThePhD ThePhD added this to the Helpdesk milestone Aug 1, 2017

@ThePhD ThePhD removed the You're.Welcome label Aug 1, 2017

@kornerr

This comment has been minimized.

Show comment
Hide comment
@kornerr

kornerr Aug 2, 2017

Thanks. That worked fine.

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