RigsofRods
Soft-body Physics Simulation
Public Member Functions | Protected Attributes | Friends
RoR::ScriptEngine Class Reference

This class represents the angelscript scripting interface. More...

#include <ScriptEngine.h>

+ Inheritance diagram for RoR::ScriptEngine:
+ Collaboration diagram for RoR::ScriptEngine:

Public Member Functions

 ScriptEngine ()
 
 ~ScriptEngine ()
 
ScriptUnitId_t loadScript (Ogre::String scriptname, ScriptCategory category=ScriptCategory::TERRAIN, ActorPtr associatedActor=nullptr, std::string buffer="")
 Loads a script. More...
 
void unloadScript (ScriptUnitId_t unique_id)
 Unloads a script. More...
 
void framestep (Ogre::Real dt)
 Calls the script's framestep function to be able to use timed things inside the script. More...
 
void setForwardScriptLogToConsole (bool doForward)
 
void triggerEvent (scriptEvents eventnum, int arg1=0, int arg2ex=0, int arg3ex=0, int arg4ex=0, std::string arg5ex="", std::string arg6ex="", std::string arg7ex="", std::string arg8ex="")
 triggers an event; Not to be used by the end-user. More...
 
void setEventsEnabled (bool val)
 
int executeString (Ogre::String command)
 executes a string (useful for the console) More...
 
void queueStringForExecution (const Ogre::String command)
 Queues a string for execution. More...
 
int addFunction (const Ogre::String &arg)
 Adds a global function to the script. More...
 
int functionExists (const Ogre::String &arg)
 Checks if a global function exists. More...
 
int deleteFunction (const Ogre::String &arg)
 Deletes a global function from the script. More...
 
int addVariable (const Ogre::String &arg)
 Adds a global variable to the script. More...
 
int deleteVariable (const Ogre::String &arg)
 Deletes a global variable from the script. More...
 
AngelScript::asIScriptFunction * getFunctionByDeclAndLogCandidates (ScriptUnitId_t nid, GetFuncFlags_t flags, const std::string &funcName, const std::string &fmtFuncDecl)
 Finds a function by full declaration, and if not found, finds candidates by name and logs them to Angelscript.log. More...
 
int fireEvent (std::string instanceName, float intensity)
 
void envokeCallback (int functionId, eventsource_t *source, NodeNum_t nodenum=NODENUM_INVALID, int type=0)
 
void forwardExceptionAsScriptEvent (const std::string &from)
 Forwards useful info from C++ try{}catch{} exceptions to script in the form of game event. More...
 
AngelScript::asIScriptEngine * getEngine ()
 
void messageLogged (const Ogre::String &message, Ogre::LogMessageLevel lml, bool maskDebug, const Ogre::String &logName, bool &skipThisMessage)
 
bool scriptUnitExists (ScriptUnitId_t unique_id)
 
ScriptUnitgetScriptUnit (ScriptUnitId_t unique_id)
 
ScriptUnitMap const & getScriptUnits () const
 

Protected Member Functions

Housekeeping
void init ()
 This function initialzies the engine and registeres all types. More...
 
Ogre::String composeModuleName (Ogre::String const &scriptName, ScriptCategory origin, ScriptUnitId_t id)
 Packs name + important info to one string, for logging and reporting purposes. More...
 
int setupScriptUnit (int unit_id)
 Helper for loadScript(), does the actual building without worry about unit management. More...
 
bool prepareContextAndHandleErrors (ScriptUnitId_t nid, int asFunctionID)
 Helper for executing any script function/snippet; does asIScriptContext::Prepare() and reports any error. More...
 
int executeContextAndHandleErrors (ScriptUnitId_t nid)
 Helper for executing any script function/snippet; registers Line/Exception callbacks (on demand) and set currently executed NID; The asIScriptContext::Prepare() and setting args must be already done. More...
 
Script diagnostics
void msgCallback (const AngelScript::asSMessageInfo *msg)
 Optional (but very recommended!) callback providing diagnostic info when things fail to start (most notably script errors). More...
 
void lineCallback (AngelScript::asIScriptContext *ctx)
 Optional callback which receives diagnostic info for every executed statement. More...
 
