@@ -452,7 +452,7 @@ int MovableObject::Save(Writer &writer) const
452
452
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
453
453
454
454
void MovableObject::Destroy (bool notInherited) {
455
- if (!m_ScriptObjectName. empty ()) {
455
+ if (ObjectScriptsInitialized ()) {
456
456
RunScriptedFunctionInAppropriateScripts (" Destroy" );
457
457
g_LuaMan.RunScriptString (m_ScriptObjectName + " = nil;" );
458
458
}
@@ -465,17 +465,19 @@ void MovableObject::Destroy(bool notInherited) {
465
465
466
466
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
467
467
468
- int MovableObject::SetupScriptObjectNameAndRunLuaCreateFunctions () {
468
+ int MovableObject::InitializeObjectScripts () {
469
469
m_ScriptObjectName = GetClassName () + " s." + g_LuaMan.GetNewObjectID ();
470
470
471
471
// Give Lua access to this object, then use that access to set up the object's Lua representation
472
472
g_LuaMan.SetTempEntity (this );
473
473
474
474
if (g_LuaMan.RunScriptString (m_ScriptObjectName + " = To" + GetClassName () + " (LuaMan.TempEntity);" ) < 0 ) {
475
+ m_ScriptObjectName = " ERROR" ;
475
476
return -2 ;
476
477
}
477
478
478
479
if (RunScriptedFunctionInAppropriateScripts (" Create" , true , true ) < 0 ) {
480
+ m_ScriptObjectName = " ERROR" ;
479
481
return -3 ;
480
482
}
481
483
return 0 ;
@@ -576,7 +578,7 @@ bool MovableObject::AddScript(const std::string &scriptPath) {
576
578
switch (LoadScript (scriptPath)) {
577
579
case 0 :
578
580
// 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 ()) {
580
582
RunScriptedFunction (scriptPath, " Create" );
581
583
return false ;
582
584
}
@@ -607,7 +609,7 @@ bool MovableObject::RemoveScript(const std::string &scriptPath) {
607
609
auto scriptEntryIterator = FindScript (scriptPath);
608
610
if (scriptEntryIterator != m_LoadedScripts.end ()) {
609
611
m_LoadedScripts.erase (scriptEntryIterator);
610
- if (!m_ScriptObjectName. empty () && RunScriptedFunction (scriptPath, " OnScriptRemoveOrDisable" , {}, {" true" }) < 0 ) {
612
+ if (ObjectScriptsInitialized () && RunScriptedFunction (scriptPath, " OnScriptRemoveOrDisable" , {}, {" true" }) < 0 ) {
611
613
g_ConsoleMan.PrintString (" NOTE: The script has been removed despite this error." );
612
614
return false ;
613
615
}
@@ -625,7 +627,7 @@ bool MovableObject::EnableScript(const std::string &scriptPath) {
625
627
626
628
auto scriptEntryIterator = FindScript (scriptPath);
627
629
if (scriptEntryIterator != m_LoadedScripts.end () && scriptEntryIterator->second == false ) {
628
- if (!m_ScriptObjectName. empty () && RunScriptedFunction (scriptPath, " OnScriptEnable" ) < 0 ) {
630
+ if (ObjectScriptsInitialized () && RunScriptedFunction (scriptPath, " OnScriptEnable" ) < 0 ) {
629
631
return false ;
630
632
}
631
633
scriptEntryIterator->second = true ;
@@ -643,7 +645,7 @@ bool MovableObject::DisableScript(const std::string &scriptPath) {
643
645
644
646
auto scriptEntryIterator = FindScript (scriptPath);
645
647
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 ) {
647
649
return false ;
648
650
}
649
651
scriptEntryIterator->second = false ;
@@ -655,7 +657,7 @@ bool MovableObject::DisableScript(const std::string &scriptPath) {
655
657
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
656
658
657
659
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 ()) {
659
661
return -1 ;
660
662
}
661
663
@@ -677,7 +679,7 @@ int MovableObject::RunScriptedFunction(const std::string &scriptPath, const std:
677
679
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
678
680
679
681
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 ()) {
681
683
return -1 ;
682
684
}
683
685
@@ -958,7 +960,7 @@ int MovableObject::UpdateScripts() {
958
960
}
959
961
960
962
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;
962
964
status = (status >= 0 ) ? RunScriptedFunctionInAppropriateScripts (" Update" , false , true ) : status;
963
965
964
966
return status;
@@ -967,7 +969,7 @@ int MovableObject::UpdateScripts() {
967
969
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
968
970
969
971
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 ()) {
971
973
return -1 ;
972
974
}
973
975
0 commit comments