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

Commit 55a10b4

Browse files
committed
Added check for whether or not an object's scripts have been initialized and used that to stop attached attachables from running scripts if their parents' scripts aren't initialized. Also did a little renaming.
1 parent 0969893 commit 55a10b4

File tree

4 files changed

+21
-13
lines changed

4 files changed

+21
-13
lines changed

Entities/Actor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1290,7 +1290,7 @@ bool Actor::UpdateAIScripted() {
12901290
}
12911291

12921292
int status = !g_LuaMan.ExpressionIsTrue(m_ScriptPresetName, false) ? ReloadScripts() : 0;
1293-
status = (status >= 0 && m_ScriptObjectName.empty()) ? SetupScriptObjectNameAndRunLuaCreateFunctions() : status;
1293+
status = (status >= 0 && !ObjectScriptsInitialized()) ? InitializeObjectScripts() : status;
12941294
g_FrameMan.StartPerformanceMeasurement(FrameMan::PERF_ACTORS_AI);
12951295
status = (status >= 0) ? RunScriptedFunctionInAppropriateScripts("UpdateAI", false, true) : status;
12961296
g_FrameMan.StopPerformanceMeasurement(FrameMan::PERF_ACTORS_AI);

Entities/Attachable.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ void Attachable::Attach(MOSRotating *pParent)
274274
// Reset the attachables timers so things that have been sitting in inventory don't make backed up emissions
275275
ResetAllTimers();
276276

277-
if (m_pParent != NULL) {
277+
if (m_pParent != NULL && m_pParent->ObjectScriptsInitialized()) {
278278
RunScriptedFunctionInAppropriateScripts("OnAttach", false, false, {m_pParent});
279279
}
280280
}

Entities/MovableObject.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ int MovableObject::Save(Writer &writer) const
452452
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
453453

