Jump to content
  • Advertisement
Sign in to follow this  
MeshGearFox

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

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

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
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.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!