ThePhD / sol2

Sol3 (sol2 v3.0) - a C++ <-> Lua API wrapper with advanced features and top notch performance - is here, and it's great! Documentation:
http://sol2.rtfd.io/
MIT License
4.06k stars 492 forks source link

How to pass Lua Closures into C++ Functions in Sol3 ? #1583

Closed theanimatorspal closed 4 months ago

theanimatorspal commented 4 months ago

This is inside a new_usertype

        "SetDrawCallback",
        [](Jkr::WindowMulT& inWindow, sol::function& inFunction) {
            inWindow.SetDrawCallBack([&inFunction](void*) { inFunction(); });
        },

but it isn't working. I also tried setting sol::function instead of sol::function&, it compiled but gave runtime error in lua_gettop() function.

skaarj1989 commented 4 months ago

Have you tried capturing inFunction by value? maybe it's a lifetime issue?

theanimatorspal commented 4 months ago

Yup. " I also tried setting sol::function instead of sol::function&," I said.

skaarj1989 commented 4 months ago

I also tried setting sol::function instead of sol::function&,

This means that you have a temporary argument sol::function inFunction and then you capture that temporary argument by reference in the next lambda (inWindow.SetDrawCallBack).

theanimatorspal commented 4 months ago

Ah man, thank You.