Index: code/branches/unity_build/src/CMakeLists.txt
===================================================================
--- code/branches/unity_build/src/CMakeLists.txt	(revision 8687)
+++ code/branches/unity_build/src/CMakeLists.txt	(revision 8688)
@@ -145,10 +145,7 @@
 ################### Tolua Bind ##################
 
-# Create directory because the tolua application doesn't work otherwise
-IF(NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}/toluabind/${CMAKE_CFG_INTDIR})
-  FILE(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/toluabind/${CMAKE_CFG_INTDIR})
-ENDIF()
-
-INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR}/toluabind/${CMAKE_CFG_INTDIR})
+# Add hook script to the lua code that generates the bindings
+SET(TOLUA_PARSER_HOOK_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/libraries/core/ToluaInterfaceHook.lua)
+SET(TOLUA_PARSER_DEPENDENCIES ${TOLUA_PARSER_DEPENDENCIES} ${TOLUA_PARSER_HOOK_SCRIPT})
 
 ################ Sub Directories ################
Index: code/branches/unity_build/src/external/tolua/lua/basic.lua
===================================================================
--- code/branches/unity_build/src/external/tolua/lua/basic.lua	(revision 8687)
+++ code/branches/unity_build/src/external/tolua/lua/basic.lua	(revision 8688)
@@ -354,3 +354,5 @@
 end
 
-
+-- called after all the required C++ includes have been written
+function post_include_hook(package_name)
+end
Index: code/branches/unity_build/src/external/tolua/lua/package.lua
===================================================================
--- code/branches/unity_build/src/external/tolua/lua/package.lua	(revision 8687)
+++ code/branches/unity_build/src/external/tolua/lua/package.lua	(revision 8688)
@@ -135,4 +135,6 @@
         i = i+1
     end
+
+    post_include_hook(self.name)
 
     output('\n')
Index: code/branches/unity_build/src/libraries/core/LuaState.cc
===================================================================
--- code/branches/unity_build/src/libraries/core/LuaState.cc	(revision 8687)
+++ code/branches/unity_build/src/libraries/core/LuaState.cc	(revision 8688)
@@ -40,16 +40,9 @@
 #include "util/Exception.h"
 #include "Resource.h"
-#include "ToluaBindCore.h"
 #include "command/IOConsole.h"
 
 namespace orxonox
 {
-    LuaState::ToluaInterfaceMap LuaState::toluaInterfaces_s;
-    std::vector<LuaState*> LuaState::instances_s;
-
     const std::string LuaState::ERROR_HANDLER_NAME = "errorHandler";
-
-    // Do this after declaring toluaInterfaces_s and instances_s to avoid larger problems
-    DeclareToluaInterface(Core);
 
     LuaState::LuaState()
@@ -277,7 +270,19 @@
     }
 
+    /*static*/ LuaState::ToluaInterfaceMap& LuaState::getToluaInterfaces()
+    {
+        static ToluaInterfaceMap p;
+        return p;
+    }
+
+    /*static*/ std::vector<LuaState*>& LuaState::getInstances()
+    {
+        static std::vector<LuaState*> p;
+        return p;
+    }
+
     /*static*/ bool LuaState::addToluaInterface(int (*function)(lua_State*), const std::string& name)
     {
-        for (ToluaInterfaceMap::const_iterator it = toluaInterfaces_s.begin(); it != toluaInterfaces_s.end(); ++it)
+        for (ToluaInterfaceMap::const_iterator it = getToluaInterfaces().begin(); it != getToluaInterfaces().end(); ++it)
         {
             if (it->first == name || it->second == function)
@@ -287,8 +292,8 @@
             }
         }
-        toluaInterfaces_s[name] = function;
+        getToluaInterfaces()[name] = function;
 
         // Open interface in all LuaStates
-        for (std::vector<LuaState*>::const_iterator it = instances_s.begin(); it != instances_s.end(); ++it)
+        for (std::vector<LuaState*>::const_iterator it = getInstances().begin(); it != getInstances().end(); ++it)
             (*function)((*it)->luaState_);
 
