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.

1 Answer 1

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(...);
Sign up to request clarification or add additional context in comments.

3 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.