Skip to content

Commit d764e5e

Browse files
committed
👼Script: 🧹 Fixed return codes of script-manip funcs.
Changes: * added + registered `enum ScriptRetCode` - used by all script manipulation funcs (add/get/delete | funcs/variables) * added func `scriptVariableExists()` to go with `scriptFunctionExists()` * added internal helper `validateScriptModule`- used by all script manipulation funcs (add/get/delete | funcs/variables) Following is analysis of former state (most but not all funcs returned 0 on success) ~ executeString() [script: not exported] - used to return 0 on success, 1 on internal error and negative number otherwise. ~ addFunction() [script: `game.addScriptFunction()`] - used to return 0 on success, 1 on internal error and negative number otherwise. ~ functionExists() [script: `game.scriptFunctionExists()`] - used to return function ID (always >0) on success, negative number on error. ~ deleteFunction() [script: `game.deleteScriptFunction()`] - used to return function ID (always >0) on success, negative number on error. ~ addVariable() [script: `game.addScriptVariable()` ] - used to return 0 on success, 1 on internal error and negative number otherwise. ~ variableExists() [script: `game.scriptVariableExists()`] - newly added, returns 0 on success and negative number otherwise. ~ deleteVariable() [script: `game.deleteScriptVariable()` ] - used to return 0 on success, 1 on internal error and negative number otherwise. ~ getVariable() [script: `game.getScriptVariable()` ] - recently added, returns 0 on success and negative number otherwise.
1 parent aa9a423 commit d764e5e

File tree

8 files changed

+416
-264
lines changed

8 files changed

+416
-264
lines changed

doc/angelscript/Script2Game/GameScriptClass.h

+30-12
Original file line numberDiff line numberDiff line change
@@ -274,41 +274,59 @@ class GameScriptClass
274274
/**
275275
* Adds a global function to the script.
276276
* @param func the function to be added, e.g.: "void func() { log('works'); }"
277+
* @param nid ScriptUnitID to act upon, or -2 to fallback to the terrain script (defined in .terrn2 [Scripts], or 'default.as')
278+
* @return 0 on success, negative number on error.
277279
*/
278-
int addScriptFunction(const string func);
280+
ScriptRetCode addScriptFunction(const string func, ScriptUnitId_t nid = -2);
279281

280282
/**
281283
* Checks if a global function exists
282284
* @param func the declaration of the function that should be checked for existance, e.g.: "void func()"
285+
* @param nid ScriptUnitID to act upon, or -2 to fallback to the terrain script (defined in .terrn2 [Scripts], or 'default.as')
286+
* @return 0 on success, negative number on error.
283287
*/
284-
int scriptFunctionExists(const string func);
288+
ScriptRetCode scriptFunctionExists(const string func, ScriptUnitId_t nid = -2);
285289

286290
/**
287291
* Removes a global function from the script.
288292
* @param func the declaration of the function that should be removed, e.g.: "void func()"
293+
* @param nid ScriptUnitID to act upon, or -2 to fallback to the terrain script (defined in .terrn2 [Scripts], or 'default.as')
294+
* @return 0 on success, negative number on error.
289295
*/
290-
int deleteScriptFunction(const string func);
296+
ScriptRetCode deleteScriptFunction(const string func, ScriptUnitId_t nid = -2);
291297

292298
/**
293299
* Adds a global variable to the script.
294300
* @param var the declaration of the variable that should be added, e.g.: "int missionState;"
301+
* @param nid ScriptUnitID to act upon, or -2 to fallback to the terrain script (defined in .terrn2 [Scripts], or 'default.as')
302+
* @return 0 on success, negative number on error.
295303
*/
296-
int addScriptVariable(const string var);
304+
ScriptRetCode addScriptVariable(const string var, ScriptUnitId_t nid = -2);
305+
306+
/**
307+
* Checks if a global variable exists in the script.
308+
* @param var the declaration of the variable that should be removed, e.g.: "int missionState;"
309+
* @param nid ScriptUnitID to act upon, or -2 to fallback to the terrain script (defined in .terrn2 [Scripts], or 'default.as')
310+
* @return 0 on success, negative number on error.
311+
*/
312+
ScriptRetCode scriptVariableExists(const string var, ScriptUnitId_t nid = -2);
297313

