-
Notifications
You must be signed in to change notification settings - Fork 348
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
addCFunction in 3rd party class #285
Comments
If you use addCFunction, you need to make sure you provide a lua_CFunction: int myCFunction(lua_State*) { return 0; }
.addCFunction("MyCustomFunc", &myCFunction)) or eventually decay a lambda to a function pointer .addCFunction("MyCustomFunc", +[](lua_State*) -> int { return 0; }) |
Your first suggestion does not work. Because the class on which I want to do addCFunction cannot be modified. It's a 3rd party class.
The second suggestion with the lambda does not work as well for the same reason. What luabridge is missing here is a specialization for addCFunction where the bound function is not part of the class. |
You are right, i thought this was also in vanilla luabridge, but this is only possible with luabridge3. can't you use a normal function ? .addFunction("MyCustomFunc", +[](MyClass* self, lua_State* L) { }) In luabridge3, addCfunction disappeared and i added support for generic CFunction with self object on top of the stack, see https://github.com/kunitoki/LuaBridge3/blob/master/Source/LuaBridge/detail/Namespace.h#L978-L998 An example usage: struct MyClass {};
int cFunctionMethod(lua_State* L)
{
auto ref = luabridge::LuaRef::fromStack(L, 1);
if (!ref.isUserdata() || !ref.isInstance<MyClass>()) {
return 0;
}
auto arg = luabridge::LuaRef::fromStack(L, 2);
if (!arg.isNumber()) {
return 0;
}
std::error_code ec;
luabridge::push(L, arg.cast<int>() + 1000, ec);
return 1;
}
luabridge::getGlobalNamespace(L)
.beginClass<MyClass>("MyClass")
.addFunction("method", &cFunctionMethod)
.endClass();
runLua("result = MyClass():method(1000)");
ASSERT_EQ(2000, result<int>()); |
@kunitoki Nice, is it possible to use that piece of code in the "official" luabridge? |
Yeah, not pretty but you can "hack it" somehow registering a static function to your class with addStaticFunction but call it with : instead of . |
Ok, let me implement this :) |
This library is just awesome but it seems that I'm overseeing something on this simple problem.
I have a class from a third party library which cannot be modified at all. I'm exposing this class with the usual beginClass.
Now I'd like to use addCFunction on this class but I cannot do that because it only accepts class member functions.
std::function also doesn't work here.
How can I add a C function with lua_State and custom return values to my class which cannot be modified?
Thank you!
The text was updated successfully, but these errors were encountered: