I'm building a simple 2D game engine, and its getting bigger and bigger, exposing all of the function in Lua will be impossible: so I'm trying to automate a little bit the process, Is there anyway to get all the n arguments (with different types) from the stack at once and inject them directly into the C++ function. I already automated functions args checking. still the function binding which is a little bit tricky
For E.g: I have normal code for changing sprite position:
int LuaSprite::SetSpritePosition(lua_State* L)
{
Drawable* sprt = (Drawable*)lua_touserdata(L, 1);
int x = (int)lua_tonumber(L, 2);
int y = (int)lua_tonumber(L, 3);
sprt->setPosition(Vec2i(x, y));
return 0;
}
The concept that i want achieve is more less like this:
int LuaSprite::SetSpritePosition(lua_State* L)
{
LOAD_ARGS(Sprite*, int, int)
GET_ARG(1)->setPosition(Vec2i(GET_ARGS_FROM(1)));
LOAD_RETURN(true, x, y, 3);
return 3;
}
So
- LOAD_ARGS will get from the stack The given type respectively.
- GET_ARG(i) will get arg at index i.
- GET_ARGS_FROM(i) will do something like this x,y,z,type
I'm not requesting same behavior as it could be impossible to do, but something similar at least
And I'm sure that this isn't achievable only using 'plain' C++ and that I need also some 'magic' macros.
I'm not using the ready auto Lua binding "libraries" because there is custom structs and some functions are using complex structures and classes
I have done a lot of searching and I feel really lost.
I found Luabridge to be extremely handy when performing these tasks. It's a header only library that works with older C++ code (before c++11 and after).
You don't program against the Lua stack directly, instead you wrap normal class/struct into binding definition.
The binding code looks something like this:
For Game Objects, I'll generally manage the lifetime in C++ and just pass pointers to Lua. Luabind lets you override the Lua CTor (__call) and ~Tor (__gc). Here are my copies:
And their use:
To call Lua Code from C++, you use LuaRefs which (IIRC) are basically variants that can be any Lua Type.
If you're interested, I found Zerobrane to be a good debugger, you just need to add the socket lua libraries