From ef8225711085237a1d327b7d25acee13a170bc72 Mon Sep 17 00:00:00 2001 From: samuncle Date: Thu, 19 Jan 2017 20:49:46 -0500 Subject: [PATCH 1/3] Add support for array in scripting --- src/scriptengine/script_engine.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/scriptengine/script_engine.cpp b/src/scriptengine/script_engine.cpp index 5f2d05171..fd049a258 100644 --- a/src/scriptengine/script_engine.cpp +++ b/src/scriptengine/script_engine.cpp @@ -31,6 +31,7 @@ #include "scriptengine/script_utils.hpp" #include "scriptengine/scriptstdstring.hpp" #include "scriptengine/scriptvec3.hpp" +#include "scriptengine/scriptarray.hpp" #include #include "states_screens/dialogs/tutorial_message_dialog.hpp" #include "tracks/track_object_manager.hpp" @@ -428,6 +429,7 @@ namespace Scripting // Register the script string type RegisterStdString(engine); //register std::string RegisterVec3(engine); //register Vec3 + RegisterScriptArray(engine, true); Scripting::Track::registerScriptFunctions(m_engine); Scripting::Challenges::registerScriptFunctions(m_engine); From 52fff152def9e39c5be78e2ec1d61525bc042e2b Mon Sep 17 00:00:00 2001 From: "auria.mg" Date: Thu, 19 Jan 2017 21:03:09 -0500 Subject: [PATCH 2/3] Scripting : add support for "onStart" callbacks in library objects --- src/scriptengine/script_engine.cpp | 19 ++++++++++++++++--- src/tracks/track_object_presentation.cpp | 24 ++++++++++++++++++++++++ src/tracks/track_object_presentation.hpp | 3 +++ 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/src/scriptengine/script_engine.cpp b/src/scriptengine/script_engine.cpp index 5f2d05171..388c9d0fb 100644 --- a/src/scriptengine/script_engine.cpp +++ b/src/scriptengine/script_engine.cpp @@ -31,6 +31,7 @@ #include "scriptengine/script_utils.hpp" #include "scriptengine/scriptstdstring.hpp" #include "scriptengine/scriptvec3.hpp" +#include "scriptengine/scriptarray.hpp" #include #include "states_screens/dialogs/tutorial_message_dialog.hpp" #include "tracks/track_object_manager.hpp" @@ -307,9 +308,20 @@ namespace Scripting // Find the function for the function we want to execute. // This is how you call a normal function with arguments // asIScriptFunction *func = engine->GetModule(0)->GetFunctionByDecl("void func(arg1Type, arg2Type)"); - func = m_engine->GetModule(MODULE_ID_MAIN_SCRIPT_FILE) - ->GetFunctionByDecl(function_name.c_str()); - + asIScriptModule* module = m_engine->GetModule(MODULE_ID_MAIN_SCRIPT_FILE); + + if (module == NULL) + { + if (warn_if_not_found) + Log::warn("Scripting", "Scripting function was not found : %s (module not found)", function_name.c_str()); + else + Log::debug("Scripting", "Scripting function was not found : %s (module not found)", function_name.c_str()); + m_functions_cache[function_name] = NULL; // remember that this function is unavailable + return; + } + + func = module->GetFunctionByDecl(function_name.c_str()); + if (func == NULL) { if (warn_if_not_found) @@ -428,6 +440,7 @@ namespace Scripting // Register the script string type RegisterStdString(engine); //register std::string RegisterVec3(engine); //register Vec3 + RegisterScriptArray(engine, true); Scripting::Track::registerScriptFunctions(m_engine); Scripting::Challenges::registerScriptFunctions(m_engine); diff --git a/src/tracks/track_object_presentation.cpp b/src/tracks/track_object_presentation.cpp index 79f079077..92de0d093 100644 --- a/src/tracks/track_object_presentation.cpp +++ b/src/tracks/track_object_presentation.cpp @@ -180,6 +180,7 @@ TrackObjectPresentationLibraryNode::TrackObjectPresentationLibraryNode( { std::string name; xml_node.get("name", &name); + m_name = name; m_node = irr_driver->getSceneManager()->addEmptySceneNode(); #ifdef DEBUG @@ -210,13 +211,17 @@ TrackObjectPresentationLibraryNode::TrackObjectPresentationLibraryNode( lib_path = track->getTrackFile("library/" + name); libroot = file_manager->createXMLTree(local_lib_node_path); if (track != NULL) + { Scripting::ScriptEngine::getInstance()->loadScript(local_script_file_path, false); + } } else if (file_manager->fileExists(lib_node_path)) { libroot = file_manager->createXMLTree(lib_node_path); if (track != NULL) + { Scripting::ScriptEngine::getInstance()->loadScript(lib_script_file_path, false); + } } else { @@ -272,6 +277,25 @@ TrackObjectPresentationLibraryNode::TrackObjectPresentationLibraryNode( m_parent = parent; } // TrackObjectPresentationLibraryNode +// ---------------------------------------------------------------------------- + +void TrackObjectPresentationLibraryNode::update(float dt) +{ + if (!m_start_executed) + { + m_start_executed = true; + std::string fn_name = StringUtils::insertValues("void %s::onStart(const string)", m_name.c_str()); + + std::string lib_id = m_parent->getID(); + std::string* lib_id_ptr = &lib_id; + + Scripting::ScriptEngine::getInstance()->runFunction(false, fn_name, + [&](asIScriptContext* ctx) { + ctx->SetArgObject(0, lib_id_ptr); + }); + } +} + // ---------------------------------------------------------------------------- TrackObjectPresentationLibraryNode::~TrackObjectPresentationLibraryNode() { diff --git a/src/tracks/track_object_presentation.hpp b/src/tracks/track_object_presentation.hpp index 97def6f66..db479ab74 100644 --- a/src/tracks/track_object_presentation.hpp +++ b/src/tracks/track_object_presentation.hpp @@ -186,11 +186,14 @@ class TrackObjectPresentationLibraryNode : public TrackObjectPresentationSceneNo { TrackObject* m_parent; using TrackObjectPresentationSceneNode::move; + std::string m_name; + bool m_start_executed = false; public: TrackObjectPresentationLibraryNode(TrackObject* parent, const XMLNode& xml_node, ModelDefinitionLoader& model_def_loader); virtual ~TrackObjectPresentationLibraryNode(); + virtual void update(float dt); void move(const core::vector3df& xyz, const core::vector3df& hpr, const core::vector3df& scale, bool isAbsoluteCoord, bool moveChildrenPhysicalBodies); }; // TrackObjectPresentationLibraryNode From 7234caffd9199b13cee3f7c8d58d59f93a079d1e Mon Sep 17 00:00:00 2001 From: "auria.mg" Date: Thu, 19 Jan 2017 21:11:44 -0500 Subject: [PATCH 3/3] Fix travis build --- src/tracks/track_object_presentation.cpp | 2 ++ src/tracks/track_object_presentation.hpp | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/tracks/track_object_presentation.cpp b/src/tracks/track_object_presentation.cpp index 92de0d093..7824338d2 100644 --- a/src/tracks/track_object_presentation.cpp +++ b/src/tracks/track_object_presentation.cpp @@ -178,6 +178,8 @@ TrackObjectPresentationLibraryNode::TrackObjectPresentationLibraryNode( ModelDefinitionLoader& model_def_loader) : TrackObjectPresentationSceneNode(xml_node) { + m_start_executed = false; + std::string name; xml_node.get("name", &name); m_name = name; diff --git a/src/tracks/track_object_presentation.hpp b/src/tracks/track_object_presentation.hpp index db479ab74..000ee5071 100644 --- a/src/tracks/track_object_presentation.hpp +++ b/src/tracks/track_object_presentation.hpp @@ -187,7 +187,7 @@ class TrackObjectPresentationLibraryNode : public TrackObjectPresentationSceneNo TrackObject* m_parent; using TrackObjectPresentationSceneNode::move; std::string m_name; - bool m_start_executed = false; + bool m_start_executed; public: TrackObjectPresentationLibraryNode(TrackObject* parent, const XMLNode& xml_node,