Skip to content

Commit 0ff075c

Browse files
authored
Merge pull request #182 from cortex-command-community/ordered-scripts
Scripts Maintaining Insertion Order
2 parents e5bbaa4 + ab8b18b commit 0ff075c

File tree

3 files changed

+22
-17
lines changed

3 files changed

+22
-17
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
187187

188188
- Fixed several issues with the way pie menus and aiming interacts between players, such as opening the pie menu always resetting the M&KB player's aim and pie selection, as well as another issue where the pie menu would fail to appear entirely for some players.
189189

190+
- Fixed issue where scripts applied to `MovableObject`s could become disordered in certain circumstances.
191+
190192
</details>
191193

192194
<details><summary><b>Removed</b></summary>

Source/Entities/MovableObject.cpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ void MovableObject::Clear() {
7979
m_HUDVisible = true;
8080
m_IsTraveling = false;
8181
m_AllLoadedScripts.clear();
82+
m_EnabledScripts.clear();
8283
m_FunctionsAndScripts.clear();
8384
m_StringValueMap.clear();
8485
m_NumberValueMap.clear();
@@ -225,8 +226,8 @@ int MovableObject::Create(const MovableObject& reference) {
225226
m_PostEffectEnabled = reference.m_PostEffectEnabled;
226227

227228
m_ForceIntoMasterLuaState = reference.m_ForceIntoMasterLuaState;
228-
for (auto& [scriptPath, scriptEnabled]: reference.m_AllLoadedScripts) {
229-
LoadScript(scriptPath, scriptEnabled);
229+
for (const auto& scriptPath: reference.m_AllLoadedScripts) {
230+
LoadScript(scriptPath, reference.m_EnabledScripts.at(scriptPath));
230231
}
231232

232233
if (reference.m_pScreenEffect) {
@@ -442,7 +443,7 @@ int MovableObject::Save(Writer& writer) const {
442443
writer << m_CanBeSquished;
443444
writer.NewProperty("HUDVisible");
444445
writer << m_HUDVisible;
445-
for (const auto& [scriptPath, scriptEnabled]: m_AllLoadedScripts) {
446+
for (const auto& [scriptPath, scriptEnabled]: m_EnabledScripts) {
446447
if (!scriptPath.empty()) {
447448
writer.NewProperty("ScriptPath");
448449
writer << scriptPath;
@@ -545,7 +546,8 @@ int MovableObject::LoadScript(const std::string& scriptPath, bool loadAsEnabledS
545546
}
546547
}
547548

548-
m_AllLoadedScripts.try_emplace(scriptPath, loadAsEnabledScript);
549+
m_EnabledScripts.try_emplace(scriptPath, loadAsEnabledScript);
550+
m_AllLoadedScripts.push_back(scriptPath);
549551

550552
std::unordered_map<std::string, LuabindObjectWrapper*> scriptFileFunctions;
551553
if (usedState.RunScriptFileAndRetrieveFunctions(scriptPath, GetSupportedScriptFunctionNames(), scriptFileFunctions) < 0) {
@@ -580,14 +582,16 @@ int MovableObject::ReloadScripts() {
580582
movableObjectPreset->ReloadScripts();
581583
}
582584

583-
std::unordered_map<std::string, bool> loadedScriptsCopy = m_AllLoadedScripts;
585+
std::unordered_map<std::string, bool> enabledScriptsCopy = m_EnabledScripts;
586+
std::vector<std::string> loadedScriptsCopy = m_AllLoadedScripts;
587+
m_EnabledScripts.clear();
584588
m_AllLoadedScripts.clear();
585589
m_FunctionsAndScripts.clear();
586-
for (const auto& [scriptPath, scriptEnabled]: loadedScriptsCopy) {
587-
status = LoadScript(scriptPath, scriptEnabled);
590+
for (const auto& scriptPath: loadedScriptsCopy) {
591+
status = LoadScript(scriptPath, enabledScriptsCopy.at(scriptPath));
588592
// If the script fails to load because of an error in its Lua, we need to manually add the script path so it's not lost forever.
589593
if (status == -4) {
590-
m_AllLoadedScripts.try_emplace(scriptPath, scriptEnabled);
594+
m_EnabledScripts.try_emplace(scriptPath, enabledScriptsCopy.at(scriptPath));
591595
} else if (status < 0) {
592596
break;
593597
}
@@ -618,7 +622,7 @@ bool MovableObject::EnableOrDisableScript(const std::string& scriptPath, bool en
618622
return false;
619623
}
620624

621-
if (auto scriptEntryIterator = m_AllLoadedScripts.find(scriptPath); scriptEntryIterator != m_AllLoadedScripts.end() && scriptEntryIterator->second == !enableScript) {
625+
if (auto scriptEntryIterator = m_EnabledScripts.find(scriptPath); scriptEntryIterator != m_EnabledScripts.end() && scriptEntryIterator->second == !enableScript) {
622626
if (ObjectScriptsInitialized() && RunFunctionOfScript(scriptPath, enableScript ? "OnScriptEnable" : "OnScriptDisable") < 0) {
623627
return false;
624628
}
@@ -640,7 +644,7 @@ bool MovableObject::EnableOrDisableScript(const std::string& scriptPath, bool en
640644
}
641645

642646
void MovableObject::EnableOrDisableAllScripts(bool enableScripts) {
643-
for (const auto& [scriptPath, scriptIsEnabled]: m_AllLoadedScripts) {
647+
for (const auto& [scriptPath, scriptIsEnabled]: m_EnabledScripts) {
644648
if (enableScripts != scriptIsEnabled) {
645649
EnableOrDisableScript(scriptPath, enableScripts);
646650
}
@@ -688,9 +692,7 @@ int MovableObject::RunFunctionOfScript(const std::string& scriptPath, const std:
688692
for (const LuaFunction& luaFunction: m_FunctionsAndScripts.at(functionName)) {
689693
const LuabindObjectWrapper* luabindObjectWrapper = luaFunction.m_LuaFunction.get();
690694
if (scriptPath == luabindObjectWrapper->GetFilePath() && usedState.RunScriptFunctionObject(luabindObjectWrapper, "_ScriptedObjects", std::to_string(m_UniqueID), functionEntityArguments, functionLiteralArguments) < 0) {
691-
if (m_AllLoadedScripts.size() > 1) {
692-
g_ConsoleMan.PrintString("ERROR: An error occured while trying to run the " + functionName + " function for script at path " + scriptPath);
693-
}
695+
g_ConsoleMan.PrintString("ERROR: An error occured while trying to run the " + functionName + " function for script at path " + scriptPath);
694696
return -2;
695697
}
696698
}

Source/Entities/MovableObject.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,14 @@ namespace RTE {
112112
/// Checks if the script at the given path is one of the scripts on this MO.
113113
/// @param scriptPath The path to the script to check.
114114
/// @return Whether or not the script is on this MO.
115-
bool HasScript(const std::string& scriptPath) const { return m_AllLoadedScripts.find(scriptPath) != m_AllLoadedScripts.end(); }
115+
bool HasScript(const std::string& scriptPath) const { return std::find(m_AllLoadedScripts.begin(), m_AllLoadedScripts.end(), scriptPath) != m_AllLoadedScripts.end(); }
116116

117117
/// Checks if the script at the given path is one of the enabled scripts on this MO.
118118
/// @param scriptPath The path to the script to check.
119119
/// @return Whether or not the script is enabled on this MO.
120120
bool ScriptEnabled(const std::string& scriptPath) const {
121-
auto scriptPathIterator = m_AllLoadedScripts.find(scriptPath);
122-
return scriptPathIterator != m_AllLoadedScripts.end() && scriptPathIterator->second == true;
121+
auto scriptPathIterator = m_EnabledScripts.find(scriptPath);
122+
return scriptPathIterator != m_EnabledScripts.end() && scriptPathIterator->second == true;
123123
}
124124

125125
/// Enables or dsiableds the script at the given path on this MO.
@@ -1235,7 +1235,8 @@ namespace RTE {
12351235
};
12361236

12371237
std::string m_ScriptObjectName; //!< The name of this object for script usage.
1238-
std::unordered_map<std::string, bool> m_AllLoadedScripts; //!< A map of script paths to the enabled state of the given script.
1238+
std::vector<std::string> m_AllLoadedScripts; //!< A vector of script for scripts applied to this object, in order of insertion.
1239+
std::unordered_map<std::string, bool> m_EnabledScripts; //!< A map of script paths to the enabled state of the given script.
12391240
std::unordered_map<std::string, std::vector<LuaFunction>> m_FunctionsAndScripts; //!< A map of function names to vectors of Lua functions. Used to maintain script execution order and avoid extraneous Lua calls.
12401241

12411242
volatile bool m_RequestedSyncedUpdate; //!< For optimisation purposes, scripts explicitly request a synced update if they want one.

0 commit comments

Comments
 (0)