Index: /code/branches/ScriptableController_HS17/src/orxonox/Level.cc
===================================================================
--- /code/branches/ScriptableController_HS17/src/orxonox/Level.cc	(revision 11548)
+++ /code/branches/ScriptableController_HS17/src/orxonox/Level.cc	(revision 11549)
@@ -87,5 +87,5 @@
         if(this->level_script_ != "")
         {
-            this->controller_.reset(new ScriptableController());
+            this->controller_ = new ScriptableController(this->getContext());
             this->controller_->runScript(this->level_script_);
         }
@@ -177,6 +177,6 @@
     {
         this->objects_.push_back(object);
-        if(this->controller_.get() != nullptr)
-            object->registerToScriptableController(this->controller_.get());
+        if(this->controller_ != nullptr)
+            object->registerToScriptableController(this->controller_);
     }
 
Index: /code/branches/ScriptableController_HS17/src/orxonox/Level.h
===================================================================
--- /code/branches/ScriptableController_HS17/src/orxonox/Level.h	(revision 11548)
+++ /code/branches/ScriptableController_HS17/src/orxonox/Level.h	(revision 11549)
@@ -92,6 +92,6 @@
             std::map<std::string,MeshLodInformation*>  lodInformation_;
 
-            std::unique_ptr<ScriptableController> controller_;
-            std::string                           level_script_;
+            ScriptableController* controller_;
+            std::string           level_script_;
     };
 }
Index: /code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/luatb.h
===================================================================
--- /code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/luatb.h	(revision 11548)
+++ /code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/luatb.h	(revision 11549)
@@ -15,5 +15,5 @@
     // visible, you should call it like this:
     //
-    // LuaWrapper<decltype(foo)>::registerFunction<foo>( ... );
+    // LuaTB<decltype(foo)>::registerFunction<foo>( ... );
     template<FunctionType func>
     static void registerFunction(ThisType *_this, lua_State *lua, std::string name);
Index: /code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/luatb.ipp
===================================================================
--- /code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/luatb.ipp	(revision 11548)
+++ /code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/luatb.ipp	(revision 11549)
@@ -3,5 +3,4 @@
 #include <iostream>
 #include <type_traits>
-#include "scriptable_controller_api.h"
 #include "luatb_typed_stack.h"
 
@@ -20,5 +19,5 @@
     {
         // Store the 'this' pointer of the caller in the extraspace
-//        *static_cast<ThisType**>(lua_getextraspace(lua)) = _this;
+        LuaTB<ThisType, Ret (ThisType::*)(Args...)>::stateToClassMap[lua] = _this;
 
         // Make a function visible to lua that will call 'func' with the correct
@@ -28,4 +27,6 @@
 
 private:
+    static std::map<lua_State*, ThisType*> stateToClassMap;
+
     // Represents a function that can made visible to lua with the correct
     // signature. It will call the corresponding C++ function with the
@@ -34,24 +35,26 @@
     static int toLuaSignature(lua_State *lua)
     {
-        // The number of arguments is the first item on the stack
-        int argc = lua_tointeger(lua, lua_gettop(lua));
-        lua_pop(lua, 1);
+        // The index of the topmost item on the stack equals the size of
+        // the stack, which also equals the number of arguments
+        int argc = lua_gettop(lua);
 
         // Check if the number of arguments match
         if(argc != sizeof...(Args))
         {
-            std::cerr << "ERROR: LuaTB: Lua script called a function with wrong number of arguments" << std::endl;
+            std::cerr << "ERROR: LuaTB: Lua script called a function with wrong number of arguments (" << argc << " given, " << sizeof...(Args) << " expected)" << std::endl;
             return LUA_ERRSYNTAX;
         }
-
-        // Retrieve 'this' pointer of caller
-//        ThisType *_this = *static_cast<ThisType**>(lua_getextraspace(lua));
-ThisType *_this = orxonox::ScriptableControllerAPI::this_;
+        orxonox::orxout(orxonox::user_warning) << "what" << std::endl;
 
         // Call getArgument for every argument seperately to convert each argument
         // to the correct type and call the function afterwards
-        ((*_this).*func)( (LuaTBTypedStack::getArgument<Args>(lua))... );
+        ((*LuaTB<ThisType, Ret (ThisType::*)(Args...)>::stateToClassMap[lua]).*func)( (LuaTBTypedStack::getArgument<Args>(lua))... );
 
         return 0;
     }
 };
+
+// This needs to be here and not in a source file, because the compiler can't know
+// the template types if it's in a separate module.
+template<typename ThisType, typename Ret, typename... Args>
+std::map<lua_State*, ThisType*> LuaTB<ThisType, Ret (ThisType::*)(Args...)>::stateToClassMap = std::map<lua_State*, ThisType*>();
Index: /code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/luatb_typed_stack.cc
===================================================================
--- /code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/luatb_typed_stack.cc	(revision 11548)
+++ /code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/luatb_typed_stack.cc	(revision 11549)
@@ -1,4 +1,5 @@
 
 #include "luatb_typed_stack.h"
+#include "luatb.h"
 #include <string>
 
Index: /code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/luatb_typed_stack.h
===================================================================
--- /code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/luatb_typed_stack.h	(revision 11548)
+++ /code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/luatb_typed_stack.h	(revision 11549)
@@ -6,4 +6,5 @@
 #include <lua.hpp>
 #include "is_callable.h"
+#include "core/CoreIncludes.h"
 
 // We need a separate class for this because we need to specialize the functions
@@ -54,4 +55,5 @@
         static std::function<Ret (Args...)> value(lua_State *lua, int ref)
         {
+            orxonox::orxout(orxonox::user_warning) << "Ref1: " << ref << std::endl;
             return [lua, ref](Args... args){return callLuaFunction<Ret, Args...>(lua, ref, args...);};
         }
