diff --git a/src/scriptengine/script_engine.cpp b/src/scriptengine/script_engine.cpp index 250204d5e..a851dbe67 100644 --- a/src/scriptengine/script_engine.cpp +++ b/src/scriptengine/script_engine.cpp @@ -177,7 +177,17 @@ namespace Scripting void ScriptEngine::runFunction(std::string function_name) { std::function callback; - runFunction(function_name, callback); + std::function get_return_value; + runFunction(function_name, callback, get_return_value); + } + + //----------------------------------------------------------------------------- + + void ScriptEngine::runFunction(std::string function_name, + std::function callback) + { + std::function get_return_value; + runFunction(function_name, callback, get_return_value); } //----------------------------------------------------------------------------- @@ -185,7 +195,9 @@ namespace Scripting /** runs the specified script * \param string scriptName = name of script to run */ - void ScriptEngine::runFunction(std::string function_name, std::function callback) + void ScriptEngine::runFunction(std::string function_name, + std::function callback, + std::function get_return_value) { int r; //int for error checking @@ -199,11 +211,11 @@ namespace Scripting if (cached_script == m_loaded_files.end()) { // Compile the script code - Log::debug("Scripting", "Compiling script '%s' (was not in cache)", script_filename.c_str()); + Log::info("Scripting", "Checking for script file '%s'", script_filename.c_str()); r = compileScript(m_engine, script_filename); if (r < 0) { - Log::debug("Scripting", "Script '%s' is not available", script_filename.c_str()); + Log::info("Scripting", "Script '%s' is not available", script_filename.c_str()); m_loaded_files[script_filename] = false; m_functions_cache[function_name] = NULL; // remember that this script is unavailable return; @@ -306,6 +318,9 @@ namespace Scripting // Retrieve the return value from the context here (for scripts that return values) // returnValue = ctx->getReturnType(); for example //float returnValue = ctx->GetReturnFloat(); + + if (get_return_value) + get_return_value(ctx); } // We must release the contexts when no longer using them diff --git a/src/scriptengine/script_engine.hpp b/src/scriptengine/script_engine.hpp index 5a7e01e88..d3b304178 100644 --- a/src/scriptengine/script_engine.hpp +++ b/src/scriptengine/script_engine.hpp @@ -53,7 +53,11 @@ namespace Scripting ~ScriptEngine(); void runFunction(std::string function_name); - void runFunction(std::string function_name, std::function callback); + void runFunction(std::string function_name, + std::function callback); + void runFunction(std::string function_name, + std::function callback, + std::function get_return_value); void evalScript(std::string script_fragment); void cleanupCache(); diff --git a/src/tracks/track.cpp b/src/tracks/track.cpp index b5d447bb1..42b9c87b8 100644 --- a/src/tracks/track.cpp +++ b/src/tracks/track.cpp @@ -1116,6 +1116,8 @@ bool Track::loadMainTrack(const XMLNode &root) // some static meshes are conditional std::string condition; n->get("if", &condition); + + // TODO: convert "if" and "ifnot" to scripting. if (condition == "splatting") { if (!irr_driver->supportsSplatting()) continue; @@ -1193,15 +1195,15 @@ bool Track::loadMainTrack(const XMLNode &root) if (!shown) continue; } - else if (condition == "allchallenges") - { - // allow ONE unsolved challenge : the last one - if (getNumOfCompletedChallenges() < m_challenges.size() - 1) - continue; - } else if (condition.size() > 0) { - Log::error("track", "Unknown condition <%s>\n", condition.c_str()); + unsigned char result = -1; + Scripting::ScriptEngine* script_engine = World::getWorld()->getScriptEngine(); + std::function null_callback; + script_engine->runFunction("bool " + condition + "()", null_callback, + [&](asIScriptContext* ctx) { result = ctx->GetReturnByte(); }); + if (result == 0) + continue; } std::string neg_condition; @@ -1210,18 +1212,18 @@ bool Track::loadMainTrack(const XMLNode &root) { if (irr_driver->supportsSplatting()) continue; } - else if (neg_condition == "allchallenges") - { - // allow ONE unsolved challenge : the last one - if (getNumOfCompletedChallenges() >= m_challenges.size() - 1) - continue; - } else if (neg_condition.size() > 0) { - Log::error("track", "Unknown condition <%s>\n", - neg_condition.c_str()); + unsigned char result = -1; + Scripting::ScriptEngine* script_engine = World::getWorld()->getScriptEngine(); + std::function null_callback; + script_engine->runFunction("bool " + neg_condition + "()", null_callback, + [&](asIScriptContext* ctx) { result = ctx->GetReturnByte(); }); + if (result != 0) + continue; } + bool tangent = false; n->get("tangents", &tangent);