298314
/**
299315
* Removes a global variable from the script.
300316
* @param var the declaration of the variable that should be removed, e.g.: "int missionState;"
317+
* @param nid ScriptUnitID to act upon, or -2 to fallback to the terrain script (defined in .terrn2 [Scripts], or 'default.as')
318+
* @return 0 on success, negative number on error.
301319
*/
302-
int deleteScriptVariable(const string var);
320+
ScriptRetCode deleteScriptVariable(const string var, ScriptUnitId_t nid = -2);
303321

304322
/**
305-
* Retrieves a memory address of a global variable in any script.
306-
* @param nid ScriptUnitID, ID of the running script, obtain one from global var `thisScript` or `game.getRunningScripts()`
307-
* @param varName Name of the variable. Type must match the reference type.
308-
* @param ref A variable-type parameter - accepts any reference.
309-
* @return 0 on success, negative number on error.
310-
*/
311-
int getScriptVariable(ScriptUnitId_t nid, const string&in varName, ?&ref);
323+
* Retrieves a memory address of a global variable in any script.
324+
* @param nid ScriptUnitID to act upon, or -2 to fallback to the terrain script (defined in .terrn2 [Scripts], or 'default.as')
325+
* @param varName Name of the variable. Type must match the reference type.
326+
* @param ref A variable-type parameter - accepts any reference.
327+
* @return 0 on success, negative number on error.
328+
*/
329+
ScriptRetCode getScriptVariable(const string&in varName, ?&ref, ScriptUnitId_t nid = -2);
312330

