aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNipun Garg2019-06-18 04:14:45 +0530
committerEugene Sandulenko2019-09-03 17:16:47 +0200
commit2c55d072d36c7c7951cc772ae968ab255cfd05f1 (patch)
tree6b121a4617204401966bb409a862ca8bcb6461d8
parentd8cf201ae751665ed0e2f4d62c9b24fb7e866aa9 (diff)
downloadscummvm-rg350-2c55d072d36c7c7951cc772ae968ab255cfd05f1.tar.gz
scummvm-rg350-2c55d072d36c7c7951cc772ae968ab255cfd05f1.tar.bz2
scummvm-rg350-2c55d072d36c7c7951cc772ae968ab255cfd05f1.zip
HDB: Add support to call Lua functions
This allows us to easily to call functions defined in the Lua scripts.
-rw-r--r--engines/hdb/lua-script.cpp20
-rw-r--r--engines/hdb/lua-script.h1
2 files changed, 19 insertions, 2 deletions
diff --git a/engines/hdb/lua-script.cpp b/engines/hdb/lua-script.cpp
index 79d164e525..8a79bdd1ed 100644
--- a/engines/hdb/lua-script.cpp
+++ b/engines/hdb/lua-script.cpp
@@ -644,7 +644,6 @@ bool LuaScript::initScript(Common::SeekableReadStream *stream, const char *scrip
spawn names into Lua once they are implemented.
*/
-
// Register panic callback function
lua_atpanic(_state, panicCB);
@@ -696,7 +695,6 @@ bool LuaScript::initScript(Common::SeekableReadStream *stream, const char *scrip
return false;
}
-
// Error handling function to be executed after the function is put on the stack
lua_rawgeti(_state, LUA_REGISTRYINDEX, _pcallErrorhandlerRegistryIndex);
lua_insert(_state, -2);
@@ -713,6 +711,24 @@ bool LuaScript::initScript(Common::SeekableReadStream *stream, const char *scrip
return true;
}
+bool LuaScript::callFunction(const char *name, int returns) {
+
+ if (!_systemInit) {
+ return false;
+ }
+
+ lua_getglobal(_state, name);
+
+ if (lua_pcall(_state, 0, 0, -2)) {
+ error("An error occured while executing \"%s\": %s.", name, lua_tostring(_state, -1));
+ lua_pop(_state, -1);
+
+ return false;
+ }
+
+ return true;
+}
+
bool LuaScript::executeMPC(Common::SeekableReadStream *stream, const char *name, const char *scriptName, int32 length) {
if (!_systemInit) {
diff --git a/engines/hdb/lua-script.h b/engines/hdb/lua-script.h
index 2a8686f96c..ea209a383c 100644
--- a/engines/hdb/lua-script.h
+++ b/engines/hdb/lua-script.h
@@ -39,6 +39,7 @@ public:
bool init();
bool initScript(Common::SeekableReadStream *stream, const char *scriptName, int32 length);
+ bool callFunction(const char *name, int returns);
bool executeMPC(Common::SeekableReadStream *stream, const char *name, const char *scriptName, int32 length);
bool executeFile(const Common::String &filename);