Skip to content
This repository was archived by the owner on Jan 5, 2024. It is now read-only.

Commit f8f8023

Browse files
committed
Added periods to MOSR method comments. These belong in the header, but I'll leave that be til we reformat MOSR completely.
Removed MovableMan::ScriptedEntity and just used LuaMan::TempEntity. They serve the same functionality and it makes more sense for that bridge between cpp and lua to be in LuaMan. Reorganized RunScriptedFunction methods, and simplified UpdateScripts and UpdateScriptedAI by pulling some logic out.
1 parent 3aff5e1 commit f8f8023

File tree

8 files changed

+76
-123
lines changed

8 files changed

+76
-123
lines changed

Entities/Actor.cpp

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,36 +1289,13 @@ bool Actor::UpdateAIScripted() {
12891289
return false;
12901290
}
12911291

1292-
// Check to make sure the preset of this is still defined in the Lua state. If not, re-create it and recover gracefully
1293-
if (!g_LuaMan.ExpressionIsTrue(m_ScriptPresetName, false)) {
1294-
RTEAbort("Trying to run UpdateAIScripted with no ScriptPresetName. This shouldn't happen, please let a dev know.")
1295-
//ReloadScripts(); //TODO test if this should be here, I think it's junk cause for any cases where there's AI, update will always be called first. If it shouldn't, change the early return above.
1296-
}
1297-
1298-
// If we don't have a Lua representation for this object instance, create one and call the Lua Create function on it
1299-
if (m_ScriptObjectName.empty()) {
1300-
RTEAbort("Trying to run UpdateAIScripted with no ScriptObjectName. This shouldn't happen, please let a dev know.")
1301-
/*//TODO test if this should be here, I think it's junk cause for any cases where there's AI, update will always be called first. If it shouldn't, change the early return above.
1302-
m_ScriptObjectName = GetClassName() + "s." + g_LuaMan.GetNewObjectID();
1303-
1304-
// Give Lua access to this object, then use that access to set up the object's Lua representation
1305-
g_MovableMan.SetScriptedEntity(this);
1306-
if (g_LuaMan.RunScriptString(m_ScriptObjectName + " = To" + GetClassName() + "(MovableMan.ScriptedEntity);") < 0) {
1307-
return false;
1308-
}
1309-
1310-
for (std::pair<std::string, bool> scriptEntry : m_LoadedScripts) {
1311-
if (g_LuaMan.RunFunctionInPresetScript("Create", scriptEntry.first, m_ScriptPresetName, m_ScriptObjectName) < 0) {
1312-
return -3;
1313-
}
1314-
}*/
1315-
}
1316-
1317-
g_FrameMan.StartPerformanceMeasurement(FrameMan::PERF_ACTORS_AI);
1318-
bool aiUpdateSucceeded = RunScriptedFunctionInAppropriateScripts("UpdateAI", false, true) < 0;
1319-
g_FrameMan.StopPerformanceMeasurement(FrameMan::PERF_ACTORS_AI);
1292+
int status = !g_LuaMan.ExpressionIsTrue(m_ScriptPresetName, false) ? ReloadScripts() : 0;
1293+
status = (status >= 0 && m_ScriptObjectName.empty()) ? SetupScriptObjectNameAndRunLuaCreateFunctions() : status;
1294+
g_FrameMan.StartPerformanceMeasurement(FrameMan::PERF_ACTORS_AI);
1295+
status = (status >= 0) ? RunScriptedFunctionInAppropriateScripts("UpdateAI", false, true) : status;
1296+
g_FrameMan.StopPerformanceMeasurement(FrameMan::PERF_ACTORS_AI);
13201297

1321-
return aiUpdateSucceeded;
1298+
return status >= 0;
13221299
}
13231300

13241301

