Changeset 11549 for code/branches/ScriptableController_HS17/src
- Timestamp:
- Nov 6, 2017, 5:23:08 PM (7 years ago)
- Location:
- code/branches/ScriptableController_HS17/src/orxonox
- Files:
-
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/ScriptableController_HS17/src/orxonox/Level.cc
r11518 r11549 87 87 if(this->level_script_ != "") 88 88 { 89 this->controller_ .reset(new ScriptableController());89 this->controller_ = new ScriptableController(this->getContext()); 90 90 this->controller_->runScript(this->level_script_); 91 91 } … … 177 177 { 178 178 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_); 181 181 } 182 182 -
code/branches/ScriptableController_HS17/src/orxonox/Level.h
r11518 r11549 92 92 std::map<std::string,MeshLodInformation*> lodInformation_; 93 93 94 std::unique_ptr<ScriptableController>controller_;95 std::string 94 ScriptableController* controller_; 95 std::string level_script_; 96 96 }; 97 97 } -
code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/luatb.h
r11519 r11549 15 15 // visible, you should call it like this: 16 16 // 17 // Lua Wrapper<decltype(foo)>::registerFunction<foo>( ... );17 // LuaTB<decltype(foo)>::registerFunction<foo>( ... ); 18 18 template<FunctionType func> 19 19 static void registerFunction(ThisType *_this, lua_State *lua, std::string name); -
code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/luatb.ipp
r11519 r11549 3 3 #include <iostream> 4 4 #include <type_traits> 5 #include "scriptable_controller_api.h"6 5 #include "luatb_typed_stack.h" 7 6 … … 20 19 { 21 20 // 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; 23 22 24 23 // Make a function visible to lua that will call 'func' with the correct … … 28 27 29 28 private: 29 static std::map<lua_State*, ThisType*> stateToClassMap; 30 30 31 // Represents a function that can made visible to lua with the correct 31 32 // signature. It will call the corresponding C++ function with the … … 34 35 static int toLuaSignature(lua_State *lua) 35 36 { 36 // The number of arguments is the first item on the stack37 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); 39 40 40 41 // Check if the number of arguments match 41 42 if(argc != sizeof...(Args)) 42 43 { 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; 44 45 return LUA_ERRSYNTAX; 45 46 } 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; 50 48 51 49 // Call getArgument for every argument seperately to convert each argument 52 50 // 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))... ); 54 52 55 53 return 0; 56 54 } 57 55 }; 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. 59 template<typename ThisType, typename Ret, typename... Args> 60 std::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 1 1 2 2 #include "luatb_typed_stack.h" 3 #include "luatb.h" 3 4 #include <string> 4 5 -
code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/luatb_typed_stack.h
r11519 r11549 6 6 #include <lua.hpp> 7 7 #include "is_callable.h" 8 #include "core/CoreIncludes.h" 8 9 9 10 // We need a separate class for this because we need to specialize the functions … … 54 55 static std::function<Ret (Args...)> value(lua_State *lua, int ref) 55 56 { 57 orxonox::orxout(orxonox::user_warning) << "Ref1: " << ref << std::endl; 56 58 return [lua, ref](Args... args){return callLuaFunction<Ret, Args...>(lua, ref, args...);}; 57 59 } … … 65 67 // We pass one extra argument in case the function has no arguments at all 66 68 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; 72 if(r != 0){ 73 const char* message = lua_tostring(lua, -1); 74 std::cout << message << std::endl; 75 lua_pop(lua, 1); 76 } 68 77 69 78 // TODO -
code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller.cc
r11519 r11549 6 6 { 7 7 8 RegisterUnloadableClass(ScriptableController); 9 8 10 // Used https://csl.name/post/lua-and-cpp/ as a reference 11 ScriptableController::ScriptableController(Context* context) 12 : BaseObject(context) 13 { 14 RegisterObject(ScriptableController); 15 } 16 9 17 int ScriptableController::runScript(const std::string &file_path) 10 18 { … … 71 79 } 72 80 81 void 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 88 void 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 73 110 void ScriptableController::printLuaError(lua_State *lua) 74 111 { -
code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller.h
r11519 r11549 17 17 { 18 18 19 class ScriptableController 19 class ScriptableController : public BaseObject, public Tickable 20 20 { 21 21 public: 22 explicit ScriptableController(Context *context); 23 22 24 int runScript(const std::string &file_path); 23 25 … … 28 30 ControllableEntity *getControllableEntityByID(int id) const; 29 31 32 void registerTimeout(std::function<void (void)> callback, double timeout); 33 34 virtual void tick(float dt) override; 35 30 36 private: 31 37 std::list<std::unique_ptr<ScriptableControllerAPI> > apis_; … … 33 39 std::map<int, WorldEntity*> worldEntities_; 34 40 std::map<int, ControllableEntity*> controllabelEntities_; 41 std::list<std::pair<std::function<void (void)>, float> > timeouts; 35 42 36 43 void printLuaError(lua_State *lua); -
code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.cc
r11519 r11549 9 9 { 10 10 11 ScriptableControllerAPI *ScriptableControllerAPI::this_;12 13 11 ScriptableControllerAPI::ScriptableControllerAPI(lua_State *lua, ScriptableController *controller) 14 12 { 15 13 this->lua_ = lua; 16 14 this->controller_ = controller; 17 ScriptableControllerAPI::this_ = this;18 15 19 16 // Haven't found a shorter way yet to write that... 20 LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::testOutput)>::registerFunction<&ScriptableControllerAPI::testOutput>(this, lua, "testOutput");21 17 LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAfterTimeout)>::registerFunction<&ScriptableControllerAPI::registerAfterTimeout>(this, lua, "registerAfterTimeout"); 22 18 LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtNearObject)>::registerFunction<&ScriptableControllerAPI::registerAtNearObject>(this, lua, "registerAtNearObject"); … … 32 28 } 33 29 34 void ScriptableControllerAPI:: testOutput()30 void ScriptableControllerAPI::registerAfterTimeout(std::function<void (void)> callback, double timeout) 35 31 { 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); 42 33 } 43 34 44 35 int ScriptableControllerAPI::registerAtNearObject(std::function<void (int, int)> callback, int obj1, int obj2, double distance) 45 36 { 46 37 orxout(user_warning) << "Working!" << std::endl; 47 38 } 48 39 -
code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.h
r11519 r11549 20 20 ~ScriptableControllerAPI(); 21 21 22 void testOutput( void);22 void testOutput(std::function<void(std::string)> callback); 23 23 24 void registerAfterTimeout(std::function<void (void)> callback, int timeout_ms);24 void registerAfterTimeout(std::function<void (void)> callback, double timeout); 25 25 int registerAtNearObject(std::function<void(int, int)> callback, int obj1, int obj2, double distance); 26 26 int registerAtAreaEnter(std::function<void (int)> callback, int obj, int x, int y, int z, int dx, int dy, int dz); … … 33 33 int setObjectPosition(int obj, double x, double y, double z); 34 34 35 static ScriptableControllerAPI *this_;36 37 35 private: 38 36 lua_State *lua_;
Note: See TracChangeset
for help on using the changeset viewer.