aboutsummaryrefslogtreecommitdiff
path: root/src/client
diff options
context:
space:
mode:
authorAlexander Pickering <alex@cogarr.net>2018-10-31 12:40:59 -0400
committerAlexander Pickering <alex@cogarr.net>2018-10-31 12:40:59 -0400
commit6e5697a125973bca83d368364e088777d23f824a (patch)
tree3cb170028bb5af3bf58621db671d2715be518b8b /src/client
parent6df9cb0de3e457788808b485b8b34bd8f0d6e42b (diff)
downloadbrokengine-6e5697a125973bca83d368364e088777d23f824a.tar.gz
brokengine-6e5697a125973bca83d368364e088777d23f824a.tar.bz2
brokengine-6e5697a125973bca83d368364e088777d23f824a.zip
Added error handeling function
Added an error handeling function to avoid crashes on errors in callbacked handeled code (ex, error in button press)
Diffstat (limited to 'src/client')
-rw-r--r--src/client/callbackhandeler.cpp27
-rw-r--r--src/client/callbackhandeler.hpp3
2 files changed, 20 insertions, 10 deletions
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 <irrlicht.h>
#include <map>
+#include <shared/lua_api/common.hpp>
#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);
}
diff --git a/src/client/callbackhandeler.hpp b/src/client/callbackhandeler.hpp
index 3d4894f..0728b29 100644
--- a/src/client/callbackhandeler.hpp
+++ b/src/client/callbackhandeler.hpp
@@ -20,4 +20,7 @@ public:
GlobalEventReceiver(IrrlichtDevice* d);
bool OnEvent(const irr::SEvent& e);
};
+
+int errfunc(lua_State *L);
+void pusherrorfunc(lua_State *L);
//GlobalEventReceiver::GlobalEventReceiver(IrrlichtDevice* d);