Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Nov 6, 2017, 5:23:08 PM (7 years ago)
Author:
kohlia
Message:

Not working yet

Location:
code/branches/ScriptableController_HS17/src/orxonox
Files:
10 edited

Legend:

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

    r11518 r11549  
    8787        if(this->level_script_ != "")
    8888        {
    89             this->controller_.reset(new ScriptableController());
     89            this->controller_ = new ScriptableController(this->getContext());
    9090            this->controller_->runScript(this->level_script_);
    9191        }
     
    177177    {
    178178        this->objects_.push_back(object);
    179         if(this->controller_.get() != nullptr)
    180             object->registerToScriptableController(this->controller_.get());
     179        if(this->controller_ != nullptr)
     180            object->registerToScriptableController(this->controller_);
    181181    }
    182182
  • code/branches/ScriptableController_HS17/src/orxonox/Level.h

    r11518 r11549  
    9292            std::map<std::string,MeshLodInformation*>  lodInformation_;
    9393
    94             std::unique_ptr<ScriptableController> controller_;
    95             std::string                           level_script_;
     94            ScriptableController* controller_;
     95            std::string           level_script_;
    9696    };
    9797}
  • code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/luatb.h

    r11519 r11549  
    1515    // visible, you should call it like this:
    1616    //
    17     // LuaWrapper<decltype(foo)>::registerFunction<foo>( ... );
     17    // LuaTB<decltype(foo)>::registerFunction<foo>( ... );
    1818    template<FunctionType func>
    1919    static void registerFunction(ThisType *_this, lua_State *lua, std::string name);
  • code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/luatb.ipp

    r11519 r11549  
    33#include <iostream>
    44#include <type_traits>
    5 #include "scriptable_controller_api.h"
    65#include "luatb_typed_stack.h"
    76
     
    2019    {
    2120        // Store the 'this' pointer of the caller in the extraspace
    22 //        *static_cast<ThisType**>(lua_getextraspace(lua)) = _this;
     21        LuaTB<ThisType, Ret (ThisType::*)(Args...)>::stateToClassMap[lua] = _this;
    2322
    2423        // Make a function visible to lua that will call 'func' with the correct
     
    2827
    2928private:
     29    static std::map<lua_State*, ThisType*> stateToClassMap;
     30
    3031    // Represents a function that can made visible to lua with the correct
    3132    // signature. It will call the corresponding C++ function with the
     
    3435    static int toLuaSignature(lua_State *lua)
    3536    {
    36         // The number of arguments is the first item on the stack
    37         int argc = lua_tointeger(lua, lua_gettop(lua));
    38         lua_pop(lua, 1);
     37        // The index of the topmost item on the stack equals the size of
     38        // the stack, which also equals the number of arguments
     39        int argc = lua_gettop(lua);
    3940
    4041        // Check if the number of arguments match
    4142        if(argc != sizeof...(Args))
    4243        {
    43             std::cerr << "ERROR: LuaTB: Lua script called a function with wrong number of arguments" << std::endl;
     44            std::cerr << "ERROR: LuaTB: Lua script called a function with wrong number of arguments (" << argc << " given, " << sizeof...(Args) << " expected)" << std::endl;
    4445            return LUA_ERRSYNTAX;
    4546        }
    46 
    47         // Retrieve 'this' pointer of caller
    48 //        ThisType *_this = *static_cast<ThisType**>(lua_getextraspace(lua));
    49 ThisType *_this = orxonox::ScriptableControllerAPI::this_;
     47        orxonox::orxout(orxonox::user_warning) << "what" << std::endl;
    5048
    5149        // Call getArgument for every argument seperately to convert each argument
    5250        // to the correct type and call the function afterwards
    53         ((*_this).*func)( (LuaTBTypedStack::getArgument<Args>(lua))... );
     51        ((*LuaTB<ThisType, Ret (ThisType::*)(Args...)>::stateToClassMap[lua]).*func)( (LuaTBTypedStack::getArgument<Args>(lua))... );
    5452
    5553        return 0;
    5654    }
    5755};
     56
     57// This needs to be here and not in a source file, because the compiler can't know
     58// the template types if it's in a separate module.
     59template<typename ThisType, typename Ret, typename... Args>
     60std::map<lua_State*, ThisType*> LuaTB<ThisType, Ret (ThisType::*)(Args...)>::stateToClassMap = std::map<lua_State*, ThisType*>();
  • code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/luatb_typed_stack.cc

    r11519 r11549  
    11
    22#include "luatb_typed_stack.h"
     3#include "luatb.h"
    34#include <string>
    45
  • code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/luatb_typed_stack.h

    r11519 r11549  
    66#include <lua.hpp>
    77#include "is_callable.h"
     8#include "core/CoreIncludes.h"
    89
    910// We need a separate class for this because we need to specialize the functions
     
    5455        static std::function<Ret (Args...)> value(lua_State *lua, int ref)
    5556        {
     57            orxonox::orxout(orxonox::user_warning) << "Ref1: " << ref << std::endl;
    5658            return [lua, ref](Args... args){return callLuaFunction<Ret, Args...>(lua, ref, args...);};
    5759        }
     
    6567        // We pass one extra argument in case the function has no arguments at all
    6668        pushArgumentsToLuaStack<Args...>(lua, args..., false);
    67         lua_pcall(lua, 1, sizeof...(args), 0);
     69        int r = lua_pcall(lua, 1, sizeof...(args), 0);
     70
     71        orxonox::orxout(orxonox::user_warning) << "Ref2: " << ref << std::endl;
     72if(r != 0){
     73        const char* message = lua_tostring(lua, -1);
     74        std::cout << message << std::endl;
     75        lua_pop(lua, 1);
     76}
    6877
    6978        // TODO
  • code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller.cc

    r11519 r11549  
    66{
    77
     8RegisterUnloadableClass(ScriptableController);
     9
    810// Used https://csl.name/post/lua-and-cpp/ as a reference
     11ScriptableController::ScriptableController(Context* context)
     12    : BaseObject(context)
     13{
     14    RegisterObject(ScriptableController);
     15}
     16
    917int ScriptableController::runScript(const std::string &file_path)
    1018{
     
    7179}
    7280
     81void ScriptableController::registerTimeout(std::function<void ()> callback, double timeout)
     82{
     83    this->timeouts.push_back(std::make_pair(callback, static_cast<float>(timeout)));
     84    orxout(user_warning) << "Calling should work..." << std::endl;
     85    callback();
     86}
     87
     88void ScriptableController::tick(float dt)
     89{
     90    auto timeout = this->timeouts.begin();
     91
     92    while(timeout != this->timeouts.end())
     93    {
     94        timeout->second -= dt;
     95        if(timeout->second <= 0)
     96        {
     97            orxout(user_warning) << "Calling..." << std::endl;
     98            timeout->first();
     99            timeout = this->timeouts.erase(timeout);
     100        }
     101        else
     102        {
     103            timeout++;
     104        }
     105    }
     106
     107    Tickable::tick(dt);
     108}
     109
    73110void ScriptableController::printLuaError(lua_State *lua)
    74111{
  • code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller.h

    r11519 r11549  
    1717{
    1818
    19 class ScriptableController
     19class ScriptableController : public BaseObject, public Tickable
    2020{
    2121public:
     22    explicit ScriptableController(Context *context);
     23
    2224    int runScript(const std::string &file_path);
    2325
     
    2830    ControllableEntity *getControllableEntityByID(int id) const;
    2931
     32    void registerTimeout(std::function<void (void)> callback, double timeout);
     33
     34    virtual void tick(float dt) override;
     35
    3036private:
    3137    std::list<std::unique_ptr<ScriptableControllerAPI> > apis_;
     
    3339    std::map<int, WorldEntity*> worldEntities_;
    3440    std::map<int, ControllableEntity*> controllabelEntities_;
     41    std::list<std::pair<std::function<void (void)>, float> > timeouts;
    3542
    3643    void printLuaError(lua_State *lua);
  • code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.cc

    r11519 r11549  
    99{
    1010
    11 ScriptableControllerAPI *ScriptableControllerAPI::this_;
    12 
    1311ScriptableControllerAPI::ScriptableControllerAPI(lua_State *lua, ScriptableController *controller)
    1412{
    1513    this->lua_ = lua;
    1614    this->controller_ = controller;
    17     ScriptableControllerAPI::this_ = this;
    1815
    1916    // Haven't found a shorter way yet to write that...
    20     LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::testOutput)>::registerFunction<&ScriptableControllerAPI::testOutput>(this, lua, "testOutput");
    2117    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAfterTimeout)>::registerFunction<&ScriptableControllerAPI::registerAfterTimeout>(this, lua, "registerAfterTimeout");
    2218    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtNearObject)>::registerFunction<&ScriptableControllerAPI::registerAtNearObject>(this, lua, "registerAtNearObject");
     
    3228}
    3329
    34 void ScriptableControllerAPI::testOutput()
     30void ScriptableControllerAPI::registerAfterTimeout(std::function<void (void)> callback, double timeout)
    3531{
    36     orxout(user_info) << "Wheee!!!" << std::endl;
    37 }
    38 
    39 void ScriptableControllerAPI::registerAfterTimeout(std::function<void (void)> callback, int timeout_ms)
    40 {
    41 
     32    this->controller_->registerTimeout(callback, timeout);
    4233}
    4334
    4435int ScriptableControllerAPI::registerAtNearObject(std::function<void (int, int)> callback, int obj1, int obj2, double distance)
    4536{
    46 
     37    orxout(user_warning) << "Working!" << std::endl;
    4738}
    4839
  • code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.h

    r11519 r11549  
    2020    ~ScriptableControllerAPI();
    2121
    22     void testOutput(void);
     22    void testOutput(std::function<void(std::string)> callback);
    2323
    24     void registerAfterTimeout(std::function<void (void)> callback, int timeout_ms);
     24    void registerAfterTimeout(std::function<void (void)> callback, double timeout);
    2525    int registerAtNearObject(std::function<void(int, int)> callback, int obj1, int obj2, double distance);
    2626    int registerAtAreaEnter(std::function<void (int)> callback, int obj, int x, int y, int z, int dx, int dy, int dz);
     
    3333    int setObjectPosition(int obj, double x, double y, double z);
    3434
    35     static ScriptableControllerAPI *this_;
    36 
    3735private:
    3836    lua_State *lua_;
Note: See TracChangeset for help on using the changeset viewer.