void exceptionCallback (AngelScript::asIScriptContext *ctx)
 Optional callback invoked when the script critically fails, allowing debugging. More...
 

Protected Attributes

AngelScript::asIScriptEngine * engine
 instance of the scripting engine More...
 
AngelScript::asIScriptContext * context
 context in which all scripting happens More...
 
Ogre::LogscriptLog
 
GameScript m_game_script
 
ScriptUnitMap m_script_units
 
ScriptUnitId_t m_terrain_script_unit = SCRIPTUNITID_INVALID
 
ScriptUnitId_t m_currently_executing_script_unit = SCRIPTUNITID_INVALID
 
scriptEvents m_currently_executing_event_trigger = SE_NO_EVENTS
 
bool m_events_enabled = true
 Hack to enable fast shutdown without cleanup. More...
 
InterThreadStoreVector< Ogre::String > stringExecutionQueue
 The string execution queue. More...
 

Friends

class GameScript
 

Detailed Description

This class represents the angelscript scripting interface.

It can load and execute scripts.

Authors
Thomas Fischer (thomas{AT}rigsofrods{DOT}com)

Definition at line 119 of file ScriptEngine.h.

Constructor & Destructor Documentation

◆ ScriptEngine()

ScriptEngine::ScriptEngine ( )

Definition at line 97 of file ScriptEngine.cpp.

+ Here is the call graph for this function:

◆ ~ScriptEngine()

ScriptEngine::~ScriptEngine ( )

Definition at line 106 of file ScriptEngine.cpp.

Member Function Documentation

◆ addFunction()

int ScriptEngine::addFunction ( const Ogre::String &  arg)

Adds a global function to the script.

Parameters
argA declaration for the function.

Definition at line 501 of file ScriptEngine.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ addVariable()

int ScriptEngine::addVariable ( const Ogre::String &  arg)

Adds a global variable to the script.

Parameters
argA declaration for the variable.

Definition at line 639 of file ScriptEngine.cpp.

+ Here is the caller graph for this function:

◆ composeModuleName()

String ScriptEngine::composeModuleName ( Ogre::String const &  scriptName,
ScriptCategory  origin,
ScriptUnitId_t  id 
)
protected

Packs name + important info to one string, for logging and reporting purposes.

Definition at line 754 of file ScriptEngine.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ deleteFunction()

int ScriptEngine::deleteFunction ( const Ogre::String &  arg)

Deletes a global function from the script.

Parameters
argA declaration for the function.

Definition at line 585 of file ScriptEngine.cpp.

+ Here is the caller graph for this function:

◆ deleteVariable()

int ScriptEngine::deleteVariable ( const Ogre::String &  arg)

Deletes a global variable from the script.

Parameters
argA declaration for the variable.

Definition at line 659 of file ScriptEngine.cpp.

+ Here is the caller graph for this function:

◆ envokeCallback()

void ScriptEngine::envokeCallback ( int  functionId,
eventsource_t source,
NodeNum_t  nodenum = NODENUM_INVALID,
int  type = 0 
)

Definition at line 443 of file ScriptEngine.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ exceptionCallback()

void ScriptEngine::exceptionCallback ( AngelScript::asIScriptContext *  ctx)
protected

Optional callback invoked when the script critically fails, allowing debugging.

https://www.angelcode.com/angelscript/sdk/docs/manual/doc_call_script_func.html#doc_call_script_4

Definition at line 233 of file ScriptEngine.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ executeContextAndHandleErrors()

int ScriptEngine::executeContextAndHandleErrors ( ScriptUnitId_t  nid)
protected

Helper for executing any script function/snippet; registers Line/Exception callbacks (on demand) and set currently executed NID; The asIScriptContext::Prepare() and setting args must be already done.

Returns
0 on success, anything else on error.

Definition at line 277 of file ScriptEngine.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ executeString()

int ScriptEngine::executeString ( Ogre::String  command)

executes a string (useful for the console)

Parameters
commandstring to execute

Definition at line 483 of file ScriptEngine.cpp.

+ Here is the caller graph for this function:

◆ fireEvent()

int ScriptEngine::fireEvent ( std::string  instanceName,
float  intensity 
)

