2

I'm using the latest version of LuaJit and need some help getting started. What I need is to have a bunch of functions exposed to the Lua environment which can be overridden inside the scripts to run the user's supplied code, these functions would then be called during set events from within C++

For example, when the user presses their TAB key down it would call a function from the lua environment such as OnScoreboardOpen() and when the user releases their TAB key it would call the corresponding function OnScoreboardClose() these functions could be attached to a metamethod like Game or GM.

Could someone point me to some tutorials or sample code showing how this can be accomplished? Thank you very much for your time.

KKlouzal
  • 732
  • 9
  • 32

1 Answers1

2

Basically you use these two functions: lua_pushXXX and lua_pcall

Depends on how you name the LUA function, it can be plain function or object method. i.e.

function OnScoreboardOpen()
end

OR

function Game:OnScoreboardOpen()
end

It's relatively simple to use plain function, just do:

// TODO: sanity check
lua_getglobal(L, name);
lua_pushnumber(L,123);
lua_pcall(...);
Non-maskable Interrupt
  • 3,841
  • 1
  • 19
  • 26
  • Thank you very much, I do need to use metamethods to keep everything organized. I'll look into these functions a bit more. Any suggestions on using metamethods? – KKlouzal Jul 11 '14 at 04:44
  • It depends. For singleton like global game facility, you don't need much organization. But for example, my own UI engine, I have hierarchy level of widgets. Some pure OO folks may name the lua-function as panelA:buttonB:onClick(button,modifier) While I choose to do panelA_buttonB:onClick(button,modifier) And make everything flat to reduce the complexity to resolve the object containing onClick. – Non-maskable Interrupt Jul 11 '14 at 05:01
  • By the way, to use meta-methods of an object, you just do lua_getglobal on the top-level name, then iterate thru' the object tree with lua_getfield. Also I should mention there are many ready-made library to do this as well. – Non-maskable Interrupt Jul 11 '14 at 05:11