313331
/**
314332
* Clears the event cache

doc/angelscript/Script2Game/globals.h

+45
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,51 @@ enum MsgType
666666
MSG_EDI_CREATE_PROJECT_REQUESTED, //!< Creates a subdir under 'projects/', pre-populates it and adds to modcache. Params: 'name' (string), 'ext' (string, optional), 'source_entry' (CacheEntryClass@)
667667
};
668668

669+
/// Binding of `RoR::ScriptRetCode`; Common return codes for script manipulation funcs (add/get/delete | funcs/variables)
670+
enum ScriptRetCode
671+
{
672+
673+
SCRIPTRETCODE_SUCCESS = AngelScript::asSUCCESS, //!< Generic success - 0 by common convention.
674+
675+
// AngelScript technical codes
676+
SCRIPTRETCODE_AS_ERROR = AngelScript::asERROR,
677+
SCRIPTRETCODE_AS_CONTEXT_ACTIVE = AngelScript::asCONTEXT_ACTIVE,
678+
SCRIPTRETCODE_AS_CONTEXT_NOT_FINISHED = AngelScript::asCONTEXT_NOT_FINISHED,
679+
SCRIPTRETCODE_AS_CONTEXT_NOT_PREPARED = AngelScript::asCONTEXT_NOT_PREPARED,
680+
SCRIPTRETCODE_AS_INVALID_ARG = AngelScript::asINVALID_ARG,
681+
SCRIPTRETCODE_AS_NO_FUNCTION = AngelScript::asNO_FUNCTION,
682+
SCRIPTRETCODE_AS_NOT_SUPPORTED = AngelScript::asNOT_SUPPORTED,
683+
SCRIPTRETCODE_AS_INVALID_NAME = AngelScript::asINVALID_NAME,
684+
SCRIPTRETCODE_AS_NAME_TAKEN = AngelScript::asNAME_TAKEN,
685+
SCRIPTRETCODE_AS_INVALID_DECLARATION = AngelScript::asINVALID_DECLARATION,
686+
SCRIPTRETCODE_AS_INVALID_OBJECT = AngelScript::asINVALID_OBJECT,
687+
SCRIPTRETCODE_AS_INVALID_TYPE = AngelScript::asINVALID_TYPE,
688+
SCRIPTRETCODE_AS_ALREADY_REGISTERED = AngelScript::asALREADY_REGISTERED,
689+
SCRIPTRETCODE_AS_MULTIPLE_FUNCTIONS = AngelScript::asMULTIPLE_FUNCTIONS,
690+
SCRIPTRETCODE_AS_NO_MODULE = AngelScript::asNO_MODULE,
691+
SCRIPTRETCODE_AS_NO_GLOBAL_VAR = AngelScript::asNO_GLOBAL_VAR,
692+
SCRIPTRETCODE_AS_INVALID_CONFIGURATION = AngelScript::asINVALID_CONFIGURATION,
693+
SCRIPTRETCODE_AS_INVALID_INTERFACE = AngelScript::asINVALID_INTERFACE,
694+
SCRIPTRETCODE_AS_CANT_BIND_ALL_FUNCTIONS = AngelScript::asCANT_BIND_ALL_FUNCTIONS,
695+
SCRIPTRETCODE_AS_LOWER_ARRAY_DIMENSION_NOT_REGISTERED = AngelScript::asLOWER_ARRAY_DIMENSION_NOT_REGISTERED,
696+
SCRIPTRETCODE_AS_WRONG_CONFIG_GROUP = AngelScript::asWRONG_CONFIG_GROUP,
697+
SCRIPTRETCODE_AS_CONFIG_GROUP_IS_IN_USE = AngelScript::asCONFIG_GROUP_IS_IN_USE,
698+
SCRIPTRETCODE_AS_ILLEGAL_BEHAVIOUR_FOR_TYPE = AngelScript::asILLEGAL_BEHAVIOUR_FOR_TYPE,
699+
SCRIPTRETCODE_AS_WRONG_CALLING_CONV = AngelScript::asWRONG_CALLING_CONV,
700+
SCRIPTRETCODE_AS_BUILD_IN_PROGRESS = AngelScript::asBUILD_IN_PROGRESS,
701+
SCRIPTRETCODE_AS_INIT_GLOBAL_VARS_FAILED = AngelScript::asINIT_GLOBAL_VARS_FAILED,
702+
SCRIPTRETCODE_AS_OUT_OF_MEMORY = AngelScript::asOUT_OF_MEMORY,
703+
SCRIPTRETCODE_AS_MODULE_IS_IN_USE = AngelScript::asMODULE_IS_IN_USE,
704+
705+
// RoR ScriptEngine return codes
706+
SCRIPTRETCODE_UNSPECIFIED_ERROR = -1001,
707+
SCRIPTRETCODE_ENGINE_NOT_CREATED = -1002,
708+
SCRIPTRETCODE_CONTEXT_NOT_CREATED = -1003,
709+
SCRIPTRETCODE_SCRIPTUNIT_NOT_EXISTS = -1004,
710+
SCRIPTRETCODE_SCRIPTUNIT_NO_MODULE = -1005,
711+
SCRIPTRETCODE_FUNCTION_NOT_EXISTS = -1006,
712+
};
713+
669714
} // namespace Script2Game
670715

671716
/// @} //addtogroup Script2Game

source/main/ForwardDeclarations.h

+3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ namespace RoR
3939

4040
typedef int ScriptUnitID_t; //!< Unique sequentially generated ID of a loaded and running scriptin session. Use `ScriptEngine::getScriptUnit()`
4141
static const ScriptUnitID_t SCRIPTUNITID_INVALID = -1;
42+
static const ScriptUnitID_t SCRIPTUNITID_DEFAULT = -2; //!< The script defined in .terrn2 [Scripts], or 'default.as' ~ classic behavior.
4243

4344
typedef int PointidID_t; //!< index to `PointColDetector::hit_pointid_list`, use `RoR::POINTIDID_INVALID` as empty value.
4445
static const PointidID_t POINTIDID_INVALID = -1;
@@ -74,6 +75,8 @@ namespace RoR
7475
typedef int CommandkeyID_t; //!< Index into `Actor::ar_commandkeys` (BEWARE: indexed 1-MAX_COMMANDKEYS, 0 is invalid value, negative subscript of any size is acceptable, see `class CmdKeyArray` ).
7576
static const CommandkeyID_t COMMANDKEYID_INVALID = 0;
7677

78+
typedef int ScriptRetCode_t; //!< see enum `RoR::ScriptRetCode` - negative numbers are AngelScript codes, positive are RoR codes.
79+
7780
class Actor;
7881
class ActorManager;
7982
class ActorSpawner;

source/main/scripting/GameScript.cpp

+17-12
Original file line numberDiff line numberDiff line change
@@ -946,34 +946,39 @@ void GameScript::boostCurrentTruck(float factor)
946946
}
947947
}
948948

949-
int GameScript::addScriptFunction(const String& arg)
949+
int GameScript::addScriptFunction(const String& arg, ScriptUnitID_t nid)
950950
{
951-
return App::GetScriptEngine()->addFunction(arg);
951+
return App::GetScriptEngine()->addFunction(arg, nid);
952952
}
953953

954-
int GameScript::scriptFunctionExists(const String& arg)
954+
int GameScript::scriptFunctionExists(const String& arg, ScriptUnitID_t nid)
955955
{
956-
return App::GetScriptEngine()->functionExists(arg);
956+
return App::GetScriptEngine()->functionExists(arg, nid);
957957
}
958958

959-
int GameScript::deleteScriptFunction(const String& arg)
959+
int GameScript::deleteScriptFunction(const String& arg, ScriptUnitID_t nid)
960960
{
961-
return App::GetScriptEngine()->deleteFunction(arg);
961+
return App::GetScriptEngine()->deleteFunction(arg, nid);
962962
}
963963

964-
int GameScript::addScriptVariable(const String& arg)
964+
int GameScript::addScriptVariable(const String& arg, ScriptUnitID_t nid)
965965
{
966-
return App::GetScriptEngine()->addVariable(arg);
966+
return App::GetScriptEngine()->addVariable(arg, nid);
967967
}
968968

969-
int GameScript::deleteScriptVariable(const String& arg)
969+
int GameScript::scriptVariableExists(const String& arg, ScriptUnitID_t nid)
970970
{
971-
return App::GetScriptEngine()->deleteVariable(arg);
971+
return App::GetScriptEngine()->variableExists(arg, nid);
972972
}
973973

974-
int GameScript::getScriptVariable(ScriptUnitID_t nid, const Ogre::String& varName, void *ref, int refTypeId)
974+
int GameScript::deleteScriptVariable(const String& arg, ScriptUnitID_t nid)
975975
{
976-
return App::GetScriptEngine()->getVariable(nid, varName, ref, refTypeId);
976+
return App::GetScriptEngine()->deleteVariable(arg, nid);
977+
}
978+
979+
int GameScript::getScriptVariable(const Ogre::String& varName, void *ref, int refTypeId, ScriptUnitID_t nid)
980+
{
981+
return App::GetScriptEngine()->getVariable(varName, ref, refTypeId, nid);
977982
}
978983

979984
int GameScript::sendGameCmd(const String& message)

source/main/scripting/GameScript.h

+13-6
Original file line numberDiff line numberDiff line change
@@ -221,35 +221,42 @@ class GameScript
221221
* (Wrapper for ScriptEngine::addFunction)
222222
* @param arg A declaration for the function.
223223
*/
224-
int addScriptFunction(const Ogre::String& arg);
224+
ScriptRetCode_t addScriptFunction(const Ogre::String& arg, ScriptUnitID_t nid);
225225

