Jump to content
  • Advertisement
Sign in to follow this  
Exponent

Question about multiple lua states.

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

Hello! Consider this scenario. I have a game with a set of lua functions which act as callbacks for various system events, not unlike the glut system of callbacks. I have functions defined in my lua script such as OnKeyPress(Key, Flags), OnHudRedraw(), OnIdle(Tick), etc.. which are all called at appropriate times. Now, if I wanted multiple scripts to define and control these callbacks, say I wanted script1.lua to handle OnKeyPress as well as script2.lua, would I create two different lua states, or basically a lua state for each script? I'm not entirely sure how to approach this problem. Any help is appreciated, thanks!

Share this post


Link to post
Share on other sites
Advertisement
only one state you should create, i think.

you may define a game event for callback, and a multi map to save events and functions, like this:

//cpp pseudo code
typedef int EVENT;
typedef int LUAFUNC;
typedef std::multimap<EVENT, LUAFUNC> CALLBACKS;

//define all events
//and export to script
#define KEY_PRESS 1

//lua pseudo code
//script1.lua

function on_key_press(key, rep) end

--register this function to a global script instance
register_event(KEY_PRESS, on_key_press)

//script2.lua
function on_key_press(key, rep) end
--same to script1.lua
register_event(KEY_PRESS, on_key_press)

//in cpp
int register_event(lua_state *L)
{
//get event code and lua function
//save to multimap
}

when you get a system call back(KEY_PRESS), just call lua functions



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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!