I've been scouring the web all day and I can't figure this out.
1) does the below code make sense?
2) how would I go about extending it to support multiple exposed classes?
//less relevant functions have been omitted below
static int loadEntity(IN lua_State *L)
{
//QUESTION: I'm assuming Lua automatically manages memory and it's safe to call
//lua_newuserdata without worrying about reallocation/freeing. This seems weird,
//but I haven't found a way around it
IEntity** ptr = (IEntity**)lua_newuserdata(L, sizeof(IEntity*));
*ptr = entityptr;
luaL_getmetatable(L, "IEntity");
lua_setmetatable(L, -2);
return 1;
}
static int loadGame(IN lua_State *L)
{
IGame** ptr = (IGame**)lua_newuserdata(L, sizeof(IGame*));
*ptr = gameptr;
luaL_getmetatable(L, "IGame");
lua_setmetatable(L, -2);
return 1;
}
static int AddModifier(IN lua_State * L)
{
IGame* game = NULL;
/////////////////////////////////////////////////////////////////
/////////////////////PROBLEM COMES UP HERE///////////////////////
/////////////////////////////////////////////////////////////////
game = checkGame(L, 2); //<- doesn't work, runs fine if I comment this out; debugging says IGame isn't even present on the stack
IEntity* entity = checkEntity(L, 1); //works as expected
...
return 1;
}
void luaExposeInterface(IN lua_State * L, IN const luaL_Reg listInterfaceFunctions[], IN char * sName)
{
luaL_newmetatable(L, sName);
//expose all the functions in the interface
luaL_setfuncs(L, listInterfaceFunctions, 0);
lua_pushvalue(L, -1);
lua_setfield(L, -1, "__index");
lua_setglobal(L, sName);
}
int main()
{
static const luaL_Reg listGameUIFuncs[] = {
{ "load", loadGame },
...
{ NULL, NULL }
};
static const luaL_Reg listEntityFuncs[] = {
{ "load", loadEntity },
...
{ "AddModifier", AddModifier },
{ NULL, NULL }
};
//this should expose both IGame and IEntity
luaExposeInterface(L, listGameUIFuncs, "IGame");
luaExposeInterface(L, listEntityFuncs, "IEntity");
...
//run the script
lua_getglobal(L, "EntryPoint");
LUAErr(L, lua_pcall(L, 0, 0, 0));
}
Here's a relevant excerpt from the Lua code:
function EntryPoint()
//successfully calls loadGame
local gamestate = IGame:load()
//sucessfully loads loadEntity
local entity = IEntity:load()
//fails if I try to access IGame
entity:AddModifier("name", 1.0, 100)
end
To recap the code - the problem is exposing both IGame and IEntity as separate interfaces within Lua. I'll be wanting to expose more than one entity instance as well.
To be frank, I'm probably missing the entire point here and doing it wrong, but I can't really find concrete info on how to achieve what I really want. Which, on the Lua side, would look something like this:
function EntryPoint(entity1, entity2, gamestate)
...
end
Since I'm not completely sure how, or in fact what it is that I'm trying to achieve, some direction or references would be nice. I know there are several binding libraries out there, but I'm still very green when it comes to Lua and I wanted to familiarize myself with the library first. Additionally, there are some requirements that I'm not ready to look over: for instance, I've excluded luabind from consideration as I'm not using Boost and do not want to add it to my codebase.