Entities/MOSRotating.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1756,9 +1756,9 @@ void MOSRotating::UpdateChildMOIDs(vector<MovableObject *> &MOIDIndex,
17561756

17571757

17581758
/// <summary>
1759-
/// Attaches the passed in Attachable and adds it to the list of attachables, not changing its parent offset and not treating it as hardcoded
1759+
/// Attaches the passed in Attachable and adds it to the list of attachables, not changing its parent offset and not treating it as hardcoded.
17601760
/// </summary>
1761-
/// <param name="pAttachable">The Attachable to attach</param>
1761+
/// <param name="pAttachable">The Attachable to attach.</param>
17621762
void MOSRotating::AddAttachable(Attachable *pAttachable)
17631763
{
17641764
if (pAttachable)
@@ -1769,21 +1769,21 @@ void MOSRotating::AddAttachable(Attachable *pAttachable)
17691769

17701770

17711771
/// <summary>
1772-
/// Attaches the passed in Attachable and adds it to the list of attachables, changing its parent offset to the passed in Vector but not treating it as hardcoded
1772+
/// Attaches the passed in Attachable and adds it to the list of attachables, changing its parent offset to the passed in Vector but not treating it as hardcoded.
17731773
/// </summary>
1774-
/// <param name="pAttachable">The Attachable to add</param>
1775-
/// <param name="parentOffsetToSet">The vector to set as the Attachable's parent offset</param>
1774+
/// <param name="pAttachable">The Attachable to add.</param>
1775+
/// <param name="parentOffsetToSet">The vector to set as the Attachable's parent offset.</param>
17761776
void MOSRotating::AddAttachable(Attachable *pAttachable, const Vector& parentOffsetToSet)
17771777
{
17781778
AddAttachable(pAttachable, parentOffsetToSet, false);
17791779
}
17801780

17811781

17821782
/// <summary>
1783-
/// Attaches the passed in Attachable and adds it to the list of attachables, not changing its parent offset but treating it as hardcoded depending on the passed in boolean
1783+
/// Attaches the passed in Attachable and adds it to the list of attachables, not changing its parent offset but treating it as hardcoded depending on the passed in boolean.
17841784
/// </summary>
1785-
/// <param name="pAttachable">The Attachable to add</param>
1786-
/// <param name="isHardcodedAttachable">Whether or not the Attachable should be treated as hardcoded</param>
1785+
/// <param name="pAttachable">The Attachable to add.</param>
1786+
/// <param name="isHardcodedAttachable">Whether or not the Attachable should be treated as hardcoded.</param>
17871787
void MOSRotating::AddAttachable(Attachable *pAttachable, bool isHardcodedAttachable)
17881788
{
17891789
if (pAttachable)
@@ -1796,9 +1796,9 @@ void MOSRotating::AddAttachable(Attachable *pAttachable, bool isHardcodedAttacha
17961796
/// <summary>
17971797
/// Attaches the passed in Attachable and adds it to the list of attachables, changing its parent offset to the passed in Vector and treating it as hardcoded depending on the passed in boolean.
17981798
/// </summary>
1799-
/// <param name="pAttachable">The Attachable to add</param>
1800-
/// <param name="parentOffsetToSet">The vector to set as the Attachable's parent offset</param>
1801-
/// <param name="isHardcodedAttachable">Whether or not the Attachable should be treated as hardcoded</param>
1799+
/// <param name="pAttachable">The Attachable to add.</param>
1800+
/// <param name="parentOffsetToSet">The vector to set as the Attachable's parent offset.</param>
1801+
/// <param name="isHardcodedAttachable">Whether or not the Attachable should be treated as hardcoded.</param>
18021802
void MOSRotating::AddAttachable(Attachable *pAttachable, const Vector & parentOffsetToSet, bool isHardcodedAttachable)
18031803
{
18041804
if (pAttachable)

Entities/MovableObject.cpp

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,24 @@ void MovableObject::Destroy(bool notInherited) {
467467

468468
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
469469

470+
int MovableObject::SetupScriptObjectNameAndRunLuaCreateFunctions() {
471+
m_ScriptObjectName = GetClassName() + "s." + g_LuaMan.GetNewObjectID();
472+
473+
// Give Lua access to this object, then use that access to set up the object's Lua representation
474+
g_LuaMan.SetTempEntity(this);
475+
476+
if (g_LuaMan.RunScriptString(m_ScriptObjectName + " = To" + GetClassName() + "(LuaMan.TempEntity);") < 0) {
477+
return -2;
478+
}
479+
480+
if (RunScriptedFunctionInAppropriateScripts("Create", true, true) < 0) {
481+
return -3;
482+
}
483+
return 0;
484+
}
485+
486+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
487+
470488
int MovableObject::LoadScript(std::string const &scriptPath, bool loadAsEnabledScript) {
471489
// Return an error if the script path is empty or already there
472490
if (scriptPath.empty()) {
@@ -638,25 +656,6 @@ bool MovableObject::DisableScript(std::string const &scriptPath) {
638656

639657
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
640658

641-
int MovableObject::RunScriptedFunctionInAppropriateScripts(std::string const &functionName, bool runOnDisabledScripts, bool stopOnError, std::vector<Entity *> functionEntityArguments, std::vector<std::string> functionLiteralArguments) {
642-
if (m_LoadedScripts.empty() || m_ScriptPresetName.empty() || m_ScriptObjectName.empty()) {
643-
return -1;
644-
}
645-
646-
int status = 0;
647-
for (std::pair<std::string, bool> scriptEntry : m_LoadedScripts) {
648-
if (runOnDisabledScripts || scriptEntry.second == true) {
649-
status = RunScriptedFunction(scriptEntry.first, functionName, functionEntityArguments, functionLiteralArguments);
650-
if (status < 0 && stopOnError) {
651-
return status;
652-
}
653-
}
654-
}
655-
return status;
656-
}
657-
658-
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
659-
660659
int MovableObject::RunScriptedFunction(std::string const &scriptPath, std::string const &functionName, std::vector<Entity *> functionEntityArguments, std::vector<std::string> functionLiteralArguments) {
661660
if (m_LoadedScripts.empty() || m_ScriptPresetName.empty() || m_ScriptObjectName.empty()) {
662661
return -1;
@@ -694,6 +693,25 @@ int MovableObject::RunScriptedFunction(std::string const &scriptPath, std::strin
694693

695694
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
696695

696+
int MovableObject::RunScriptedFunctionInAppropriateScripts(std::string const &functionName, bool runOnDisabledScripts, bool stopOnError, std::vector<Entity *> functionEntityArguments, std::vector<std::string> functionLiteralArguments) {
697+
if (m_LoadedScripts.empty() || m_ScriptPresetName.empty() || m_ScriptObjectName.empty()) {
698+
return -1;
699+
}
700+
701+
int status = 0;
702+
for (std::pair<std::string, bool> scriptEntry : m_LoadedScripts) {
703+
if (runOnDisabledScripts || scriptEntry.second == true) {
704+
status = RunScriptedFunction(scriptEntry.first, functionName, functionEntityArguments, functionLiteralArguments);
705+
if (status < 0 && stopOnError) {
706+
return status;
707+
}
708+
}
709+
}
710+
return status;
711+
}
712+
713+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
714+
697715
/*
698716
//////////////////////////////////////////////////////////////////////////////////////////
699717
// Constructor: MovableObject
@@ -956,30 +974,11 @@ int MovableObject::UpdateScripts() {
956974
return -1;
957975
}
958976

959-
// Check to make sure the preset of this is still defined in the Lua state. If not, re-create it and recover gracefully
960-
if (!g_LuaMan.ExpressionIsTrue(m_ScriptPresetName, false)) { ReloadScripts(); }
961-
962-
// If we don't have a Lua representation for this object instance, create one and call the Lua Create function on it
963-
if (m_ScriptObjectName.empty()) {
964-
m_ScriptObjectName = GetClassName() + "s." + g_LuaMan.GetNewObjectID();
965-
966-
// Give Lua access to this object, then use that access to set up the object's Lua representation
967-
g_MovableMan.SetScriptedEntity(this);
977+
int status = !g_LuaMan.ExpressionIsTrue(m_ScriptPresetName, false) ? ReloadScripts() : 0;
978+
status = (status >= 0 && m_ScriptObjectName.empty()) ? SetupScriptObjectNameAndRunLuaCreateFunctions() : status;
979+
status = (status >= 0) ? RunScriptedFunctionInAppropriateScripts("Update", false, true) : status;
968980

969-
if (g_LuaMan.RunScriptString(m_ScriptObjectName + " = To" + GetClassName() + "(MovableMan.ScriptedEntity);") < 0) {
970-
return -2;
971-
}
972-
973-
if (RunScriptedFunctionInAppropriateScripts("Create", true, true) < 0) {
974-
return -3;
975-
}
976-
}
977-
978-
if (RunScriptedFunctionInAppropriateScripts("Update", false, true) < 0) {
979-
return -3;
980-
}
981-
982-
return 0;
981+
return status;
983982
}
984983

985984
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Entities/MovableObject.h

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -242,27 +242,27 @@ ENTITYALLOCATION(MovableObject)
242242
virtual bool DisableScript(std::string const &scriptPath);
243243

244244
/// <summary>
245-
/// Runs the given function in all scripts that have it, with the given arguments, with the ability to not run on disabled scripts and to cease running if there's an error.
246-
/// The first argument to the function will always be 'self'. If either argument list is not emtpy, its entries will be passed into the Lua function in order, with entity arguments first.
245+
/// Runs the given function for the given script, with the given arguments. The first argument to the function will always be 'self'.
246+
/// If either argument list is not emtpy, its entries will be passed into the Lua function in order, with entity arguments first.
247247
/// </summary>
248+
/// <param name="scriptPath">The path to the script to run.</param>
248249
/// <param name="functionName">The name of the function to run.</param>
249-
/// <param name="runOnDisabledScripts">Whether to run the function on disabled scripts. Defaults to false.</param>
250-
/// <param name="stopOnError">Whether to stop if there's an error running any script, or simply print it to the console and continue. Defaults to false.</param>
251250
/// <param name="functionEntityArguments">Optional vector of entity pointers that should be passed into the Lua function. Their internal Lua states will not be accessible. Defaults to empty.</param>
252251
/// <param name="functionLiteralArguments">Optional vector of strings, that should be passed into the Lua function. Entries must be surrounded with escaped quotes (i.e.`\"`) they'll be passed in as-is, allowing them to act as booleans, etc.. Defaults to empty.</param>
253252
/// <returns>An error return value signaling sucess or any particular failure. Anything below 0 is an error signal.</returns>
254-
int RunScriptedFunctionInAppropriateScripts(std::string const &functionName, bool runOnDisabledScripts = false, bool stopOnError = false, std::vector<Entity *> functionEntityArguments = std::vector<Entity *>(), std::vector<std::string> functionLiteralArguments = std::vector<std::string>());
253+
int RunScriptedFunction(std::string const &scriptPath, std::string const &functionName, std::vector<Entity *> functionEntityArguments = std::vector<Entity *>(), std::vector<std::string> functionLiteralArguments = std::vector<std::string>());
255254

256255
/// <summary>
257-
/// Runs the given function for the given script, with the given arguments. The first argument to the function will always be 'self'.
258-
/// If either argument list is not emtpy, its entries will be passed into the Lua function in order, with entity arguments first.
256+
/// Runs the given function in all scripts that have it, with the given arguments, with the ability to not run on disabled scripts and to cease running if there's an error.
257+
/// The first argument to the function will always be 'self'. If either argument list is not emtpy, its entries will be passed into the Lua function in order, with entity arguments first.
259258
/// </summary>
260-
/// <param name="scriptPath">The path to the script to run.</param>
261259
/// <param name="functionName">The name of the function to run.</param>
260+
/// <param name="runOnDisabledScripts">Whether to run the function on disabled scripts. Defaults to false.</param>
261+
/// <param name="stopOnError">Whether to stop if there's an error running any script, or simply print it to the console and continue. Defaults to false.</param>
262262
/// <param name="functionEntityArguments">Optional vector of entity pointers that should be passed into the Lua function. Their internal Lua states will not be accessible. Defaults to empty.</param>
263263
/// <param name="functionLiteralArguments">Optional vector of strings, that should be passed into the Lua function. Entries must be surrounded with escaped quotes (i.e.`\"`) they'll be passed in as-is, allowing them to act as booleans, etc.. Defaults to empty.</param>
264264
/// <returns>An error return value signaling sucess or any particular failure. Anything below 0 is an error signal.</returns>
265-
int RunScriptedFunction(std::string const &scriptPath, std::string const &functionName, std::vector<Entity *> functionEntityArguments = std::vector<Entity *>(), std::vector<std::string> functionLiteralArguments = std::vector<std::string>());
265+
int RunScriptedFunctionInAppropriateScripts(std::string const &functionName, bool runOnDisabledScripts = false, bool stopOnError = false, std::vector<Entity *> functionEntityArguments = std::vector<Entity *>(), std::vector<std::string> functionLiteralArguments = std::vector<std::string>());
266266

267267
//////////////////////////////////////////////////////////////////////////////////////////
268268
// Virtual method: GetClass
@@ -1776,6 +1776,12 @@ ENTITYALLOCATION(MovableObject)
17761776
/// <returns>A vector containing the script function names this class supports.</returns>
17771777
virtual const std::vector<std::string> GetSupportedScriptFunctionNames() { return std::vector<std::string> {"Create", "Destroy", "Update", "OnPieMenu", "OnScriptRemoveOrDisable", "OnScriptEnable"}; }
17781778

1779+
/// <summary>
1780+
/// Does necessary work to setup a script object name for this object, allowing it to be accessed in Lua, then runs all of the MO's scripts' Create functions in Lua.
1781+
/// </summary>
1782+
/// <returns>0 on success, -2 if it fails to setup the script object in Lua, and -3 if it fails to run any Create function.</returns>
1783+
int SetupScriptObjectNameAndRunLuaCreateFunctions();
1784+
17791785
//////////////////////////////////////////////////////////////////////////////////////////
17801786
// Virtual method: UpdateChildMOIDs
17811787
//////////////////////////////////////////////////////////////////////////////////////////

Managers/LuaMan.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,8 @@ void LuaMan::Clear()
394394
m_NextPresetID = 0;
395395
m_NextObjectID = 0;
396396
m_pTempEntity = 0;
397+
m_TempEntityVector.clear();
398+
m_TempEntityVector.shrink_to_fit();
397399

398400
//Clear files list
399401
for (int i = 0; i < MAX_OPEN_FILES; ++i)
@@ -2157,7 +2159,6 @@ int LuaMan::Create()
21572159
.def("GetAGResolution", &MovableMan::GetAGResolution)
21582160
.def("GetSplashRatio", &MovableMan::GetSplashRatio)
21592161
.property("MaxDroppedItems", &MovableMan::GetMaxDroppedItems, &MovableMan::SetMaxDroppedItems)
2160-
.property("ScriptedEntity", &MovableMan::GetScriptedEntity, &MovableMan::SetScriptedEntity)
21612162
.def("SortTeamRoster", &MovableMan::SortTeamRoster)
21622163
.def("ChangeActorTeam", &MovableMan::ChangeActorTeam)
21632164
.def("AddMO", &AddMO, adopt(_2))

Managers/LuaMan.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ enum ServerResult
401401
// The next unique object ID to hand out to the next scripted Entity instance that wants to run its preset's scripts
402402
// This gets incremented each time a new one is requested to give unique ID's to all scripted objects
403403
long m_NextObjectID;
404-
// Temporary holder for an Entity object that we want to pass into the Lua state without fuss
404+
// Temporary holder for an Entity object that we want to pass into the Lua state without fuss. Lets you export objects to lua easily.
405405
Entity *m_pTempEntity;
406406
// Temporary holder for a vector of Entities that we want to pass into the Lua state without a fuss. Usually used to pass arguments to special Lua functions.
407407
std::vector<Entity *> m_TempEntityVector;

Managers/MovableMan.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ void MovableMan::Clear()
7070
m_SloMoDuration = 1000;
7171
m_SettlingEnabled = true;
7272
m_MOSubtractionEnabled = true;
73-
m_pObjectToScriptUpdate = 0;
7473
}
7574

7675

@@ -1658,9 +1657,6 @@ void MovableMan::Update()
16581657
if (g_TimerMan.SimUpdatesSinceDrawn() == 0)
16591658
g_SceneMan.ClearPostEffects();
16601659

1661-
// Lua transfer pointer needs to be cleared, what was set here last update isn't valid anymore
1662-
m_pObjectToScriptUpdate = 0;
1663-
16641660
// Reset the draw HUD roster line settings
16651661
m_SortTeamRoster[Activity::TEAM_1] = false;
16661662
m_SortTeamRoster[Activity::TEAM_2] = false;

0 commit comments

Comments
 (0)