Jump to content
  • Advertisement
Sign in to follow this  
Six222

Keeping a function on the stack in Lua

This topic is 1924 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
Advertisement

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!