More work on scripting, test ways to pass arguments to script functions
This commit is contained in:
parent
585d1c5016
commit
1559d03ea1
@ -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();
|
||||
|
@ -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<void(asIScriptContext*)> 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<void(asIScriptContext*)> 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();
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
@ -21,6 +21,7 @@
|
||||
|
||||
#include <string>
|
||||
#include <angelscript.h>
|
||||
#include <functional>
|
||||
|
||||
class TrackObjectPresentation;
|
||||
|
||||
@ -52,10 +53,12 @@ namespace Scripting
|
||||
class ScriptEngine
|
||||
{
|
||||
public:
|
||||
|
||||
ScriptEngine();
|
||||
~ScriptEngine();
|
||||
|
||||
void runScript(std::string scriptName);
|
||||
void runScript(std::string scriptName, std::function<void(asIScriptContext*)> callback);
|
||||
void cleanupCache();
|
||||
|
||||
private:
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user