More work on scripting, test ways to pass arguments to script functions

This commit is contained in:
Marianne Gagnon 2015-04-30 20:24:02 -04:00
parent 585d1c5016
commit 1559d03ea1
6 changed files with 45 additions and 32 deletions

View File

@ -173,7 +173,7 @@ void Physics::update(float dt)
int kartid2 = p->getUserPointer(1)->getPointerKart()->getWorldKartId(); int kartid2 = p->getUserPointer(1)->getPointerKart()->getWorldKartId();
// TODO: do not use globals this way, pass directly as function paramters // TODO: do not use globals this way, pass directly as function paramters
Scripting::Physics::setCollision(kartid1,kartid2); Scripting::Physics::setCollision(kartid1,kartid2);
script_engine->runScript("onKartKartCollision"); script_engine->runScript("void onKartKartCollision()");
continue; continue;
} // if kart-kart collision } // if kart-kart collision
@ -188,7 +188,7 @@ void Physics::update(float dt)
p->getUserPointer(0)->getPointerPhysicalObject()->getID(), p->getUserPointer(0)->getPointerPhysicalObject()->getID(),
"kart" "kart"
); );
script_engine->runScript("onKartObjectCollision"); script_engine->runScript("void onKartObjectCollision()");
PhysicalObject *obj = p->getUserPointer(0) PhysicalObject *obj = p->getUserPointer(0)
->getPointerPhysicalObject(); ->getPointerPhysicalObject();
if (obj->isCrashReset()) if (obj->isCrashReset())
@ -261,7 +261,7 @@ void Physics::update(float dt)
p->getUserPointer(1)->getPointerPhysicalObject()->getID(), p->getUserPointer(1)->getPointerPhysicalObject()->getID(),
"item" "item"
); );
script_engine->runScript("onItemObjectCollision"); script_engine->runScript("void onItemObjectCollision()");
p->getUserPointer(0)->getPointerFlyable() p->getUserPointer(0)->getPointerFlyable()
->hit(NULL, p->getUserPointer(1)->getPointerPhysicalObject()); ->hit(NULL, p->getUserPointer(1)->getPointerPhysicalObject());
PhysicalObject* obj = p->getUserPointer(1)->getPointerPhysicalObject(); PhysicalObject* obj = p->getUserPointer(1)->getPointerPhysicalObject();

View File

