Sign in to follow this  
MeshGearFox

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

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this