Moved scripts to stk-code/data/scripts and provided access to them via file_manager
This commit is contained in:
parent
4578ddc073
commit
3c67a6159f
@ -118,6 +118,7 @@ FileManager::FileManager()
|
|||||||
m_subdir_name[MUSIC ] = "music";
|
m_subdir_name[MUSIC ] = "music";
|
||||||
m_subdir_name[TRANSLATION] = "po";
|
m_subdir_name[TRANSLATION] = "po";
|
||||||
m_subdir_name[TEXTURE ] = "textures";
|
m_subdir_name[TEXTURE ] = "textures";
|
||||||
|
m_subdir_name[SCRIPT ] = "scripts";
|
||||||
m_subdir_name[SFX ] = "sfx";
|
m_subdir_name[SFX ] = "sfx";
|
||||||
m_subdir_name[SKIN ] = "skins";
|
m_subdir_name[SKIN ] = "skins";
|
||||||
m_subdir_name[SHADER ] = "shaders";
|
m_subdir_name[SHADER ] = "shaders";
|
||||||
|
@ -49,8 +49,8 @@ public:
|
|||||||
enum AssetType {ASSET_MIN,
|
enum AssetType {ASSET_MIN,
|
||||||
CHALLENGE=ASSET_MIN,
|
CHALLENGE=ASSET_MIN,
|
||||||
FONT, GFX, GRANDPRIX, GUI, MODEL, MUSIC,
|
FONT, GFX, GRANDPRIX, GUI, MODEL, MUSIC,
|
||||||
SFX, SHADER, SKIN, TEXTURE, TRANSLATION,
|
SCRIPT, SFX, SHADER, SKIN, TEXTURE,
|
||||||
ASSET_MAX = TRANSLATION,
|
TRANSLATION, ASSET_MAX = TRANSLATION,
|
||||||
ASSET_COUNT};
|
ASSET_COUNT};
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
@ -1,28 +0,0 @@
|
|||||||
This is currently only a tiny prototype to get an idea of what the scripting engine can do/ check it's performance.
|
|
||||||
TODO:
|
|
||||||
get file manager support for loading scripts
|
|
||||||
move scripts to stk-data, etc.
|
|
||||||
|
|
||||||
|
|
||||||
Steps to get this working.
|
|
||||||
|
|
||||||
==>change stk-code/src/scriptengine/scriptengine.cpp line 254 as appropriate
|
|
||||||
(add the required directory)
|
|
||||||
Build the game.
|
|
||||||
Play the tutorial
|
|
||||||
Test by changing the strings in stk-code/src/tracks/tutorial_bananas.as
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Steps for creating new scripts/ actions
|
|
||||||
Add an action trigger to the relevant track (Either add it on blender and export it / set it up in scene.xml)
|
|
||||||
Create a new script with the defined action scene.xml for the action trigger created. (For example, for tutorial_bananas =>
|
|
||||||
|
|
||||||
<object type="action-trigger" action="tutorial_bananas" distance="7.0" xyz="67.90 0.86 99.49" hpr="0.0 -0.0 0.0" scale="7.00 7.00 7.00"/>
|
|
||||||
|
|
||||||
action is "tutorial_bananas"
|
|
||||||
so the script it calls is, stk/stk-code/scriptengine/tutorial_bananas.as
|
|
||||||
|
|
||||||
Make sure the method onTrigger() is defined. Check tutorial_bananas.as for more details
|
|
||||||
|
|
@ -27,6 +27,7 @@
|
|||||||
#include "tracks/track_object_manager.hpp"
|
#include "tracks/track_object_manager.hpp"
|
||||||
#include "tracks/track.hpp"
|
#include "tracks/track.hpp"
|
||||||
#include "karts/kart.hpp"
|
#include "karts/kart.hpp"
|
||||||
|
#include "io/file_manager.hpp"
|
||||||
|
|
||||||
using namespace irr;
|
using namespace irr;
|
||||||
|
|
||||||
@ -80,6 +81,32 @@ void squashKart(asIScriptGeneric *gen){
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
std::string getScript(std::string scriptName){
|
||||||
|
std::string script_dir = file_manager->getAsset(FileManager::SCRIPT, "");
|
||||||
|
|
||||||
|
script_dir += scriptName + ".as";
|
||||||
|
FILE *f = fopen(script_dir.c_str(), "rb");
|
||||||
|
if( f == 0 )
|
||||||
|
{
|
||||||
|
std::cout << "Failed to open the script file " + scriptName + ".as" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Determine the size of the file
|
||||||
|
fseek(f, 0, SEEK_END);
|
||||||
|
int len = ftell(f);
|
||||||
|
fseek(f, 0, SEEK_SET);
|
||||||
|
|
||||||
|
// Read the entire file
|
||||||
|
std::string script;
|
||||||
|
script.resize(len);
|
||||||
|
int c = fread(&script[0], len, 1, f);
|
||||||
|
fclose(f);
|
||||||
|
if( c == 0 )
|
||||||
|
{
|
||||||
|
std::cout << "Failed to load script file." << std::endl;
|
||||||
|
}
|
||||||
|
return script;
|
||||||
|
}
|
||||||
void ScriptEngine::runScript(std::string scriptName)
|
void ScriptEngine::runScript(std::string scriptName)
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -204,42 +231,14 @@ int ScriptEngine::compileScript(asIScriptEngine *engine, std::string scriptName)
|
|||||||
{
|
{
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
// For now we will load the script directtly from a file on the disk.
|
std::string script = getScript(scriptName);
|
||||||
//TODO use filemanager to do this.
|
|
||||||
//std::string load_dir = "D:\\Github\\stk\\stk-code\\src\\scriptengine\\";
|
|
||||||
//std::string load_dir = "//media//New Volume//Github//stk//stk-code//src//scriptengine//";
|
|
||||||
std::string load_dir = "..//..//src//scriptengine//";
|
|
||||||
load_dir += scriptName + ".as";
|
|
||||||
FILE *f = fopen(load_dir.c_str(), "rb");
|
|
||||||
if( f == 0 )
|
|
||||||
{
|
|
||||||
std::cout << "Failed to open the script file " + scriptName + ".as" << std::endl;
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Determine the size of the file
|
|
||||||
fseek(f, 0, SEEK_END);
|
|
||||||
int len = ftell(f);
|
|
||||||
fseek(f, 0, SEEK_SET);
|
|
||||||
|
|
||||||
// Read the entire file
|
|
||||||
std::string script;
|
|
||||||
script.resize(len);
|
|
||||||
int c = fread(&script[0], len, 1, f);
|
|
||||||
fclose(f);
|
|
||||||
if( c == 0 )
|
|
||||||
{
|
|
||||||
std::cout << "Failed to load script file." << std::endl;
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add the script sections that will be compiled into executable code.
|
// Add the script sections that will be compiled into executable code.
|
||||||
// If we want to combine more than one file into the same script, then
|
// If we want to combine more than one file into the same script, then
|
||||||
// we can call AddScriptSection() several times for the same module and
|
// we can call AddScriptSection() several times for the same module and
|
||||||
// the script engine will treat them all as if they were one. The script
|
// the script engine will treat them all as if they were one. The script
|
||||||
// section name, will allow us to localize any errors in the script code.
|
// section name, will allow us to localize any errors in the script code.
|
||||||
asIScriptModule *mod = engine->GetModule(0, asGM_ALWAYS_CREATE);
|
asIScriptModule *mod = engine->GetModule(0, asGM_ALWAYS_CREATE);
|
||||||
r = mod->AddScriptSection("script", &script[0], len);
|
r = mod->AddScriptSection("script", &script[0], script.size());
|
||||||
if( r < 0 )
|
if( r < 0 )
|
||||||
{
|
{
|
||||||
std::cout << "AddScriptSection() failed" << std::endl;
|
std::cout << "AddScriptSection() failed" << std::endl;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user