Explorar el Código

Merge pull request #958 from sgrenier/next

Exposed game command-line arguments to Lua script.
Steve Grenier hace 12 años
padre
commit
be35f66409
Se han modificado 1 ficheros con 17 adiciones y 0 borrados
  1. 17 0
      gameplay/src/ScriptController.cpp

+ 17 - 0
gameplay/src/ScriptController.cpp

@@ -713,6 +713,23 @@ void ScriptController::initialize()
         GP_ERROR("Failed to load custom loadfile() function with error: '%s'.", lua_tostring(_lua, -1));
     if (luaL_dostring(_lua, lua_dofile_function))
         GP_ERROR("Failed to load custom dofile() function with error: '%s'.", lua_tostring(_lua, -1));
+
+    // Write game command-line arguments to a global lua "arg" table
+    std::ostringstream args;
+    int argc;
+    char** argv;
+    Game::getInstance()->getArguments(&argc, &argv);
+    args << "arg = { }\n";
+    for (int i = 0; i < argc; ++i)
+    {
+        args << "arg[" << (i) << "] = [[" << argv[i] << "]]\n";
+    }
+    std::string argsStr = args.str();
+    if (argsStr.length() > 0)
+    {
+        if (luaL_dostring(_lua, argsStr.c_str()))
+            GP_ERROR("Failed to pass command-line arguments with error: '%s'.", lua_tostring(_lua, -1));
+    }
 }
 
 void ScriptController::initializeGame()