Original Post
I have a working Lua setup that allows me to define sprite behaviour in scripts. However, it's really quite clunky and I'm reaching the point where I need to improve it. The problem is that I don't really know how to go about doing this. My current setup (link contains a sample Lua script and the relevant C/C++ functions) has a lua_State* for each sprite instance, which, on creation of the sprite, loads the relevant script file and registers a set of C functions. When I want to call a Lua function (e.g. to update the sprite state), I put the C++ state into a table, push the table onto the stack, call the function which modifies the state, pop the table, and update the C++ state to match what is in the popped table. This seems needlessly complex. Calling C functions from Lua is easy enough; no complaints there. But calling Lua functions in one script file from Lua functions in another script file (e.g. if a dying enemy wants to tell the scorekeeping sprite to update itself) is absolutely horrible. I can only pass one argument at a time (well, if I wanted to pass two arguments, then I'd have to make another version of the C functions that handle the interface between two sprites' scripts), and the mere fact that getting from Lua to Lua requires a detour through C just grates on my nerves. I'm fairly certain that there's a better way to do this. I just don't know where to get started. Any advice would be appreciated, even if it's only pointing me to a specific bit of the Lua documentation that would be pertinent.