If I have some lua code like this:
doSomething(function()
print("Hello!")
end)
How can I make it that, using the C API, I create C Lua function for doSomething that can then save the function passed to it to be executed a later date?
It’s just a normal entry on the stack. Check it with
lua_isfunction()then useluaL_ref()to generate a reference (so that the garbage collector doesn’t steal it), and then maybelua_topointer()to store a pointer to this object, depends what you want to do with it.When you’re finished it’s just a
luaL_unref().