So I began implementing Lua into my 2D game engine and realized that it becomes increasingly difficult to run a script every update and a script every render while carrying over globals and not re-loading the script every time. What I mean by that is, I either have to load the script on EVERY update cycle and EVERY render cycle, OR I don't carry globals over by creating separate lua_State s. Any ideas, or am I just missing something HUGE with lua?
Moderators - Reputation: 30351
Posted 02 July 2014 - 09:39 PM
By "loading a script", do you mean sending a pile of text to the lua VM and asking it to execute it?
or am I just missing something HUGE with lua?
Imagine if in C, you decided to run an exe file every single Update/Render, rather than just calling a function.
You can do that in Lua - load the script once (just like we run an exe file once), and then call your Lua functions (from C) every time you want to update/render.
Members - Reputation: 399
Posted 03 July 2014 - 05:04 AM
call your Lua functions (from C) every time you want to update/render.
I'd add one note about performance.
PIL book shows simple example using lua_getglobal(). It's quite costly due to lua string construction from passed C string.
It's few times cheaper to store required function in registry, and later retrieving that function by integer id, obtained during function registration.