454454
void MovableObject::Destroy(bool notInherited) {
455-
if (!m_ScriptObjectName.empty()) {
455+
if (ObjectScriptsInitialized()) {
456456
RunScriptedFunctionInAppropriateScripts("Destroy");
457457
g_LuaMan.RunScriptString(m_ScriptObjectName + " = nil;");
458458
}
@@ -465,17 +465,19 @@ void MovableObject::Destroy(bool notInherited) {
465465

466466
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
467467

468-
int MovableObject::SetupScriptObjectNameAndRunLuaCreateFunctions() {
468+
int MovableObject::InitializeObjectScripts() {
469469
m_ScriptObjectName = GetClassName() + "s." + g_LuaMan.GetNewObjectID();
470470

471471
// Give Lua access to this object, then use that access to set up the object's Lua representation
472472
g_LuaMan.SetTempEntity(this);
473473

474474
if (g_LuaMan.RunScriptString(m_ScriptObjectName + " = To" + GetClassName() + "(LuaMan.TempEntity);") < 0) {
475+
m_ScriptObjectName = "ERROR";
475476
return -2;
476477
}
477478

478479
if (RunScriptedFunctionInAppropriateScripts("Create", true, true) < 0) {
480+
m_ScriptObjectName = "ERROR";
479481
return -3;
480482
}
481483
return 0;
@@ -576,7 +578,7 @@ bool MovableObject::AddScript(const std::string &scriptPath) {
576578
switch (LoadScript(scriptPath)) {
577579
case 0:
578580
// If we have a ScriptObjectName that means Create has already been run for pre-existing scripts. Run it right away for this one.
579-
if (!m_ScriptObjectName.empty()) {
581+
if (ObjectScriptsInitialized()) {
580582
RunScriptedFunction(scriptPath, "Create");
581583
return false;
582584
}
@@ -607,7 +609,7 @@ bool MovableObject::RemoveScript(const std::string &scriptPath) {
607609
auto scriptEntryIterator = FindScript(scriptPath);
608610
if (scriptEntryIterator != m_LoadedScripts.end()) {
609611
m_LoadedScripts.erase(scriptEntryIterator);
610-
if (!m_ScriptObjectName.empty() && RunScriptedFunction(scriptPath, "OnScriptRemoveOrDisable", {}, {"true"}) < 0) {
612+
if (ObjectScriptsInitialized() && RunScriptedFunction(scriptPath, "OnScriptRemoveOrDisable", {}, {"true"}) < 0) {
611613
g_ConsoleMan.PrintString("NOTE: The script has been removed despite this error.");
612614
return false;
613615
}
@@ -625,7 +627,7 @@ bool MovableObject::EnableScript(const std::string &scriptPath) {
625627

626628
auto scriptEntryIterator = FindScript(scriptPath);
627629
if (scriptEntryIterator != m_LoadedScripts.end() && scriptEntryIterator->second == false) {
628-
if (!m_ScriptObjectName.empty() && RunScriptedFunction(scriptPath, "OnScriptEnable") < 0) {
630+
if (ObjectScriptsInitialized() && RunScriptedFunction(scriptPath, "OnScriptEnable") < 0) {
629631
return false;
630632
}
631633
scriptEntryIterator->second = true;
@@ -643,7 +645,7 @@ bool MovableObject::DisableScript(const std::string &scriptPath) {
643645

644646
auto scriptEntryIterator = FindScript(scriptPath);
645647
if (scriptEntryIterator != m_LoadedScripts.end() && scriptEntryIterator->second == true) {
646-
if (!m_ScriptObjectName.empty() && RunScriptedFunction(scriptPath, "OnScriptRemoveOrDisable", {}, {"false"}) < 0) {
648+
if (ObjectScriptsInitialized() && RunScriptedFunction(scriptPath, "OnScriptRemoveOrDisable", {}, {"false"}) < 0) {
647649
return false;
648650
}
649651
scriptEntryIterator->second = false;
@@ -655,7 +657,7 @@ bool MovableObject::DisableScript(const std::string &scriptPath) {
655657
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
656658

657659
int MovableObject::RunScriptedFunction(const std::string &scriptPath, const std::string &functionName, std::vector<Entity *> functionEntityArguments, std::vector<std::string> functionLiteralArguments) {
658-
if (m_LoadedScripts.empty() || m_ScriptPresetName.empty() || m_ScriptObjectName.empty()) {
660+
if (m_LoadedScripts.empty() || m_ScriptPresetName.empty() || !ObjectScriptsInitialized()) {
659661
return -1;
660662
}
661663

@@ -677,7 +679,7 @@ int MovableObject::RunScriptedFunction(const std::string &scriptPath, const std:
677679
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
678680

679681
int MovableObject::RunScriptedFunctionInAppropriateScripts(const std::string &functionName, bool runOnDisabledScripts, bool stopOnError, std::vector<Entity *> functionEntityArguments, std::vector<std::string> functionLiteralArguments) {
680-
if (m_LoadedScripts.empty() || m_ScriptPresetName.empty() || m_ScriptObjectName.empty()) {
682+
if (m_LoadedScripts.empty() || m_ScriptPresetName.empty() || !ObjectScriptsInitialized()) {
681683
return -1;
682684
}
683685

@@ -958,7 +960,7 @@ int MovableObject::UpdateScripts() {
958960
}
959961

960962
int status = !g_LuaMan.ExpressionIsTrue(m_ScriptPresetName, false) ? ReloadScripts() : 0;
961-
status = (status >= 0 && m_ScriptObjectName.empty()) ? SetupScriptObjectNameAndRunLuaCreateFunctions() : status;
963+
status = (status >= 0 && !ObjectScriptsInitialized()) ? InitializeObjectScripts() : status;
962964
status = (status >= 0) ? RunScriptedFunctionInAppropriateScripts("Update", false, true) : status;
963965

964966
return status;
@@ -967,7 +969,7 @@ int MovableObject::UpdateScripts() {
967969
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
968970

969971
int MovableObject::OnPieMenu(Actor *pieMenuActor) {
970-
if (!pieMenuActor || m_LoadedScripts.empty() || m_ScriptPresetName.empty() || m_ScriptObjectName.empty()) {
972+
if (!pieMenuActor || m_LoadedScripts.empty() || m_ScriptPresetName.empty() || !ObjectScriptsInitialized()) {
971973
return -1;
972974
}
973975

Entities/MovableObject.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,12 @@ ENTITYALLOCATION(MovableObject)
264264
/// <returns>An error return value signaling sucess or any particular failure. Anything below 0 is an error signal.</returns>
265265
int RunScriptedFunctionInAppropriateScripts(const std::string &functionName, bool runOnDisabledScripts = false, bool stopOnError = false, std::vector<Entity *> functionEntityArguments = std::vector<Entity *>(), std::vector<std::string> functionLiteralArguments = std::vector<std::string>());
266266

267+
/// <summary>
268+
/// Gets whether or not the object has a script name, and there were no errors when initializing its Lua scripts. If there were, the object would need to be reloaded.
269+
/// </summary>
270+
/// <returns>Whether or not the object's scripts have been succesfully initialized.</returns>
271+
bool ObjectScriptsInitialized() { return !m_ScriptObjectName.empty() && m_ScriptObjectName != "ERROR"; }
272+
267273
//////////////////////////////////////////////////////////////////////////////////////////
268274
// Virtual method: GetClass
269275
//////////////////////////////////////////////////////////////////////////////////////////
@@ -1776,7 +1782,7 @@ ENTITYALLOCATION(MovableObject)
17761782
/// 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.
17771783
/// </summary>
17781784
/// <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>
1779-
int SetupScriptObjectNameAndRunLuaCreateFunctions();
1785+
int InitializeObjectScripts();
17801786

17811787
//////////////////////////////////////////////////////////////////////////////////////////
17821788
// Virtual method: UpdateChildMOIDs

0 commit comments

Comments
 (0)