Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Crashes when killing the player

Location:
code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller
Files:
3 edited

Legend:

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

    r11638 r11662  
    3535    if((ret = luaL_loadfile(lua, file_path.c_str())) != 0)
    3636    {
     37        orxout(user_error) << "Failed to load level script " + file_path << std::endl;
    3738        this->printLuaError(lua);
    3839        delete api;
     
    4344    if((ret = lua_pcall(lua, 0, LUA_MULTRET, 0)) != 0)
    4445    {
     46        orxout(user_error) << "Level script returned an error" << std::endl;
    4547        this->printLuaError(lua);
    4648        delete api;
     
    8688
    8789    for(auto &api : this->apis_)
    88         api->pawnKilled(pawn_id_iter->second);
     90        api->pawnKilled(pawn_id_iter->second, pawn);
    8991
    9092    this->pawns_.erase(pawn_id_iter->second);
     
    110112void ScriptableController::killPawn(std::string id)
    111113{
    112     auto pawn = this->pawns_.find(id);
    113     if(pawn == this->pawns_.end())
     114    auto pawn = this->getPawnByID(id);
     115    if(pawn == nullptr)
    114116    {
    115117        orxout(user_warning) << "Tried to destroy unknown pawn " << id << std::endl;
     
    117119    }
    118120
    119     pawn->second->kill();
     121    pawn->kill();
    120122}
    121123
     
    141143Pawn *ScriptableController::getPawnByID(std::string id) const
    142144{
     145    if(id == "player" || id == "Player" || id == "PLAYER")
     146        return orxonox_cast<Pawn*>(this->player_->getControllableEntity());
     147
    143148    auto pawn = this->pawns_.find(id);
    144149    return pawn != this->pawns_.end() ? pawn->second : nullptr;
     
    151156    // Yes, this is 'const char*' and not 'std::string' because that's what lua gives us.
    152157    const char* message = lua_tostring(lua, -1);
    153     std::cout << message << std::endl;
     158    orxout(user_error) << message << std::endl;
    154159    lua_pop(lua, 1);
    155160}
  • 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);
  • code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.h

    r11638 r11662  
    1414class ScriptableController;
    1515class WorldEntity;
     16class Pawn;
    1617
    1718/**
     
    2425class ScriptableControllerAPI
    2526{
     27friend class ScriptableController;
     28
    2629public:
    2730    /**
     
    140143
    141144    /**
     145     * @brief Spawn an object
     146     * @param type Name of the class of the object you want to spawn
     147     * @param id The newly created ID that can be used to access this object
     148     */
     149    void spawn(std::string type, std::string id);
     150
     151    /**
    142152     * @brief Set the position of an object
    143153     * @param id The ID of the object
     
    185195// ### API END ################################################################
    186196
     197private:
    187198    /**
    188199     * @brief Called by ScriptableController when a pawn is killed
     
    191202     * Calls the lua callbacks associated with this event.
    192203     */
    193     void pawnKilled(std::string id);
     204    void pawnKilled(std::string id, Pawn *pawn);
    194205
    195206    /**
     
    202213    void pawnHit(std::string target_id, std::string source_id, double new_health, double new_shield);
    203214
    204 private:
    205215    /**
    206216     * @brief Groups everything together that is needed to handle a near-object event
Note: See TracChangeset for help on using the changeset viewer.