# Keeping a function on the stack in Lua

## Recommended Posts

Six222    453

I've been recently learning how to use the Lua C API and I was wondering how you would keep a loaded file in the stack after it's been called? I would like to load a file and to be able to run it multiple times for example:

//Pushes onto stack

lua_pcall(L, 0, 0, 0);

//Some other code
...

//I'd like to call the function again here but lua_pcall has already popped it of the stack?


Do I really need to load the file again or is there a way to keep a hold of the chunk and just push it back onto the stack?

##### Share on other sites
Gambini    384

luaL_ref is what you are looking for. As long as you don't destroy/close the lua_State that the ref was made in, the function will exist until you call luaL_unref. lua_ref is a macro that will make the ref in the global registry if you don't want to specify a table yourself.