@ -114,24 +114,25 @@ std::string getScript(std::string scriptName)
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/** runs the specified script /** runs the specified script
* \param string scriptName = name of script to run * \param string scriptName = name of script to run
*/ */
void ScriptEngine::runScript(std::string scriptName) void ScriptEngine::runScript(std::string scriptName)
{ {
PROFILER_PUSH_CPU_MARKER("RunScript", 255, 0, 0); std::function<void(asIScriptContext*)> callback;
runScript(scriptName, callback);
}
// Create a context that will execute the script. //-----------------------------------------------------------------------------
asIScriptContext *ctx = m_engine->CreateContext();
if (ctx == NULL) /** runs the specified script
{ * \param string scriptName = name of script to run
Log::error("Scripting", "Failed to create the context."); */
//m_engine->Release(); void ScriptEngine::runScript(std::string scriptName, std::function<void(asIScriptContext*)> callback)
return; {
}
int r; //int for error checking int r; //int for error checking
asIScriptFunction *func; asIScriptFunction *func;
auto cached_script = m_script_cache.find(scriptName); auto cached_script = m_script_cache.find(scriptName);
if (cached_script == m_script_cache.end()) 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()); Log::debug("Scripting", "Script '%s' is not available", scriptName.c_str());
m_script_cache[scriptName] = NULL; // remember that this script is unavailable m_script_cache[scriptName] = NULL; // remember that this script is unavailable
ctx->Release();
return; return;
} }
@ -156,7 +156,6 @@ void ScriptEngine::runScript(std::string scriptName)
{ {
Log::debug("Scripting", "Scripting function was not found : %s", scriptName.c_str()); Log::debug("Scripting", "Scripting function was not found : %s", scriptName.c_str());
m_script_cache[scriptName] = NULL; // remember that this script is unavailable m_script_cache[scriptName] = NULL; // remember that this script is unavailable
ctx->Release();
return; return;
} }
@ -172,10 +171,19 @@ void ScriptEngine::runScript(std::string scriptName)
if (func == NULL) if (func == NULL)
{ {
PROFILER_POP_CPU_MARKER();
return; // script unavailable 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() // 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 // 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 // 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); //ctx->setArgType(index, value);
//for example : ctx->SetArgFloat(0, 3.14159265359f); //for example : ctx->SetArgFloat(0, 3.14159265359f);
if (callback)
callback(ctx);
// Execute the function // Execute the function
r = ctx->Execute(); r = ctx->Execute();
@ -230,8 +240,6 @@ void ScriptEngine::runScript(std::string scriptName)
// We must release the contexts when no longer using them // We must release the contexts when no longer using them
ctx->Release(); ctx->Release();
PROFILER_POP_CPU_MARKER();
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------

View File

@ -21,6 +21,7 @@
#include <string> #include <string>
#include <angelscript.h> #include <angelscript.h>
#include <functional>
class TrackObjectPresentation; class TrackObjectPresentation;
@ -52,10 +53,12 @@ namespace Scripting
class ScriptEngine class ScriptEngine
{ {
public: public:
ScriptEngine(); ScriptEngine();
~ScriptEngine(); ~ScriptEngine();
void runScript(std::string scriptName); void runScript(std::string scriptName);
void runScript(std::string scriptName, std::function<void(asIScriptContext*)> callback);
void cleanupCache(); void cleanupCache();
private: private:

View File

@ -39,10 +39,10 @@ namespace Scripting
namespace Track namespace Track
{ {
//register callbacks //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; asIScriptFunction *func;
std::string function_name = "void " + scriptName + "()";
func = engine->GetModule(0)->GetFunctionByDecl(function_name.c_str()); func = engine->GetModule(0)->GetFunctionByDecl(function_name.c_str());
return func; return func;
} }

View File

@ -1486,7 +1486,7 @@ void Track::update(float dt)
if (!m_startup_run) // first time running update = good point to run startup script if (!m_startup_run) // first time running update = good point to run startup script
{ {
Scripting::ScriptEngine* script_engine = World::getWorld()->getScriptEngine(); Scripting::ScriptEngine* script_engine = World::getWorld()->getScriptEngine();
script_engine->runScript("onStart"); script_engine->runScript("void onStart()");
m_startup_run = true; m_startup_run = true;
} }
m_track_object_manager->update(dt); m_track_object_manager->update(dt);
@ -1497,8 +1497,10 @@ void Track::update(float dt)
} }
CheckManager::get()->update(dt); CheckManager::get()->update(dt);
ItemManager::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 } // update
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------

View File

@ -22,6 +22,7 @@
#include "audio/sfx_buffer.hpp" #include "audio/sfx_buffer.hpp"
#include "challenges/unlock_manager.hpp" #include "challenges/unlock_manager.hpp"
#include "config/user_config.hpp" #include "config/user_config.hpp"
#include "graphics/camera.hpp"
#include "graphics/irr_driver.hpp" #include "graphics/irr_driver.hpp"
#include "graphics/material_manager.hpp" #include "graphics/material_manager.hpp"
#include "graphics/mesh_tools.hpp" #include "graphics/mesh_tools.hpp"
@ -34,6 +35,7 @@
#include "input/input_device.hpp" #include "input/input_device.hpp"
#include "input/input_manager.hpp" #include "input/input_manager.hpp"
#include "items/item_manager.hpp" #include "items/item_manager.hpp"
#include "karts/abstract_kart.hpp"
#include "modes/world.hpp" #include "modes/world.hpp"
#include "scriptengine/script_engine.hpp" #include "scriptengine/script_engine.hpp"
#include "states_screens/dialogs/race_paused_dialog.hpp" #include "states_screens/dialogs/race_paused_dialog.hpp"
@ -932,13 +934,11 @@ void TrackObjectPresentationActionTrigger::onTriggerItemApproached(Item* who)
Scripting::ScriptEngine* script_engine = Scripting::ScriptEngine* script_engine =
World::getWorld()->getScriptEngine(); World::getWorld()->getScriptEngine();
m_action_active = false; m_action_active = false;
script_engine->runScript(m_action); int idKart = 0;
Camera* camera = Camera::getActiveCamera();
/* if (camera != NULL && camera->getKart() != NULL)
Catch exception -> script not found idKart = camera->getKart()->getWorldKartId();
fprintf(stderr, "[TrackObject] WARNING: unknown action <%s>\n", script_engine->runScript("void " + m_action + "(int)",
m_action.c_str()); [=](asIScriptContext* ctx) { ctx->SetArgDWord(0, idKart); });
*/
} }
} // onTriggerItemApproached } // onTriggerItemApproached