Index: code/branches/gamestates2/src/libraries/core/GUIManager.cc
===================================================================
--- code/branches/gamestates2/src/libraries/core/GUIManager.cc	(revision 6735)
+++ code/branches/gamestates2/src/libraries/core/GUIManager.cc	(revision 6736)
@@ -30,4 +30,5 @@
 #include "GUIManager.h"
 
+#include <boost/bind.hpp>
 #include <memory>
 extern "C" {
@@ -175,5 +176,5 @@
     {
         assert(guiSystem_);
-        guiSystem_->injectTimePulse(time.getDeltaTime());
+        this->protectedCall(boost::bind(&CEGUI::System::injectTimePulse, _1, time.getDeltaTime()));
     }
 
@@ -298,10 +299,11 @@
     void GUIManager::keyPressed(const KeyEvent& evt)
     {
-        guiSystem_->injectKeyDown(evt.getKeyCode());
-        guiSystem_->injectChar(evt.getText());
-    }
+        this->protectedCall(boost::bind(&CEGUI::System::injectKeyDown, _1, evt.getKeyCode()));
+        this->protectedCall(boost::bind(&CEGUI::System::injectChar, _1, evt.getText()));
+    }
+
     void GUIManager::keyReleased(const KeyEvent& evt)
     {
-        guiSystem_->injectKeyUp(evt.getKeyCode());
+        this->protectedCall(boost::bind(&CEGUI::System::injectKeyUp, _1, evt.getKeyCode()));
     }
 
@@ -317,13 +319,5 @@
     void GUIManager::buttonPressed(MouseButtonCode::ByEnum id)
     {
-        try
-        {
-            guiSystem_->injectMouseButtonDown(convertButton(id));
-        }
-        catch (CEGUI::ScriptException& ex)
-        {
-            // We simply ignore the exception and proceed
-            COUT(1) << ex.getMessage() << std::endl;
-        }
+        this->protectedCall(boost::bind(&CEGUI::System::injectMouseButtonDown, _1, convertButton(id)));
     }
 
@@ -339,22 +333,15 @@
     void GUIManager::buttonReleased(MouseButtonCode::ByEnum id)
     {
-        try
-        {
-            guiSystem_->injectMouseButtonUp(convertButton(id));
-        }
-        catch (CEGUI::ScriptException& ex)
-        {
-            // We simply ignore the exception and proceed
-            COUT(1) << ex.getMessage() << std::endl;
-        }
+        this->protectedCall(boost::bind(&CEGUI::System::injectMouseButtonUp, _1, convertButton(id)));
     }
 
     void GUIManager::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize)
     {
-        guiSystem_->injectMousePosition(static_cast<float>(abs.x), static_cast<float>(abs.y));
-    }
+        this->protectedCall(boost::bind(&CEGUI::System::injectMousePosition, _1, (float)abs.x, (float)abs.y));
+    }
+
     void GUIManager::mouseScrolled(int abs, int rel)
     {
-        guiSystem_->injectMouseWheelChange(static_cast<float>(rel));
+        this->protectedCall(boost::bind(&CEGUI::System::injectMouseWheelChange, _1, (float)rel));
     }
 
@@ -393,4 +380,34 @@
     }
 
+    /** Executes a CEGUI function normally, but catches CEGUI::ScriptException.
+        When a ScriptException occurs, the error message will be displayed and
+        the program carries on.
+    @remarks
+        The exception behaviour may pose problems if the code is not written
+        exception-safe (and you can forget about that in Lua). The program might
+        be left in an undefined state. But otherwise one script error would
+        terminate the whole program...
+    @note
+        Your life gets easier if you use boost::bind to create the object/function.
+    @param function
+        Any callable object/function that takes this->guiSystem_ as its only parameter.
+    @return
+        True if input was handled, false otherwise. A caught exception yields true.
+    */
+    template <typename FunctionType>
+    bool GUIManager::protectedCall(FunctionType function)
+    {
+        try
+        {
+            return function(this->guiSystem_);
+        }
+        catch (CEGUI::ScriptException& ex)
+        {
+            // Display the error and proceed. See @remarks why this can be dangerous.
+            COUT(1) << ex.getMessage() << std::endl;
+            return true;
+        }
+    }
+
     void GUIManager::subscribeEventHelper(CEGUI::Window* window, const std::string& event, const std::string& function)
     {
Index: code/branches/gamestates2/src/libraries/core/GUIManager.h
===================================================================
--- code/branches/gamestates2/src/libraries/core/GUIManager.h	(revision 6735)
+++ code/branches/gamestates2/src/libraries/core/GUIManager.h	(revision 6736)
@@ -101,4 +101,7 @@
         void executeCode(const std::string& str);
 
+        template <typename FunctionType>
+        bool protectedCall(FunctionType function);
+
         // keyHandler functions
         void keyPressed (const KeyEvent& evt);
