Index: code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.cc
===================================================================
--- code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.cc	(revision 11638)
+++ code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.cc	(revision 11662)
@@ -4,4 +4,7 @@
 #include "scriptable_controller.h"
 #include "tools/Timer.h"
+#include "worldentities/pawns/Pawn.h"
+#include "infos/Bot.h"
+#include "worldentities/pawns/ModularSpaceShip.h"
 
 namespace orxonox
@@ -32,4 +35,5 @@
 
     LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::killPawn)>::registerFunction<&ScriptableControllerAPI::killPawn>(this, lua, "killPawn");
+    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::spawn)>::registerFunction<&ScriptableControllerAPI::spawn>(this, lua, "spawn");
 
     this->periodicTimer.setTimer(ScriptableControllerAPI::periodic_interval, true, createExecutor(createFunctor(&ScriptableControllerAPI::periodic, this)), false);
@@ -103,4 +107,54 @@
 }
 
+void ScriptableControllerAPI::spawn(std::string type, std::string id)
+{
+    if(this->controller_->getWorldEntityByID(id) != nullptr)
+    {
+        orxout(user_warning) << "Script tried to spawn an object, but an object with the given ID exists already" << std::endl;
+        return;
+    }
+
+    Identifier *identifier = ClassByString(type);
+    if(!identifier)
+    {
+        orxout(user_error) << "Script tried to spawn unknown object" << std::endl;
+        return;
+    }
+
+    if(!identifier->isLoadable())
+    {
+        orxout(user_error) << "Script tried to spawn unloadable object" << std::endl;
+        return;
+    }
+
+    WorldEntity *entity;
+    Identifiable *obj = identifier->fabricate(this->controller_->getWorldEntityByID("Player")->getContext());
+
+    if(obj->isA(ClassIdentifier<WorldEntity>::getIdentifier()))
+    {
+        entity = orxonox_cast<WorldEntity*>(obj);
+    }
+    else if(obj->isA(ClassIdentifier<PlayerInfo>::getIdentifier()))
+    {
+        // TODO This does not work yet because somehow the controllable entity is not set
+        // yet at this stage.
+//        entity = orxonox_cast<PlayerInfo*>(obj)->getControllableEntity();
+        return;
+    }
+    else
+    {
+        orxout(user_warning) << "Script tried to spawn an object that is neither a WorldEntity, nor a PlayerInfo" << std::endl;
+        return;
+    }
+
+    if(entity->isA(ClassIdentifier<MobileEntity>::getIdentifier()))
+        this->controller_->registerMobileEntity(id, orxonox_cast<MobileEntity*>(entity));
+
+    if(entity->isA(ClassIdentifier<Pawn>::getIdentifier()))
+        this->controller_->registerPawn(id, orxonox_cast<Pawn*>(entity));
+
+    this->controller_->registerWorldEntity(id, orxonox_cast<WorldEntity*>(entity));
+}
+
 void ScriptableControllerAPI::setPosition(std::string id, double x, double y, double z)
 {
@@ -179,5 +233,5 @@
 }
 
-void ScriptableControllerAPI::pawnKilled(std::string id)
+void ScriptableControllerAPI::pawnKilled(std::string id, Pawn *pawn)
 {
     for(auto callback : this->pawnDestroyedHandlers_[id])
@@ -185,4 +239,14 @@
 
     this->pawnDestroyedHandlers_.erase(id);
+
+    // We need to delete those handlers as well, they're no longer valid
+    auto near_obj_handler = this->nearObjectHandlers_.begin();
+    while(near_obj_handler != this->nearObjectHandlers_.end())
+    {
+        if(near_obj_handler->entity1_ == pawn || near_obj_handler->entity2_ == pawn)
+            near_obj_handler = this->nearObjectHandlers_.erase(near_obj_handler);
+        else
+            near_obj_handler++;
+    }
 }
 
@@ -257,4 +321,6 @@
 
     // Pawns to kill
+    // TODO Possible race condidtion when the player destroys the pawn
+    // between the callback and the next periodic call.
     for(auto &pawn : this->pawnsToKill_)
         this->controller_->killPawn(pawn);