Definition at line 413 of file ScriptEngine.cpp.

+ Here is the caller graph for this function:

◆ forwardExceptionAsScriptEvent()

void ScriptEngine::forwardExceptionAsScriptEvent ( const std::string &  from)

Forwards useful info from C++ try{}catch{} exceptions to script in the form of game event.

AngelScript doesn't have exceptions in this sense (in AS jargon, 'Exception' means basically 'panic' as in Lua/Rust...) and most exceptions this game encounters (Ogre::Exception) are trivially recoverable, so it doesn't make sense to panic AngelScript when they happen.

Definition at line 246 of file ScriptEngine.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ framestep()

void ScriptEngine::framestep ( Ogre::Real  dt)

Calls the script's framestep function to be able to use timed things inside the script.

Parameters
dttime passed since the last call to this function in seconds

Definition at line 384 of file ScriptEngine.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ functionExists()

int ScriptEngine::functionExists ( const Ogre::String &  arg)

Checks if a global function exists.

Parameters
argA declaration for the function.

Definition at line 560 of file ScriptEngine.cpp.

+ Here is the caller graph for this function:

◆ getEngine()

AngelScript::asIScriptEngine* RoR::ScriptEngine::getEngine ( )
inline

Definition at line 223 of file ScriptEngine.h.

+ Here is the caller graph for this function:

◆ getFunctionByDeclAndLogCandidates()

asIScriptFunction * ScriptEngine::getFunctionByDeclAndLogCandidates ( ScriptUnitId_t  nid,
GetFuncFlags_t  flags,
const std::string &  funcName,
const std::string &  fmtFuncDecl 
)

Finds a function by full declaration, and if not found, finds candidates by name and logs them to Angelscript.log.

Returns
Angelscript function on success, null on error.

Definition at line 690 of file ScriptEngine.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getScriptUnit()

ScriptUnit & ScriptEngine::getScriptUnit ( ScriptUnitId_t  unique_id)

Definition at line 1000 of file ScriptEngine.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getScriptUnits()

ScriptUnitMap const& RoR::ScriptEngine::getScriptUnits ( ) const
inline

Definition at line 235 of file ScriptEngine.h.

+ Here is the caller graph for this function:

◆ init()

void ScriptEngine::init ( )
protected

This function initialzies the engine and registeres all types.

Definition at line 119 of file ScriptEngine.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ lineCallback()

void ScriptEngine::lineCallback ( AngelScript::asIScriptContext *  ctx)
protected

Optional callback which receives diagnostic info for every executed statement.

https://www.angelcode.com/angelscript/sdk/docs/manual/classas_i_script_context.html#ae2747f643bf9a07364f922c460ef57dd

Definition at line 215 of file ScriptEngine.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ loadScript()

ScriptUnitId_t ScriptEngine::loadScript ( Ogre::String  scriptname,
ScriptCategory  category = ScriptCategory::TERRAIN,
ActorPtr  associatedActor = nullptr,
std::string  buffer = "" 
)

Loads a script.

Parameters
scriptnamefilename to load; if buffer is supplied, this is only a display name.
categoryHow to treat the script?
associatedActorOnly for category ACTOR
bufferString with full script body; if empty, a file will be loaded as usual.
Returns
Unique ID of the script unit (because one script file can be loaded multiple times).

Definition at line 759 of file ScriptEngine.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ messageLogged()

void ScriptEngine::messageLogged ( const Ogre::String &  message,
Ogre::LogMessageLevel  lml,
bool  maskDebug,
const Ogre::String &  logName,
bool &  skipThisMessage 
)

Definition at line 113 of file ScriptEngine.cpp.

+ Here is the call graph for this function:

◆ msgCallback()

void ScriptEngine::msgCallback ( const AngelScript::asSMessageInfo *  msg)
protected

Optional (but very recommended!) callback providing diagnostic info when things fail to start (most notably script errors).

Definition at line 196 of file ScriptEngine.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ prepareContextAndHandleErrors()

bool ScriptEngine::prepareContextAndHandleErrors ( ScriptUnitId_t  nid,
int  asFunctionID 
)
protected

