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
Accessing Lua self object from a C++ function #1559
Comments
Would something like this work for you? #define SOL_ALL_SAFETIES_ON 1
#include <sol/sol.hpp>
struct Cpp {
};
int main() {
sol::state lua;
lua.open_libraries(sol::lib::base, sol::lib::package);
auto cpp_meta = lua.new_usertype<Cpp>("Cpp");
lua.script(R"(
Cpp.memberFunc = function(...)
print(...)
end
cpp = Cpp.new()
cpp:memberFunc("hello world!")
-- prints sol.Cpp: 0x5b0038 hello world!
)");
return 0;
} Its not what you ask, but it achieves what you set out to do in your code example from what I see. |
@Rochet2 Thank you for your suggestion.
However the goal is cpp:memberFunction to be called from the C++ class such
that the Lua implementation has access to the Lua self table. I know I can
pass in the C++ this pointer but that won’t ensure access to the self
object.
…On Sun, Dec 3, 2023 at 12:30 PM Rochet2 ***@***.***> wrote:
Would something like this work for you?
#define SOL_ALL_SAFETIES_ON 1
#include <sol/sol.hpp>
struct Cpp {
};
int main() {
sol::state lua;
lua.open_libraries(sol::lib::base, sol::lib::package);
auto cpp_meta = lua.new_usertype<Cpp>("Cpp");
lua.script(R"( Cpp.memberFunc = function(...) print(...) end cpp = Cpp.new() cpp:memberFunc("hello world!") )");
return 0;
}
Its not what you ask, but it achieves what you set out to do in your code
example from what I see.
—
Reply to this email directly, view it on GitHub
<#1559 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAZKSUCE2KYGS4NUDHAOLHTYHRIFDAVCNFSM6AAAAABAEUFCKWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQMZXGQZTMOJRGQ>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
In short I need something like Attaching one of the tests using
|
I think I found the solution, it's not super obvious, but there is a working example: https://github.com/ThePhD/sol2/blob/develop/examples/source/self_from_lua.cpp The key is to dig inside the state's stack using this_state (from the example above):
|
I need to call a Lua function attached to the object instance from C++ class member function.
In other words I need to pass the self object to the function when calling from C++. If I know the name of the object I can access it by state["objectName"] and pass it. But unless I cache it somewhere I can't really convert this pointer to self object. I'm looking for an elegant mechanism to do this as I have a ton of bound C++ classes that need to call custom functions.
Ideally I'd want to do this at construction time. Perhaps by getting access to the Lua table object as a constructor parameter? Would that even work, or is the table created after the object has been constructed?
class Cpp {
public:
...
void callLuaMemberFunction {
if (luaMemberFunc) {
luaMemberFunc(, params...);
}
}
void bindLuaFunction(const std::string& key, sol::function func) {
if (key == "memberFunc") {
luaMemberFunc = std::move(func);
}
// ... [Handle other overridable functions similarly]
}
private:
sol::function luaMemberFunc;
}
at binding time:
cpp.set(sol::meta_function::new_index, [](Cpp& _this, const std::string& key, sol::function func) {
_this.bindLuaFunction(key, func);
});
Lua:
cpp = Cpp()
cpp:memberFunc = function(...)
end
Currently I'm solving this by passing the name to the object at construction which then looks up the object by name every call. A typo could be an issue if we pass a different name than the object's assigned name. Something I'd like to avoid. I also want to avoid caching the object for each Lua member function (I'm assuming I'd have access to it at new_index call).
The text was updated successfully, but these errors were encountered: