C++, Lua: Storing references to Lua functions in C++.

Started by
0 comments, last by dmail 13 years, 4 months ago
I'm using Lua to handle scripting in my game, the core engine of which is written in C++.

In terms of the script format, each script has the same functions in it--general things describing how the game objects react in response to events generated by the engine (collision and the like). A sort of soft polymorphism, if you will.

Currently my code to call Lua functions from C++ is very inefficient -- essentially, every object reloads its script every turn. I was doing this so that the functions getting called would be the proper ones associated with the object calling them. However this is a pretty sloppy method of getting this behavior.

So. I remember someone telling me you could get references to Lua functions, store those, and then call the functions by reference instead of directly. Except I'm not really that sure how to.

Currently I'm loading a script in with lua_dofile, doing lua_getglobal on a function, storing a reference to it in the lua registry with LuaL_ref, and... repeating the last two steps for every function. And I'm retrieving them from the lua_registry with rawgeti.

This doesn't seem to actually work though. Either the references lose their associated functions or point to the wrong things.

Anyone have any experience with this or know what I'm trying to do?
Advertisement
What you say you are doing is the correct method to grab a registry reference and later use it. Can you please show the actual code you use or a minimal example which does not seem to work for you.

This topic is closed to new replies.

Advertisement