From 6e5697a125973bca83d368364e088777d23f824a Mon Sep 17 00:00:00 2001 From: Alexander Pickering Date: Wed, 31 Oct 2018 12:40:59 -0400 Subject: Added error handeling function Added an error handeling function to avoid crashes on errors in callbacked handeled code (ex, error in button press) --- src/client/callbackhandeler.cpp | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) (limited to 'src/client/callbackhandeler.cpp') diff --git a/src/client/callbackhandeler.cpp b/src/client/callbackhandeler.cpp index 469084d..e24763d 100644 --- a/src/client/callbackhandeler.cpp +++ b/src/client/callbackhandeler.cpp @@ -8,6 +8,7 @@ extern "C" { } #include #include +#include #include "callbackhandeler.hpp" @@ -36,16 +37,20 @@ GlobalEventReceiver::GlobalEventReceiver(IrrlichtDevice* d){ } void callMouse(lua_State* L, const char* funcname, double x, double y, double event){ - lua_getglobal(L,"GAME"); - lua_getfield(L,-1,funcname); + lua_getglobal(L,"GAME");//GAME + pusherrorfunc(L);//{GAME},errfun + lua_getfield(L,-2,funcname);//{GAME},errfunc,funcname? if(!lua_isnil(L,-1)){ - lua_pushnumber(L,x); - lua_pushnumber(L,y); - lua_pushnumber(L,event); - lua_call(L,3,0); - lua_pop(L,1); - }else{ + lua_pushnumber(L,x);//{GAME},errfunc,func,x + lua_pushnumber(L,y);//{GAME},errfunc,func,x,y + lua_pushnumber(L,event);//{GAME},errfunc,func,x,y,event + int err = lua_pcall(L,3,0,-4);//{GAME},errfunc + if(err){ + printf("Failed to call GAME.%s\n",funcname); + } lua_pop(L,2); + }else{ + lua_pop(L,3); } } @@ -177,12 +182,14 @@ bool GlobalEventReceiver::OnEvent(const SEvent& e){ lua_getglobal(L,"GAME");//{} lua_getfield(L,-1,"onKeyDown");//{},()|nil if(!lua_isnil(L,-1)){ + pusherrorfunc(L);//GAME,GAME.onKeyDown(),errfunc + lua_pushvalue(L,-2);//GAME,GAME.onKeyDown(),errfunc,onKeyDown() lua_pushnumber(L,se.Key); lua_pushboolean(L,se.PressedDown); lua_pushboolean(L,se.Control); lua_pushboolean(L,se.Shift); - lua_call(L,4,0); - lua_pop(L,1); + lua_pcall(L,4,0,-5);//GAME,GAME.onKeyDown() + lua_pop(L,2); }else{ lua_pop(L,2); } -- cgit v1.2.3-70-g09d2