From 1559d03ea130cd3be4827329244fe963037be4d9 Mon Sep 17 00:00:00 2001 From: Marianne Gagnon Date: Thu, 30 Apr 2015 20:24:02 -0400 Subject: [PATCH] More work on scripting, test ways to pass arguments to script functions --- src/physics/physics.cpp | 6 ++-- src/scriptengine/script_engine.cpp | 38 ++++++++++++++---------- src/scriptengine/script_engine.hpp | 5 +++- src/scriptengine/script_track.cpp | 4 +-- src/tracks/track.cpp | 8 +++-- src/tracks/track_object_presentation.cpp | 16 +++++----- 6 files changed, 45 insertions(+), 32 deletions(-) diff --git a/src/physics/physics.cpp b/src/physics/physics.cpp index 056044f7d..0ad5d29e3 100644 --- a/src/physics/physics.cpp +++ b/src/physics/physics.cpp @@ -173,7 +173,7 @@ void Physics::update(float dt) int kartid2 = p->getUserPointer(1)->getPointerKart()->getWorldKartId(); // TODO: do not use globals this way, pass directly as function paramters Scripting::Physics::setCollision(kartid1,kartid2); - script_engine->runScript("onKartKartCollision"); + script_engine->runScript("void onKartKartCollision()"); continue; } // if kart-kart collision @@ -188,7 +188,7 @@ void Physics::update(float dt) p->getUserPointer(0)->getPointerPhysicalObject()->getID(), "kart" ); - script_engine->runScript("onKartObjectCollision"); + script_engine->runScript("void onKartObjectCollision()"); PhysicalObject *obj = p->getUserPointer(0) ->getPointerPhysicalObject(); if (obj->isCrashReset()) @@ -261,7 +261,7 @@ void Physics::update(float dt) p->getUserPointer(1)->getPointerPhysicalObject()->getID(), "item" ); - script_engine->runScript("onItemObjectCollision"); + script_engine->runScript("void onItemObjectCollision()"); p->getUserPointer(0)->getPointerFlyable() ->hit(NULL, p->getUserPointer(1)->getPointerPhysicalObject()); PhysicalObject* obj = p->getUserPointer(1)->getPointerPhysicalObject(); diff --git a/src/scriptengine/script_engine.cpp b/src/scriptengine/script_engine.cpp index 13aa1ac42..885bd22a2 100644 --- a/src/scriptengine/script_engine.cpp +++ b/src/scriptengine/script_engine.cpp @@ -114,24 +114,25 @@ std::string getScript(std::string scriptName) } //----------------------------------------------------------------------------- + /** runs the specified script * \param string scriptName = name of script to run */ void ScriptEngine::runScript(std::string scriptName) { - PROFILER_PUSH_CPU_MARKER("RunScript", 255, 0, 0); + std::function callback; + runScript(scriptName, callback); +} - // Create a context that will execute the script. - asIScriptContext *ctx = m_engine->CreateContext(); - if (ctx == NULL) - { - Log::error("Scripting", "Failed to create the context."); - //m_engine->Release(); - return; - } +//----------------------------------------------------------------------------- + +/** runs the specified script +* \param string scriptName = name of script to run +*/ +void ScriptEngine::runScript(std::string scriptName, std::function callback) +{ int r; //int for error checking - asIScriptFunction *func; auto cached_script = m_script_cache.find(scriptName); if (cached_script == m_script_cache.end()) @@ -143,7 +144,6 @@ void ScriptEngine::runScript(std::string scriptName) { Log::debug("Scripting", "Script '%s' is not available", scriptName.c_str()); m_script_cache[scriptName] = NULL; // remember that this script is unavailable - ctx->Release(); return; } @@ -156,7 +156,6 @@ void ScriptEngine::runScript(std::string scriptName) { Log::debug("Scripting", "Scripting function was not found : %s", scriptName.c_str()); m_script_cache[scriptName] = NULL; // remember that this script is unavailable - ctx->Release(); return; } @@ -172,10 +171,19 @@ void ScriptEngine::runScript(std::string scriptName) if (func == NULL) { - PROFILER_POP_CPU_MARKER(); return; // script unavailable } + + // Create a context that will execute the script. + asIScriptContext *ctx = m_engine->CreateContext(); + if (ctx == NULL) + { + Log::error("Scripting", "Failed to create the context."); + //m_engine->Release(); + return; + } + // Prepare the script context with the function we wish to execute. Prepare() // must be called on the context before each new script function that will be // executed. Note, that if because we intend to execute the same function @@ -194,6 +202,8 @@ void ScriptEngine::runScript(std::string scriptName) //ctx->setArgType(index, value); //for example : ctx->SetArgFloat(0, 3.14159265359f); + if (callback) + callback(ctx); // Execute the function r = ctx->Execute(); @@ -230,8 +240,6 @@ void ScriptEngine::runScript(std::string scriptName) // We must release the contexts when no longer using them ctx->Release(); - - PROFILER_POP_CPU_MARKER(); } //----------------------------------------------------------------------------- diff --git a/src/scriptengine/script_engine.hpp b/src/scriptengine/script_engine.hpp index 67b145b38..41c2c65cd 100644 --- a/src/scriptengine/script_engine.hpp +++ b/src/scriptengine/script_engine.hpp @@ -21,6 +21,7 @@ #include #include +#include class TrackObjectPresentation; @@ -48,14 +49,16 @@ namespace Scripting asIScriptFunction* registerScriptCallbacks(asIScriptEngine *engine, std::string scriptName); } - + class ScriptEngine { public: + ScriptEngine(); ~ScriptEngine(); void runScript(std::string scriptName); + void runScript(std::string scriptName, std::function callback); void cleanupCache(); private: diff --git a/src/scriptengine/script_track.cpp b/src/scriptengine/script_track.cpp index 090bc9e18..7cfb2602b 100644 --- a/src/scriptengine/script_track.cpp +++ b/src/scriptengine/script_track.cpp @@ -39,10 +39,10 @@ namespace Scripting namespace Track { //register callbacks - asIScriptFunction* registerScriptCallbacks(asIScriptEngine *engine, std::string scriptName) + // TODO: move this out of Track namespace, it's not specific to tracks + asIScriptFunction* registerScriptCallbacks(asIScriptEngine *engine, std::string function_name) { asIScriptFunction *func; - std::string function_name = "void " + scriptName + "()"; func = engine->GetModule(0)->GetFunctionByDecl(function_name.c_str()); return func; } diff --git a/src/tracks/track.cpp b/src/tracks/track.cpp index 49fe9b9bd..deaf53440 100644 --- a/src/tracks/track.cpp +++ b/src/tracks/track.cpp @@ -1486,7 +1486,7 @@ void Track::update(float dt) if (!m_startup_run) // first time running update = good point to run startup script { Scripting::ScriptEngine* script_engine = World::getWorld()->getScriptEngine(); - script_engine->runScript("onStart"); + script_engine->runScript("void onStart()"); m_startup_run = true; } m_track_object_manager->update(dt); @@ -1497,8 +1497,10 @@ void Track::update(float dt) } CheckManager::get()->update(dt); ItemManager::get()->update(dt); - Scripting::ScriptEngine* script_engine = World::getWorld()->getScriptEngine(); - script_engine->runScript("onUpdate"); + + // TODO: enable onUpdate scripts if we ever find a compelling use for them + //Scripting::ScriptEngine* script_engine = World::getWorld()->getScriptEngine(); + //script_engine->runScript("void onUpdate()"); } // update // ---------------------------------------------------------------------------- diff --git a/src/tracks/track_object_presentation.cpp b/src/tracks/track_object_presentation.cpp index 308ecc17f..9780c1242 100644 --- a/src/tracks/track_object_presentation.cpp +++ b/src/tracks/track_object_presentation.cpp @@ -22,6 +22,7 @@ #include "audio/sfx_buffer.hpp" #include "challenges/unlock_manager.hpp" #include "config/user_config.hpp" +#include "graphics/camera.hpp" #include "graphics/irr_driver.hpp" #include "graphics/material_manager.hpp" #include "graphics/mesh_tools.hpp" @@ -34,6 +35,7 @@ #include "input/input_device.hpp" #include "input/input_manager.hpp" #include "items/item_manager.hpp" +#include "karts/abstract_kart.hpp" #include "modes/world.hpp" #include "scriptengine/script_engine.hpp" #include "states_screens/dialogs/race_paused_dialog.hpp" @@ -932,13 +934,11 @@ void TrackObjectPresentationActionTrigger::onTriggerItemApproached(Item* who) Scripting::ScriptEngine* script_engine = World::getWorld()->getScriptEngine(); m_action_active = false; - script_engine->runScript(m_action); - - /* - Catch exception -> script not found - fprintf(stderr, "[TrackObject] WARNING: unknown action <%s>\n", - m_action.c_str()); - - */ + int idKart = 0; + Camera* camera = Camera::getActiveCamera(); + if (camera != NULL && camera->getKart() != NULL) + idKart = camera->getKart()->getWorldKartId(); + script_engine->runScript("void " + m_action + "(int)", + [=](asIScriptContext* ctx) { ctx->SetArgDWord(0, idKart); }); } } // onTriggerItemApproached