@@ -299,6 +304,6 @@
     /*static*/ bool LuaState::removeToluaInterface(const std::string& name)
     {
-        ToluaInterfaceMap::iterator it = toluaInterfaces_s.find(name);
-        if (it == toluaInterfaces_s.end())
+        ToluaInterfaceMap::iterator it = getToluaInterfaces().find(name);
+        if (it == getToluaInterfaces().end())
         {
             COUT(2) << "Warning: Cannot remove Tolua interface '" << name << "': Not found" << std::endl;
@@ -307,5 +312,5 @@
 
         // Close interface in all LuaStates
-        for (std::vector<LuaState*>::const_iterator itState = instances_s.begin(); itState != instances_s.end(); ++itState)
+        for (std::vector<LuaState*>::const_iterator itState = getInstances().begin(); itState != getInstances().end(); ++itState)
         {
             lua_pushnil((*itState)->luaState_);
@@ -314,5 +319,5 @@
 
         // Remove entry
-        toluaInterfaces_s.erase(it);
+        getToluaInterfaces().erase(it);
 
         // Return dummy bool
@@ -322,5 +327,5 @@
     /*static*/ void LuaState::openToluaInterfaces(lua_State* state)
     {
-        for (ToluaInterfaceMap::const_iterator it = toluaInterfaces_s.begin(); it != toluaInterfaces_s.end(); ++it)
+        for (ToluaInterfaceMap::const_iterator it = getToluaInterfaces().begin(); it != getToluaInterfaces().end(); ++it)
             (*it->second)(state);
     }
@@ -328,5 +333,5 @@
     /*static*/ void LuaState::closeToluaInterfaces(lua_State* state)
     {
-        for (ToluaInterfaceMap::const_iterator it = toluaInterfaces_s.begin(); it != toluaInterfaces_s.end(); ++it)
+        for (ToluaInterfaceMap::const_iterator it = getToluaInterfaces().begin(); it != getToluaInterfaces().end(); ++it)
         {
             lua_pushnil(state);
Index: code/branches/unity_build/src/libraries/core/LuaState.h
===================================================================
--- code/branches/unity_build/src/libraries/core/LuaState.h	(revision 8687)
+++ code/branches/unity_build/src/libraries/core/LuaState.h	(revision 8688)
@@ -48,6 +48,4 @@
 #include <vector>
 #include <boost/shared_ptr.hpp>
-
-#include "ToluaInterface.h"
 
 namespace orxonox // tolua_export
@@ -120,7 +118,25 @@
 
         typedef std::map<std::string, int (*)(lua_State *L)> ToluaInterfaceMap;
-        static ToluaInterfaceMap toluaInterfaces_s;
-        static std::vector<LuaState*> instances_s;
+        static ToluaInterfaceMap& getToluaInterfaces();
+        static std::vector<LuaState*>& getInstances();
     }; // tolua_export
+
+
+    //! Helper class that registers/unregisters tolua bindings
+    class ToluaBindingsHelper
+    {
+    public:
+        ToluaBindingsHelper(int (*function)(lua_State*), const std::string& libraryName)
+            : libraryName_(libraryName)
+        {
+            LuaState::addToluaInterface(function, libraryName_);
+        }
+        ~ToluaBindingsHelper()
+        {
+            LuaState::removeToluaInterface(libraryName_);
+        }
+    private:
+        std::string libraryName_;
+    };
 } // tolua_export
 
Index: code/branches/unity_build/src/libraries/core/ToluaInterface.h
===================================================================
--- code/branches/unity_build/src/libraries/core/ToluaInterface.h	(revision 8687)
+++ 	(revision )
@@ -1,48 +1,0 @@
-/*
- *   ORXONOX - the hottest 3D action shooter ever to exist
- *                    > www.orxonox.net <
- *
- *
- *   License notice:
- *
- *   This program is free software; you can redistribute it and/or
- *   modify it under the terms of the GNU General Public License
- *   as published by the Free Software Foundation; either version 2
- *   of the License, or (at your option) any later version.
- *
- *   This program is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *   GNU General Public License for more details.
- *
- *   You should have received a copy of the GNU General Public License
- *   along with this program; if not, write to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- *
- *   Author:
- *      Reto Grieder
- *   Co-authors:
- *      ...
- *
- */
-
-/**
-@file
-@ingroup Lua
-@brief
-    This is required because tolua would parse this macro in LuaState.h and fail
-*/
-
-#ifndef _ToluaInterface_H__
-#define _ToluaInterface_H__
-
-#include "CorePrereqs.h"
-#include <boost/preprocessor/cat.hpp>
-#include <loki/ScopeGuard.h>
-
-// Macro for declaring a tolua interface of a library/module
-#define DeclareToluaInterface(libraryName) \
-    static bool BOOST_PP_CAT(bDummy##libraryName, __UNIQUE_NUMBER__) = orxonox::LuaState::addToluaInterface(&tolua_##libraryName##_open, #libraryName); \
-    static Loki::ScopeGuardImpl1<bool (*)(const std::string&), std::string> BOOST_PP_CAT(dummy##libraryName, __UNIQUE_NUMBER__)(&orxonox::LuaState::removeToluaInterface, #libraryName)
-
-#endif /* _ToluaInterface_H__ */
Index: code/branches/unity_build/src/libraries/core/ToluaInterfaceHook.lua
===================================================================
--- code/branches/unity_build/src/libraries/core/ToluaInterfaceHook.lua	(revision 8688)
+++ code/branches/unity_build/src/libraries/core/ToluaInterfaceHook.lua	(revision 8688)
@@ -0,0 +1,30 @@
+--
+--             ORXONOX - the hottest 3D action shooter ever to exist
+--                             > www.orxonox.net <
+--
+--        This program is free software; you can redistribute it and/or
+--         modify it under the terms of the GNU General Public License
+--        as published by the Free Software Foundation; either version 2
+--            of the License, or (at your option) any later version.
+--
+--       This program is distributed in the hope that it will be useful,
+--        but WITHOUT ANY WARRANTY; without even the implied warranty of
+--        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+--                 GNU General Public License for more details.
+--
+--   You should have received a copy of the GNU General Public License along
+--      with this program; if not, write to the Free Software Foundation,
+--     Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+--
+--
+--  Author:
+--    Reto Grieder
+--
+
+-- Writes the static initialiser that registers the package open() function
+function post_include_hook(package_name)
+    output('\n')
+    output('#include "core/LuaState.h"\n')
+    output('int tolua_'..package_name..'_open(lua_State* tolua_S);\n')
+    output('static orxonox::ToluaBindingsHelper initialiser(&tolua_'..package_name..'_open, "'..package_name..'");')
+end
Index: code/branches/unity_build/src/modules/notifications/NotificationManager.cc
===================================================================
--- code/branches/unity_build/src/modules/notifications/NotificationManager.cc	(revision 8687)
+++ code/branches/unity_build/src/modules/notifications/NotificationManager.cc	(revision 8688)
@@ -47,6 +47,4 @@
 #include "NotificationQueue.h"
 
-#include "ToluaBindNotifications.h"
-
 namespace orxonox
 {
@@ -54,7 +52,4 @@
     const std::string NotificationManager::ALL("all");
     const std::string NotificationManager::NONE("none");
-
-    // Register tolua_open function when loading the library.
-    DeclareToluaInterface(Notifications);
 
     ManageScopedSingleton(NotificationManager, ScopeID::Root, false);
Index: code/branches/unity_build/src/modules/pickup/PickupManager.cc
===================================================================
--- code/branches/unity_build/src/modules/pickup/PickupManager.cc	(revision 8687)
+++ code/branches/unity_build/src/modules/pickup/PickupManager.cc	(revision 8688)
@@ -49,11 +49,6 @@
 #include "PickupRepresentation.h"
 
-#include "ToluaBindPickup.h"
-
 namespace orxonox
 {
-    // Register tolua_open function when loading the library
-    DeclareToluaInterface(Pickup);
-
     ManageScopedSingleton(PickupManager, ScopeID::Root, false);
 
Index: code/branches/unity_build/src/modules/questsystem/QuestManager.cc
===================================================================
--- code/branches/unity_build/src/modules/questsystem/QuestManager.cc	(revision 8687)
+++ code/branches/unity_build/src/modules/questsystem/QuestManager.cc	(revision 8688)
@@ -47,11 +47,6 @@
 #include "QuestItem.h"
 
-#include "ToluaBindQuestsystem.h"
-
 namespace orxonox
 {
-    // Register tolua_open function when loading the library
-    DeclareToluaInterface(Questsystem);
-
     ManageScopedSingleton(QuestManager, ScopeID::Root, false);
 
Index: code/branches/unity_build/src/orxonox/Main.cc
===================================================================
--- code/branches/unity_build/src/orxonox/Main.cc	(revision 8687)
+++ code/branches/unity_build/src/orxonox/Main.cc	(revision 8688)
@@ -39,9 +39,4 @@
 #include "core/Game.h"
 #include "core/LuaState.h"
-#include "ToluaBindOrxonox.h"
-#include "ToluaBindNetwork.h"
-
-DeclareToluaInterface(Orxonox);
-DeclareToluaInterface(Network);
 
 namespace orxonox
