I'm using sol2 (v3.3.0) in C++ to run Lua coroutines. I want to pass a Lua function to C++ and execute it as a coroutine. However, my attempts to convert sol::function to sol::coroutine are not working.
Here is a minimal example that doesn't work:
#include <sol/sol.hpp>
int main() {
sol::state lua;
lua.open_libraries(sol::lib::base);
lua.set_function("run_coroutine", [](sol::function func) {
sol::coroutine co = func;
co();
co();
co();
});
lua.script(R"(
function story()
print("1")
coroutine.yield()
print("2")
coroutine.yield()
print("3")
end
run_coroutine(story)
)");
}
The expected output is
1
2
3
but actually nothing is shown.
How can I correctly run the Lua function as a coroutine in C++?