Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Dec 11, 2017, 4:35:38 PM (6 years ago)
Author:
kohlia
Message:

Crashes when killing the player

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.cc

    r11638 r11662  
    44#include "scriptable_controller.h"
    55#include "tools/Timer.h"
     6#include "worldentities/pawns/Pawn.h"
     7#include "infos/Bot.h"
     8#include "worldentities/pawns/ModularSpaceShip.h"
    69
    710namespace orxonox
     
    3235
    3336    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::killPawn)>::registerFunction<&ScriptableControllerAPI::killPawn>(this, lua, "killPawn");
     37    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::spawn)>::registerFunction<&ScriptableControllerAPI::spawn>(this, lua, "spawn");
    3438
    3539    this->periodicTimer.setTimer(ScriptableControllerAPI::periodic_interval, true, createExecutor(createFunctor(&ScriptableControllerAPI::periodic, this)), false);
     
    103107}
    104108
     109void ScriptableControllerAPI::spawn(std::string type, std::string id)
     110{
     111    if(this->controller_->getWorldEntityByID(id) != nullptr)
     112    {
     113        orxout(user_warning) << "Script tried to spawn an object, but an object with the given ID exists already" << std::endl;
     114        return;
     115    }
     116
     117    Identifier *identifier = ClassByString(type);
     118    if(!identifier)
     119    {
     120        orxout(user_error) << "Script tried to spawn unknown object" << std::endl;
     121        return;
     122    }
     123
     124    if(!identifier->isLoadable())
     125    {
     126        orxout(user_error) << "Script tried to spawn unloadable object" << std::endl;
     127        return;
     128    }
     129
     130    WorldEntity *entity;
     131    Identifiable *obj = identifier->fabricate(this->controller_->getWorldEntityByID("Player")->getContext());
     132
     133    if(obj->isA(ClassIdentifier<WorldEntity>::getIdentifier()))
     134    {
     135        entity = orxonox_cast<WorldEntity*>(obj);
     136    }
     137    else if(obj->isA(ClassIdentifier<PlayerInfo>::getIdentifier()))
     138    {
     139        // TODO This does not work yet because somehow the controllable entity is not set
     140        // yet at this stage.
     141//        entity = orxonox_cast<PlayerInfo*>(obj)->getControllableEntity();
     142        return;
     143    }
     144    else
     145    {
     146        orxout(user_warning) << "Script tried to spawn an object that is neither a WorldEntity, nor a PlayerInfo" << std::endl;
     147        return;
     148    }
     149
     150    if(entity->isA(ClassIdentifier<MobileEntity>::getIdentifier()))
     151        this->controller_->registerMobileEntity(id, orxonox_cast<MobileEntity*>(entity));
     152
     153    if(entity->isA(ClassIdentifier<Pawn>::getIdentifier()))
     154        this->controller_->registerPawn(id, orxonox_cast<Pawn*>(entity));
     155
     156    this->controller_->registerWorldEntity(id, orxonox_cast<WorldEntity*>(entity));
     157}
     158
    105159void ScriptableControllerAPI::setPosition(std::string id, double x, double y, double z)
    106160{
     
    179233}
    180234
    181 void ScriptableControllerAPI::pawnKilled(std::string id)
     235void ScriptableControllerAPI::pawnKilled(std::string id, Pawn *pawn)
    182236{
    183237    for(auto callback : this->pawnDestroyedHandlers_[id])
     
    185239
    186240    this->pawnDestroyedHandlers_.erase(id);
     241
     242    // We need to delete those handlers as well, they're no longer valid
     243    auto near_obj_handler = this->nearObjectHandlers_.begin();
     244    while(near_obj_handler != this->nearObjectHandlers_.end())
     245    {
     246        if(near_obj_handler->entity1_ == pawn || near_obj_handler->entity2_ == pawn)
     247            near_obj_handler = this->nearObjectHandlers_.erase(near_obj_handler);
     248        else
     249            near_obj_handler++;
     250    }
    187251}
    188252
     
    257321
    258322    // Pawns to kill
     323    // TODO Possible race condidtion when the player destroys the pawn
     324    // between the callback and the next periodic call.
    259325    for(auto &pawn : this->pawnsToKill_)
    260326        this->controller_->killPawn(pawn);
Note: See TracChangeset for help on using the changeset viewer.