[Orxonox-commit 6928] r11549 - in code/branches/ScriptableController_HS17/src/orxonox: . scriptablecontroller
kohlia at orxonox.net
kohlia at orxonox.net
Mon Nov 6 17:23:09 CET 2017
Author: kohlia
Date: 2017-11-06 17:23:08 +0100 (Mon, 06 Nov 2017)
New Revision: 11549
Modified:
code/branches/ScriptableController_HS17/src/orxonox/Level.cc
code/branches/ScriptableController_HS17/src/orxonox/Level.h
code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/luatb.h
code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/luatb.ipp
code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/luatb_typed_stack.cc
code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/luatb_typed_stack.h
code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller.cc
code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller.h
code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.cc
code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.h
Log:
Not working yet
Modified: code/branches/ScriptableController_HS17/src/orxonox/Level.cc
===================================================================
--- code/branches/ScriptableController_HS17/src/orxonox/Level.cc 2017-11-06 16:11:38 UTC (rev 11548)
+++ code/branches/ScriptableController_HS17/src/orxonox/Level.cc 2017-11-06 16:23:08 UTC (rev 11549)
@@ -86,7 +86,7 @@
if(this->level_script_ != "")
{
- this->controller_.reset(new ScriptableController());
+ this->controller_ = new ScriptableController(this->getContext());
this->controller_->runScript(this->level_script_);
}
}
@@ -176,8 +176,8 @@
void Level::addObject(BaseObject* object)
{
this->objects_.push_back(object);
- if(this->controller_.get() != nullptr)
- object->registerToScriptableController(this->controller_.get());
+ if(this->controller_ != nullptr)
+ object->registerToScriptableController(this->controller_);
}
BaseObject* Level::getObject(unsigned int index) const
Modified: code/branches/ScriptableController_HS17/src/orxonox/Level.h
===================================================================
--- code/branches/ScriptableController_HS17/src/orxonox/Level.h 2017-11-06 16:11:38 UTC (rev 11548)
+++ code/branches/ScriptableController_HS17/src/orxonox/Level.h 2017-11-06 16:23:08 UTC (rev 11549)
@@ -91,8 +91,8 @@
std::list<BaseObject*> objects_;
std::map<std::string,MeshLodInformation*> lodInformation_;
- std::unique_ptr<ScriptableController> controller_;
- std::string level_script_;
+ ScriptableController* controller_;
+ std::string level_script_;
};
}
Modified: code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/luatb.h
===================================================================
--- code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/luatb.h 2017-11-06 16:11:38 UTC (rev 11548)
+++ code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/luatb.h 2017-11-06 16:23:08 UTC (rev 11549)
@@ -14,7 +14,7 @@
// Make a function visible to lua. If you want to make a function 'foo'
// visible, you should call it like this:
//
- // LuaWrapper<decltype(foo)>::registerFunction<foo>( ... );
+ // LuaTB<decltype(foo)>::registerFunction<foo>( ... );
template<FunctionType func>
static void registerFunction(ThisType *_this, lua_State *lua, std::string name);
};
Modified: code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/luatb.ipp
===================================================================
--- code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/luatb.ipp 2017-11-06 16:11:38 UTC (rev 11548)
+++ code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/luatb.ipp 2017-11-06 16:23:08 UTC (rev 11549)
@@ -2,7 +2,6 @@
#include <lua.hpp>
#include <iostream>
#include <type_traits>
-#include "scriptable_controller_api.h"
#include "luatb_typed_stack.h"
@@ -19,7 +18,7 @@
static void registerFunction(ThisType *_this, lua_State *lua, std::string name)
{
// Store the 'this' pointer of the caller in the extraspace
-// *static_cast<ThisType**>(lua_getextraspace(lua)) = _this;
+ LuaTB<ThisType, Ret (ThisType::*)(Args...)>::stateToClassMap[lua] = _this;
// Make a function visible to lua that will call 'func' with the correct
// arguments
@@ -27,6 +26,8 @@
}
private:
+ static std::map<lua_State*, ThisType*> stateToClassMap;
+
// Represents a function that can made visible to lua with the correct
// signature. It will call the corresponding C++ function with the
// correctly typed arguments
@@ -33,25 +34,27 @@
template<Ret (ThisType::*func)(Args...)>
static int toLuaSignature(lua_State *lua)
{
- // The number of arguments is the first item on the stack
- int argc = lua_tointeger(lua, lua_gettop(lua));
- lua_pop(lua, 1);
+ // The index of the topmost item on the stack equals the size of
+ // the stack, which also equals the number of arguments
+ int argc = lua_gettop(lua);
// Check if the number of arguments match
if(argc != sizeof...(Args))
{
- std::cerr << "ERROR: LuaTB: Lua script called a function with wrong number of arguments" << std::endl;
+ std::cerr << "ERROR: LuaTB: Lua script called a function with wrong number of arguments (" << argc << " given, " << sizeof...(Args) << " expected)" << std::endl;
return LUA_ERRSYNTAX;
}
+ orxonox::orxout(orxonox::user_warning) << "what" << std::endl;
- // Retrieve 'this' pointer of caller
-// ThisType *_this = *static_cast<ThisType**>(lua_getextraspace(lua));
-ThisType *_this = orxonox::ScriptableControllerAPI::this_;
-
// Call getArgument for every argument seperately to convert each argument
// to the correct type and call the function afterwards
- ((*_this).*func)( (LuaTBTypedStack::getArgument<Args>(lua))... );
+ ((*LuaTB<ThisType, Ret (ThisType::*)(Args...)>::stateToClassMap[lua]).*func)( (LuaTBTypedStack::getArgument<Args>(lua))... );
return 0;
}
};
+
+// This needs to be here and not in a source file, because the compiler can't know
+// the template types if it's in a separate module.
+template<typename ThisType, typename Ret, typename... Args>
+std::map<lua_State*, ThisType*> LuaTB<ThisType, Ret (ThisType::*)(Args...)>::stateToClassMap = std::map<lua_State*, ThisType*>();
Modified: code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/luatb_typed_stack.cc
===================================================================
--- code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/luatb_typed_stack.cc 2017-11-06 16:11:38 UTC (rev 11548)
+++ code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/luatb_typed_stack.cc 2017-11-06 16:23:08 UTC (rev 11549)
@@ -1,5 +1,6 @@
#include "luatb_typed_stack.h"
+#include "luatb.h"
#include <string>
// Explicit and full specializations need to be in a .cpp file
Modified: code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/luatb_typed_stack.h
===================================================================
--- code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/luatb_typed_stack.h 2017-11-06 16:11:38 UTC (rev 11548)
+++ code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/luatb_typed_stack.h 2017-11-06 16:23:08 UTC (rev 11549)
@@ -5,6 +5,7 @@
#include <functional>
#include <lua.hpp>
#include "is_callable.h"
+#include "core/CoreIncludes.h"
// We need a separate class for this because we need to specialize the functions
// and that's not possible if we didn't specialize the class. And the logical
@@ -53,6 +54,7 @@
// like to hide from the user.
static std::function<Ret (Args...)> value(lua_State *lua, int ref)
{
+ orxonox::orxout(orxonox::user_warning) << "Ref1: " << ref << std::endl;
return [lua, ref](Args... args){return callLuaFunction<Ret, Args...>(lua, ref, args...);};
}
};
@@ -64,8 +66,15 @@
lua_rawgeti(lua, LUA_REGISTRYINDEX, ref);
// We pass one extra argument in case the function has no arguments at all
pushArgumentsToLuaStack<Args...>(lua, args..., false);
- lua_pcall(lua, 1, sizeof...(args), 0);
+ int r = lua_pcall(lua, 1, sizeof...(args), 0);
+ orxonox::orxout(orxonox::user_warning) << "Ref2: " << ref << std::endl;
+if(r != 0){
+ const char* message = lua_tostring(lua, -1);
+ std::cout << message << std::endl;
+ lua_pop(lua, 1);
+}
+
// TODO
return Ret();
}
Modified: code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller.cc
===================================================================
--- code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller.cc 2017-11-06 16:11:38 UTC (rev 11548)
+++ code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller.cc 2017-11-06 16:23:08 UTC (rev 11549)
@@ -5,7 +5,15 @@
namespace orxonox
{
+RegisterUnloadableClass(ScriptableController);
+
// Used https://csl.name/post/lua-and-cpp/ as a reference
+ScriptableController::ScriptableController(Context* context)
+ : BaseObject(context)
+{
+ RegisterObject(ScriptableController);
+}
+
int ScriptableController::runScript(const std::string &file_path)
{
int ret;
@@ -70,6 +78,35 @@
return obj != this->controllabelEntities_.end() ? obj->second : nullptr;
}
+void ScriptableController::registerTimeout(std::function<void ()> callback, double timeout)
+{
+ this->timeouts.push_back(std::make_pair(callback, static_cast<float>(timeout)));
+ orxout(user_warning) << "Calling should work..." << std::endl;
+ callback();
+}
+
+void ScriptableController::tick(float dt)
+{
+ auto timeout = this->timeouts.begin();
+
+ while(timeout != this->timeouts.end())
+ {
+ timeout->second -= dt;
+ if(timeout->second <= 0)
+ {
+ orxout(user_warning) << "Calling..." << std::endl;
+ timeout->first();
+ timeout = this->timeouts.erase(timeout);
+ }
+ else
+ {
+ timeout++;
+ }
+ }
+
+ Tickable::tick(dt);
+}
+
void ScriptableController::printLuaError(lua_State *lua)
{
// The error message is on top of the stack.
Modified: code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller.h
===================================================================
--- code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller.h 2017-11-06 16:11:38 UTC (rev 11548)
+++ code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller.h 2017-11-06 16:23:08 UTC (rev 11549)
@@ -16,9 +16,11 @@
namespace orxonox
{
-class ScriptableController
+class ScriptableController : public BaseObject, public Tickable
{
public:
+ explicit ScriptableController(Context *context);
+
int runScript(const std::string &file_path);
void registerWorldEntity(int id, WorldEntity *obj);
@@ -27,11 +29,16 @@
WorldEntity *getWorldEntityByID(int id) const;
ControllableEntity *getControllableEntityByID(int id) const;
+ void registerTimeout(std::function<void (void)> callback, double timeout);
+
+ virtual void tick(float dt) override;
+
private:
std::list<std::unique_ptr<ScriptableControllerAPI> > apis_;
ControllableEntity *player_; // TODO
std::map<int, WorldEntity*> worldEntities_;
std::map<int, ControllableEntity*> controllabelEntities_;
+ std::list<std::pair<std::function<void (void)>, float> > timeouts;
void printLuaError(lua_State *lua);
};
Modified: code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.cc
===================================================================
--- code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.cc 2017-11-06 16:11:38 UTC (rev 11548)
+++ code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.cc 2017-11-06 16:23:08 UTC (rev 11549)
@@ -8,16 +8,12 @@
namespace orxonox
{
-ScriptableControllerAPI *ScriptableControllerAPI::this_;
-
ScriptableControllerAPI::ScriptableControllerAPI(lua_State *lua, ScriptableController *controller)
{
this->lua_ = lua;
this->controller_ = controller;
- ScriptableControllerAPI::this_ = this;
// Haven't found a shorter way yet to write that...
- LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::testOutput)>::registerFunction<&ScriptableControllerAPI::testOutput>(this, lua, "testOutput");
LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAfterTimeout)>::registerFunction<&ScriptableControllerAPI::registerAfterTimeout>(this, lua, "registerAfterTimeout");
LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtNearObject)>::registerFunction<&ScriptableControllerAPI::registerAtNearObject>(this, lua, "registerAtNearObject");
LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtAreaEnter)>::registerFunction<&ScriptableControllerAPI::registerAtAreaEnter>(this, lua, "registerAtAreaEnter");
@@ -31,19 +27,14 @@
lua_close(this->lua_);
}
-void ScriptableControllerAPI::testOutput()
+void ScriptableControllerAPI::registerAfterTimeout(std::function<void (void)> callback, double timeout)
{
- orxout(user_info) << "Wheee!!!" << std::endl;
+ this->controller_->registerTimeout(callback, timeout);
}
-void ScriptableControllerAPI::registerAfterTimeout(std::function<void (void)> callback, int timeout_ms)
-{
-
-}
-
int ScriptableControllerAPI::registerAtNearObject(std::function<void (int, int)> callback, int obj1, int obj2, double distance)
{
-
+ orxout(user_warning) << "Working!" << std::endl;
}
int ScriptableControllerAPI::registerAtAreaEnter(std::function<void (int)> callback, int obj, int x, int y, int z, int dx, int dy, int dz)
Modified: code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.h
===================================================================
--- code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.h 2017-11-06 16:11:38 UTC (rev 11548)
+++ code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.h 2017-11-06 16:23:08 UTC (rev 11549)
@@ -19,9 +19,9 @@
ScriptableControllerAPI(lua_State *lua, ScriptableController *controller);
~ScriptableControllerAPI();
- void testOutput(void);
+ void testOutput(std::function<void(std::string)> callback);
- void registerAfterTimeout(std::function<void (void)> callback, int timeout_ms);
+ void registerAfterTimeout(std::function<void (void)> callback, double timeout);
int registerAtNearObject(std::function<void(int, int)> callback, int obj1, int obj2, double distance);
int registerAtAreaEnter(std::function<void (int)> callback, int obj, int x, int y, int z, int dx, int dy, int dz);
int registerAtAreaLeave(std::function<void (int)> callback, int obj, int x, int y, int z, int dx, int dy, int dz);
@@ -32,8 +32,6 @@
void removeObject(int obj);
int setObjectPosition(int obj, double x, double y, double z);
- static ScriptableControllerAPI *this_;
-
private:
lua_State *lua_;
ScriptableController *controller_;
More information about the Orxonox-commit
mailing list