Jump to content
  • Advertisement
Sign in to follow this  
Krun

lua_load and calling functions

This topic is 5094 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

Hi! Me again! I can't figure out why this doesn't work: "If there are no errors, lua_load pushes the compiled chunk as a Lua function on top of the stack. Otherwise, it pushes an error message." - from the Lua manual Now, I get the pushed Lua function with this code: <code> luaL_loadfile(L, path); // load the script lua_CFunction funct = lua_tocfunction(L,lua_gettop(L)); // get the function from the top of the stack </code> But when I call func(L) I get a exception. I wan't to use this code cause it would allow me to execute the script multiple times just by calling the function instead of loading the whole script every time (too slow for game).

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Krun
<code>
luaL_loadfile(L, path); // load the script
lua_CFunction funct = lua_tocfunction(L,lua_gettop(L)); // get the function from the top of the stack
</code>

lua_tocfunction won't return anything but NULL if the function isn't actually a C function (that is, pushed onto the stack with lua_pushcfunction or lua_pushcclosure). And if you loaded it in with luaL_loadfile, it isn't a C function. Methinks you should check out lua_ref and lua_getref for how to retain a reference to a Lua object.

Share this post


Link to post
Share on other sites
This code works fine but only the first time. The second time I try to run it I get a "atempted to call a nil value" error cause Lua seems to pop the code once it is runned.

Is there any way to load a script from a file once, and then run it in memory more than once without having to reload the script file?

Share this post


Link to post
Share on other sites
Yes. As I said, use lua_ref and lua_getref. They'll allow you to hold a reference to your function from C.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
use lua_ref and lua_getref.

I belive this could be it, but I'm using Lua 5.0.2 and there is no lua_pushref function (only lua_ref,lua_unref,lua_getref).

Share this post


Link to post
Share on other sites
Just foud this post:
http://archive.neotonic.com/archive/lua-l/messages/14563?mode=author&noheader=1#doc_14563

The guy says that you have to create a table in the registry and load script chunks into it. That way the chunks will allways exsist (won't be destroyed after run). Still, I don't know how to implement the above system (Lua n00b).

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!