2

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?

1 Answer 1

3

It's just a normal entry on the stack. Check it with lua_isfunction() then use luaL_ref() to generate a reference (so that the garbage collector doesn't steal it), and then maybe lua_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().

Sign up to request clarification or add additional context in comments.

1 Comment

Oh overlooked the later execute bit, that's just lua_call() but make sure you read the details under lua.org/manual/5.1/manual.html#lua_call

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.