Sign in to follow this  

Keeping a function on the stack in Lua

This topic is 1706 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
luaL_loadfile(L, "test.lua");

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 this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

This topic is 1706 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this