Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller.cc @ 11549

Last change on this file since 11549 was 11549, checked in by kohlia, 7 years ago

Not working yet

File size: 3.1 KB
Line 
1
2#include "scriptable_controller.h"
3#include "luatb.h"
4
5namespace orxonox
6{
7
8RegisterUnloadableClass(ScriptableController);
9
10// Used https://csl.name/post/lua-and-cpp/ as a reference
11ScriptableController::ScriptableController(Context* context)
12    : BaseObject(context)
13{
14    RegisterObject(ScriptableController);
15}
16
17int ScriptableController::runScript(const std::string &file_path)
18{
19    int ret;
20
21    // Create a lua object
22    lua_State *lua = luaL_newstate();
23    if(lua == nullptr)
24    {
25        orxout(internal_warning) << "ScriptableController: Falied to load script " << file_path << std::endl;
26        return LUA_ERRMEM;
27    }
28
29    // Make standard libraries available in the Lua object
30    luaL_openlibs(lua);
31
32    // Register the API
33    ScriptableControllerAPI *api = new ScriptableControllerAPI(lua, this);
34
35    // Load the program; this supports both source code and bytecode files.
36    if((ret = luaL_loadfile(lua, file_path.c_str())) != 0)
37    {
38        this->printLuaError(lua);
39        delete api;
40        return ret;
41    }
42
43    // Execute the script
44    if((ret = lua_pcall(lua, 0, LUA_MULTRET, 0)) != 0)
45    {
46        this->printLuaError(lua);
47        delete api;
48        return ret;
49    }
50
51    // Remeber the api
52    this->apis_.push_back(std::unique_ptr<ScriptableControllerAPI>(api));
53
54    // Only the caller knows, when the end-of-life of the script is reached,
55    // so we give him control over when to destroy it.
56    return 0;
57}
58
59void ScriptableController::registerWorldEntity(int id, WorldEntity *obj)
60{
61    this->worldEntities_[id] = obj;
62}
63
64void ScriptableController::registerControllableEntity(int id, ControllableEntity *obj)
65{
66    this->worldEntities_[id] = obj;
67}
68
69WorldEntity *ScriptableController::getWorldEntityByID(int id) const
70{
71    auto obj = this->worldEntities_.find(id);
72    return obj != this->worldEntities_.end() ? obj->second : nullptr;
73}
74
75ControllableEntity *ScriptableController::getControllableEntityByID(int id) const
76{
77    auto obj = this->controllabelEntities_.find(id);
78    return obj != this->controllabelEntities_.end() ? obj->second : nullptr;
79}
80
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
110void ScriptableController::printLuaError(lua_State *lua)
111{
112    // The error message is on top of the stack.
113    // Fetch it, print it and then pop it off the stack.
114    // Yes, this is 'const char*' and not 'std::string' because that's what lua gives us.
115    const char* message = lua_tostring(lua, -1);
116    std::cout << message << std::endl;
117    lua_pop(lua, 1);
118}
119
120}
Note: See TracBrowser for help on using the repository browser.