Helper for executing any script function/snippet; does asIScriptContext::Prepare() and reports any error.

Returns
true on success, false on error.

Definition at line 364 of file ScriptEngine.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ queueStringForExecution()

void ScriptEngine::queueStringForExecution ( const Ogre::String  command)

Queues a string for execution.

Use this when you want to execute a script statement from another thread.

Parameters
commandstring to queue for execution
See also
executeString

Definition at line 478 of file ScriptEngine.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ scriptUnitExists()

bool ScriptEngine::scriptUnitExists ( ScriptUnitId_t  unique_id)

Definition at line 994 of file ScriptEngine.cpp.

+ Here is the caller graph for this function:

◆ setEventsEnabled()

void RoR::ScriptEngine::setEventsEnabled ( bool  val)
inline

Definition at line 159 of file ScriptEngine.h.

+ Here is the caller graph for this function:

◆ setForwardScriptLogToConsole()

void ScriptEngine::setForwardScriptLogToConsole ( bool  doForward)

Definition at line 982 of file ScriptEngine.cpp.

+ Here is the caller graph for this function:

◆ setupScriptUnit()

int ScriptEngine::setupScriptUnit ( int  unit_id)
protected

Helper for loadScript(), does the actual building without worry about unit management.

Returns
0 on success, anything else on error.

Definition at line 808 of file ScriptEngine.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ triggerEvent()

void ScriptEngine::triggerEvent ( scriptEvents  eventnum,
int  arg1 = 0,
int  arg2ex = 0,
int  arg3ex = 0,
int  arg4ex = 0,
std::string  arg5ex = "",
std::string  arg6ex = "",
std::string  arg7ex = "",
std::string  arg8ex = "" 
)

triggers an event; Not to be used by the end-user.

Runs either eventCallbackEx(), if registered, or eventCallback(), if registered, in this order.

Definition at line 712 of file ScriptEngine.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ unloadScript()

void ScriptEngine::unloadScript ( ScriptUnitId_t  unique_id)

Unloads a script.

Parameters
unique_idThe script unit ID as returned by loadScript()

Definition at line 964 of file ScriptEngine.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Friends And Related Function Documentation

◆ GameScript

friend class GameScript
friend

Definition at line 121 of file ScriptEngine.h.

Field Documentation

◆ context

AngelScript::asIScriptContext* RoR::ScriptEngine::context
protected

context in which all scripting happens

Definition at line 295 of file ScriptEngine.h.

◆ engine

AngelScript::asIScriptEngine* RoR::ScriptEngine::engine
protected

instance of the scripting engine

Definition at line 294 of file ScriptEngine.h.

◆ m_currently_executing_event_trigger

scriptEvents RoR::ScriptEngine::m_currently_executing_event_trigger = SE_NO_EVENTS
protected

Definition at line 301 of file ScriptEngine.h.

◆ m_currently_executing_script_unit

ScriptUnitId_t RoR::ScriptEngine::m_currently_executing_script_unit = SCRIPTUNITID_INVALID
protected

Definition at line 300 of file ScriptEngine.h.

◆ m_events_enabled

bool RoR::ScriptEngine::m_events_enabled = true
protected

Hack to enable fast shutdown without cleanup.

Definition at line 302 of file ScriptEngine.h.

◆ m_game_script

GameScript RoR::ScriptEngine::m_game_script
protected

Definition at line 297 of file ScriptEngine.h.

◆ m_script_units

ScriptUnitMap RoR::ScriptEngine::m_script_units
protected

Definition at line 298 of file ScriptEngine.h.

◆ m_terrain_script_unit

ScriptUnitId_t RoR::ScriptEngine::m_terrain_script_unit = SCRIPTUNITID_INVALID
protected

Definition at line 299 of file ScriptEngine.h.

◆ scriptLog

Ogre::Log* RoR::ScriptEngine::scriptLog
protected

Definition at line 296 of file ScriptEngine.h.

◆ stringExecutionQueue

InterThreadStoreVector<Ogre::String> RoR::ScriptEngine::stringExecutionQueue
protected

The string execution queue.

See also
queueStringForExecution

Definition at line 304 of file ScriptEngine.h.


The documentation for this class was generated from the following files: