PtokaX forum

Development Section => Your Developing Problems => Topic started by: vimesSam on 07 June, 2006, 11:09:35

Title: [toLua++]Access to C++ function from Lua Script
Post by: vimesSam on 07 June, 2006, 11:09:35
I'm using CEGUI and in its latest version it comes with a script module using Lua and ToLua++
I've had a hard time getting toLua++ to work but finally managed to get a precompiled version of toLua++ via irrLua package which you can find here (http://prdownloads.sourceforge.net/irrlua/IrrLua-0.7-src.zip?download) and with which I managed to create my cpp Lua file from a .pkg (i.e a modified .h header)
The thing is that I get an error on runtime :
07/06/2006 17:12:14 (Error) Exception: (LuaScriptModule) Unable to execute scripted event handler: handleReturnToGame
[string "../datafiles/lua_scripts/demo8.lua"]:180: attempt to index global `LUAGameEngine' (a nil value)


when I attempt to execute the following function in my script
-----------------------------------------
-- HandlerBack to Game
-----------------------------------------
function handleReturnToGame(args)

local winMgr = CEGUI.WindowManager:getSingleton()
local temp = winMgr:getWindow("Menu");
temp:setVisible(false);
local modelman = LUAGameEngine:getInstance()

end


Here is the .pkg
class GameEngine
{
public:

// Returns the currently active state
GameState* getCurrentState(){}

// <SINGLETON>
static GameEngine& getInstance();

};

and here is the .cpp obtained? [i added #include "GameEngine.h",
#include "GameState.h" and using namespace Global;]
/*
** Lua binding: LUAGameEngine
** Generated automatically by tolua++-1.0.7 on 06/07/06 17:03:28.
*/

#ifndef __cplusplus
#include "stdlib.h"
#endif
#include "string.h"

#include "tolua++.h"
#include "GameEngine.h"
#include "GameState.h"

using namespace Global;
/* Exported function */
TOLUA_API int? tolua_LUAGameEngine_open (lua_State* tolua_S);


/* function to register type */
static void tolua_reg_types (lua_State* tolua_S)
{
tolua_usertype(tolua_S,"GameState");
tolua_usertype(tolua_S,"GameEngine");
}

/* method: getCurrentState of class? GameEngine */
#ifndef TOLUA_DISABLE_tolua_LUAGameEngine_GameEngine_getCurrentState00
static int tolua_LUAGameEngine_GameEngine_getCurrentState00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
tolua_Error tolua_err;
if (
? ? ?!tolua_isusertype(tolua_S,1,"GameEngine",0,&tolua_err) ||
? ? ?!tolua_isnoobj(tolua_S,2,&tolua_err)
)
? goto tolua_lerror;
else
#endif
{
? GameEngine* self = (GameEngine*)? tolua_tousertype(tolua_S,1,0);
#ifndef TOLUA_RELEASE
? if (!self) tolua_error(tolua_S,"invalid 'self' in function 'getCurrentState'",NULL);
#endif
? {
? ?GameState* tolua_ret = (GameState*)? self->getCurrentState();
? ?tolua_pushusertype(tolua_S,(void*)tolua_ret,"GameState");
? }
}
return 1;
#ifndef TOLUA_RELEASE
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'getCurrentState'.",&tolua_err);
return 0;
#endif
}
#endif //#ifndef TOLUA_DISABLE

/* method: getInstance of class? GameEngine */
#ifndef TOLUA_DISABLE_tolua_LUAGameEngine_GameEngine_getInstance00
static int tolua_LUAGameEngine_GameEngine_getInstance00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
tolua_Error tolua_err;
if (
? ? ?!tolua_isusertable(tolua_S,1,"GameEngine",0,&tolua_err) ||
? ? ?!tolua_isnoobj(tolua_S,2,&tolua_err)
)
? goto tolua_lerror;
else
#endif
{
? {
? ?GameEngine& tolua_ret = (GameEngine&)? GameEngine::getInstance();
? ?tolua_pushusertype(tolua_S,(void*)&tolua_ret,"GameEngine");
? }
}
return 1;
#ifndef TOLUA_RELEASE
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'getInstance'.",&tolua_err);
return 0;
#endif
}
#endif //#ifndef TOLUA_DISABLE

/* Open function */
TOLUA_API int tolua_LUAGameEngine_open (lua_State* tolua_S)
{
tolua_open(tolua_S);
tolua_reg_types(tolua_S);
tolua_module(tolua_S,NULL,0);
tolua_beginmodule(tolua_S,NULL);
? tolua_cclass(tolua_S,"GameEngine","GameEngine","",NULL);
? tolua_beginmodule(tolua_S,"GameEngine");
? ?tolua_function(tolua_S,"getCurrentState",tolua_LUAGameEngine_GameEngine_getCurrentState00);
? ?tolua_function(tolua_S,"getInstance",tolua_LUAGameEngine_GameEngine_getInstance00);
? tolua_endmodule(tolua_S);
tolua_endmodule(tolua_S);
return 1;
}


here is the error ad the function again
07/06/2006 17:12:14 (Error) Exception: (LuaScriptModule) Unable to execute scripted event handler: handleReturnToGame
[string "../datafiles/lua_scripts/demo8.lua"]:180: attempt to index global `LUAGameEngine' (a nil value)


when I attempt to execute the following function in my script
-----------------------------------------
-- HandlerBack to Game
-----------------------------------------
function handleReturnToGame(args)

local winMgr = CEGUI.WindowManager:getSingleton()
local temp = winMgr:getWindow("Menu");
temp:setVisible(false);
local modelman = LUAGameEngine:getInstance()

end

I am a beginner so I might be missing something obvious but...anyone has a clue why it is acting like this?