Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Lua bindings should work now

File size: 3.0 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}
85
86void ScriptableController::tick(float dt)
87{
88    auto timeout = this->timeouts.begin();
89
90    while(timeout != this->timeouts.end())
91    {
92        timeout->second -= dt;
93        if(timeout->second <= 0)
94        {
95            timeout->first();
96            timeout = this->timeouts.erase(timeout);
97        }
98        else
99        {
100            timeout++;
101        }
102    }
103
104    Tickable::tick(dt);
105}
106
107void ScriptableController::printLuaError(lua_State *lua)
108{
109    // The error message is on top of the stack.
110    // Fetch it, print it and then pop it off the stack.
111    // Yes, this is 'const char*' and not 'std::string' because that's what lua gives us.
112    const char* message = lua_tostring(lua, -1);
113    std::cout << message << std::endl;
114    lua_pop(lua, 1);
115}
116
117}
Note: See TracBrowser for help on using the repository browser.