226226
/**
227227
* Checks if a global function exists in the script
228228
* (Wrapper for ScriptEngine::functionExists)
229229
* @param arg A declaration for the function.
230230
*/
231-
int scriptFunctionExists(const Ogre::String& arg);
231+
ScriptRetCode_t scriptFunctionExists(const Ogre::String& arg, ScriptUnitID_t nid);
232232

233233
/**
234234
* Deletes a global function from the script
235235
* (Wrapper for ScriptEngine::deleteFunction)
236236
* @param arg A declaration for the function.
237237
*/
238-
int deleteScriptFunction(const Ogre::String& arg);
238+
ScriptRetCode_t deleteScriptFunction(const Ogre::String& arg, ScriptUnitID_t nid);
239239

240240
/**
241241
* Adds a global variable to the script
242242
* (Wrapper for ScriptEngine::addVariable)
243243
* @param arg A declaration for the variable.
244244
*/
245-
int addScriptVariable(const Ogre::String& arg);
245+
ScriptRetCode_t addScriptVariable(const Ogre::String& arg, ScriptUnitID_t nid);
246+
247+
/**
248+
* Adds a global variable to the script
249+
* (Wrapper for ScriptEngine::variableExists)
250+
* @param arg A declaration for the variable.
251+
*/
252+
ScriptRetCode_t scriptVariableExists(const Ogre::String& arg, ScriptUnitID_t nid);
246253

247254
/**
248255
* Deletes a global variable from the script
249256
* (Wrapper for ScriptEngine::deleteVariable)
250257
* @param arg A declaration for the variable.
251258
*/
252-
int deleteScriptVariable(const Ogre::String& arg);
259+
ScriptRetCode_t deleteScriptVariable(const Ogre::String& arg, ScriptUnitID_t nid);
253260

254261
/**
255262
* Retrieves a memory address of a global variable in any script.
@@ -259,7 +266,7 @@ class GameScript
259266
* @param refTypeId Type of the reference; To be registered as variable-type parameter `?&out`
260267
* @return 0 on success, negative number on error.
261268
*/
262-
int getScriptVariable(ScriptUnitID_t nid, const Ogre::String& varName, void *ref, int refTypeId);
269+
ScriptRetCode_t getScriptVariable(const Ogre::String& varName, void *ref, int refTypeId, ScriptUnitID_t nid);
263270

264271
void clearEventCache();
265272

0 commit comments

Comments
 (0)