@@ -65,5 +67,12 @@
         // We pass one extra argument in case the function has no arguments at all
         pushArgumentsToLuaStack<Args...>(lua, args..., false);
-        lua_pcall(lua, 1, sizeof...(args), 0);
+        int r = lua_pcall(lua, 1, sizeof...(args), 0);
+
+        orxonox::orxout(orxonox::user_warning) << "Ref2: " << ref << std::endl;
+if(r != 0){
+        const char* message = lua_tostring(lua, -1);
+        std::cout << message << std::endl;
+        lua_pop(lua, 1);
+}
 
         // TODO
Index: /code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller.cc
===================================================================
--- /code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller.cc	(revision 11548)
+++ /code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller.cc	(revision 11549)
@@ -6,5 +6,13 @@
 {
 
+RegisterUnloadableClass(ScriptableController);
+
 // Used https://csl.name/post/lua-and-cpp/ as a reference
+ScriptableController::ScriptableController(Context* context)
+    : BaseObject(context)
+{
+    RegisterObject(ScriptableController);
+}
+
 int ScriptableController::runScript(const std::string &file_path)
 {
@@ -71,4 +79,33 @@
 }
 
+void ScriptableController::registerTimeout(std::function<void ()> callback, double timeout)
+{
+    this->timeouts.push_back(std::make_pair(callback, static_cast<float>(timeout)));
+    orxout(user_warning) << "Calling should work..." << std::endl;
+    callback();
+}
+
+void ScriptableController::tick(float dt)
+{
+    auto timeout = this->timeouts.begin();
+
+    while(timeout != this->timeouts.end())
+    {
+        timeout->second -= dt;
+        if(timeout->second <= 0)
+        {
+            orxout(user_warning) << "Calling..." << std::endl;
+            timeout->first();
+            timeout = this->timeouts.erase(timeout);
+        }
+        else
+        {
+            timeout++;
+        }
+    }
+
+    Tickable::tick(dt);
+}
+
 void ScriptableController::printLuaError(lua_State *lua)
 {
Index: /code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller.h
===================================================================
--- /code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller.h	(revision 11548)
+++ /code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller.h	(revision 11549)
@@ -17,7 +17,9 @@
 {
 
-class ScriptableController
+class ScriptableController : public BaseObject, public Tickable
 {
 public:
+    explicit ScriptableController(Context *context);
+
     int runScript(const std::string &file_path);
 
@@ -28,4 +30,8 @@
     ControllableEntity *getControllableEntityByID(int id) const;
 
+    void registerTimeout(std::function<void (void)> callback, double timeout);
+
+    virtual void tick(float dt) override;
+
 private:
     std::list<std::unique_ptr<ScriptableControllerAPI> > apis_;
@@ -33,4 +39,5 @@
     std::map<int, WorldEntity*> worldEntities_;
     std::map<int, ControllableEntity*> controllabelEntities_;
+    std::list<std::pair<std::function<void (void)>, float> > timeouts;
 
     void printLuaError(lua_State *lua);
Index: /code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.cc
===================================================================
--- /code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.cc	(revision 11548)
+++ /code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.cc	(revision 11549)
@@ -9,14 +9,10 @@
 {
 
-ScriptableControllerAPI *ScriptableControllerAPI::this_;
-
 ScriptableControllerAPI::ScriptableControllerAPI(lua_State *lua, ScriptableController *controller)
 {
     this->lua_ = lua;
     this->controller_ = controller;
-    ScriptableControllerAPI::this_ = this;
 
     // Haven't found a shorter way yet to write that...
-    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::testOutput)>::registerFunction<&ScriptableControllerAPI::testOutput>(this, lua, "testOutput");
     LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAfterTimeout)>::registerFunction<&ScriptableControllerAPI::registerAfterTimeout>(this, lua, "registerAfterTimeout");
     LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtNearObject)>::registerFunction<&ScriptableControllerAPI::registerAtNearObject>(this, lua, "registerAtNearObject");
@@ -32,17 +28,12 @@
 }
 
-void ScriptableControllerAPI::testOutput()
+void ScriptableControllerAPI::registerAfterTimeout(std::function<void (void)> callback, double timeout)
 {
-    orxout(user_info) << "Wheee!!!" << std::endl;
-}
-
-void ScriptableControllerAPI::registerAfterTimeout(std::function<void (void)> callback, int timeout_ms)
-{
-
+    this->controller_->registerTimeout(callback, timeout);
 }
 
 int ScriptableControllerAPI::registerAtNearObject(std::function<void (int, int)> callback, int obj1, int obj2, double distance)
 {
-
+    orxout(user_warning) << "Working!" << std::endl;
 }
 
Index: /code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.h
===================================================================
--- /code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.h	(revision 11548)
+++ /code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.h	(revision 11549)
@@ -20,7 +20,7 @@
     ~ScriptableControllerAPI();
 
-    void testOutput(void);
+    void testOutput(std::function<void(std::string)> callback);
 
-    void registerAfterTimeout(std::function<void (void)> callback, int timeout_ms);
+    void registerAfterTimeout(std::function<void (void)> callback, double timeout);
     int registerAtNearObject(std::function<void(int, int)> callback, int obj1, int obj2, double distance);
     int registerAtAreaEnter(std::function<void (int)> callback, int obj, int x, int y, int z, int dx, int dy, int dz);
@@ -33,6 +33,4 @@
     int setObjectPosition(int obj, double x, double y, double z);
 
-    static ScriptableControllerAPI *this_;
-
 private:
     lua_State *lua_;
