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

Commit e3bc4fc

Browse files
committed
Code review comments - added constness, fixed if check on Attachable, wrote out autos that were used for iterators, fixed range loops not using const references
1 parent 55a10b4 commit e3bc4fc

File tree

5 files changed

+22
-17
lines changed

5 files changed

+22
-17
lines changed

Entities/Actor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1538,7 +1538,7 @@ ENTITYALLOCATION(Actor)
15381538
/// Gets a vector containing the script function names this class supports.
15391539
/// </summary>
15401540
/// <returns>A vector containing the script function names this class supports.</returns>
1541-
virtual const std::vector<std::string> GetSupportedScriptFunctionNames() override { auto functionNames = MOSRotating::GetSupportedScriptFunctionNames(); functionNames.insert(functionNames.end(), {"UpdateAI"}); return functionNames; }
1541+
virtual const std::vector<std::string> GetSupportedScriptFunctionNames() const override { auto functionNames = MOSRotating::GetSupportedScriptFunctionNames(); functionNames.insert(functionNames.end(), {"UpdateAI"}); return functionNames; }
15421542

15431543
//////////////////////////////////////////////////////////////////////////////////////////
15441544
// Private member variable and method declarations

Entities/Attachable.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ void Attachable::Detach()
306306

307307
m_RestTimer.Reset();
308308

309-
if (temporaryParent != NULL) {
309+
if (temporaryParent != NULL && temporaryParent->ObjectScriptsInitialized()) {
310310
RunScriptedFunctionInAppropriateScripts("OnDetach", false, false, {temporaryParent});
311311
}
312312
}

Entities/Attachable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ ENTITYALLOCATION(Attachable)
704704
/// Gets a vector containing the script function names this class supports.
705705
/// </summary>
706706
/// <returns>A vector containing the script function names this class supports.</returns>
707-
virtual const std::vector<std::string> GetSupportedScriptFunctionNames() override { auto functionNames = MOSRotating::GetSupportedScriptFunctionNames(); functionNames.insert(functionNames.end(), {"OnAttach", "OnDetach"}); return functionNames; }
707+
virtual const std::vector<std::string> GetSupportedScriptFunctionNames() const override { auto functionNames = MOSRotating::GetSupportedScriptFunctionNames(); functionNames.insert(functionNames.end(), {"OnAttach", "OnDetach"}); return functionNames; }
708708

709709

710710
//////////////////////////////////////////////////////////////////////////////////////////

Entities/MovableObject.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ int MovableObject::Create(const MovableObject &reference)
205205
m_MissionCritical = reference.m_MissionCritical;
206206
m_CanBeSquished = reference.m_CanBeSquished;
207207
m_HUDVisible = reference.m_HUDVisible;
208-
for (std::pair<std::string, bool> scriptEntry : reference.m_LoadedScripts) {
208+
for (const std::pair<std::string, bool> &scriptEntry : reference.m_LoadedScripts) {
209209
m_LoadedScripts.push_back({scriptEntry.first, scriptEntry.second});
210210
}
211211
m_ScriptPresetName = reference.m_ScriptPresetName;
@@ -329,9 +329,7 @@ int MovableObject::ReadProperty(std::string propName, Reader &reader)
329329
else if (propName == "ScriptPath")
330330
{
331331
std::string scriptPath = reader.ReadPropValue();
332-
if (LoadScript(scriptPath) == -2) {
333-
reader.ReportError("Duplicate script path " + scriptPath);
334-
}
332+
if (LoadScript(scriptPath) == -2) { reader.ReportError("Duplicate script path " + scriptPath); }
335333
}
336334
else if (propName == "ScreenEffect")
337335
{
@@ -495,7 +493,7 @@ int MovableObject::LoadScript(const std::string &scriptPath, bool loadAsEnabledS
495493
m_LoadedScripts.push_back({scriptPath, loadAsEnabledScript});
496494

497495
// Clear the temporary variable names that will hold the functions read in from the file
498-
for (std::string functionName : GetSupportedScriptFunctionNames()) {
496+
for (const std::string &functionName : GetSupportedScriptFunctionNames()) {
499497
if (g_LuaMan.RunScriptString(functionName + " = nil;") < 0) {
500498
return -3;
501499
}
@@ -551,7 +549,7 @@ int MovableObject::ReloadScripts() {
551549
object->m_ScriptPresetName.clear();
552550

553551
int status = 0;
554-
for (std::pair<std::string, bool> scriptEntry : loadedScriptsCopy) {
552+
for (const std::pair<std::string, bool> &scriptEntry : loadedScriptsCopy) {
555553
status = object->LoadScript(scriptEntry.first, scriptEntry.second);
556554
if (status < 0) {
557555
return status;
@@ -606,7 +604,7 @@ bool MovableObject::RemoveScript(const std::string &scriptPath) {
606604
return false;
607605
}
608606

609-
auto scriptEntryIterator = FindScript(scriptPath);
607+
std::vector<std::pair<std::string, bool>>::const_iterator scriptEntryIterator = FindScript(scriptPath);
610608
if (scriptEntryIterator != m_LoadedScripts.end()) {
611609
m_LoadedScripts.erase(scriptEntryIterator);
612610
if (ObjectScriptsInitialized() && RunScriptedFunction(scriptPath, "OnScriptRemoveOrDisable", {}, {"true"}) < 0) {
@@ -625,7 +623,7 @@ bool MovableObject::EnableScript(const std::string &scriptPath) {
625623
return false;
626624
}
627625

628-
auto scriptEntryIterator = FindScript(scriptPath);
626+
std::vector<std::pair<std::string, bool>>::iterator scriptEntryIterator = FindScript(scriptPath);
629627
if (scriptEntryIterator != m_LoadedScripts.end() && scriptEntryIterator->second == false) {
630628
if (ObjectScriptsInitialized() && RunScriptedFunction(scriptPath, "OnScriptEnable") < 0) {
631629
return false;
@@ -643,7 +641,7 @@ bool MovableObject::DisableScript(const std::string &scriptPath) {
643641
return false;
644642
}
645643

646-
auto scriptEntryIterator = FindScript(scriptPath);
644+
std::vector<std::pair<std::string, bool>>::iterator scriptEntryIterator = FindScript(scriptPath);
647645
if (scriptEntryIterator != m_LoadedScripts.end() && scriptEntryIterator->second == true) {
648646
if (ObjectScriptsInitialized() && RunScriptedFunction(scriptPath, "OnScriptRemoveOrDisable", {}, {"false"}) < 0) {
649647
return false;
@@ -684,7 +682,7 @@ int MovableObject::RunScriptedFunctionInAppropriateScripts(const std::string &fu
684682
}
685683

686684
int status = 0;
687-
for (std::pair<std::string, bool> scriptEntry : m_LoadedScripts) {
685+
for (const std::pair<std::string, bool> &scriptEntry : m_LoadedScripts) {
688686
if (runOnDisabledScripts || scriptEntry.second == true) {
689687
status = RunScriptedFunction(scriptEntry.first, functionName, functionEntityArguments, functionLiteralArguments);
690688
if (status < 0 && stopOnError) {

Entities/MovableObject.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,12 +199,19 @@ ENTITYALLOCATION(MovableObject)
199199
/// <returns>The iterator pointing to the vector entry for the script or the end of the vector if the script was not found.</returns>
200200
virtual std::vector<std::pair<std::string, bool>>::iterator const FindScript(std::string const &scriptPath) { return std::find_if(m_LoadedScripts.begin(), m_LoadedScripts.end(), [&scriptPath](auto element) { return element.first == scriptPath; }); }
201201

202+
/// <summary>
203+
/// Convenience method to get the script at the given path if it's on this MO. Like standard find, returns m_LoadedScripts.cend() if it's not.
204+
/// </summary>
205+
/// <param name="scriptPath">The path to the script to find.</param>
206+
/// <returns>The iterator pointing to the vector entry for the script or the end of the vector if the script was not found.</returns>
207+
virtual std::vector<std::pair<std::string, bool>>::const_iterator const FindScript(std::string const &scriptPath) const { return std::find_if(m_LoadedScripts.cbegin(), m_LoadedScripts.cend(), [&scriptPath](auto element) { return element.first == scriptPath; }); }
208+
202209
/// <summary>
203210
/// Checks if the script at the given path is one of the scripts on this MO.
204211
/// </summary>
205212
/// <param name="scriptPath">The path to the script to check.</param>
206213
/// <returns>Whether or not the script is on this MO.</returns>
207-
virtual bool const HasScript(const std::string &scriptPath) { return FindScript(scriptPath) != m_LoadedScripts.end(); }
214+
virtual bool const HasScript(const std::string &scriptPath) const { return FindScript(scriptPath) != m_LoadedScripts.end(); }
208215

209216
/// <summary>
210217
/// Adds the script at the given path as one of the scripts on this MO.
@@ -225,7 +232,7 @@ ENTITYALLOCATION(MovableObject)
225232
/// </summary>
226233
/// <param name="scriptPath">The path to the script to check.</param>
227234
/// <returns>Whether or not the script is enabled on this MO.</returns>
228-
virtual bool const ScriptEnabled(const std::string &scriptPath) { auto scriptIterator = FindScript(scriptPath); return scriptIterator != m_LoadedScripts.end() && scriptIterator->second == true; }
235+
virtual bool const ScriptEnabled(const std::string &scriptPath) const { auto scriptIterator = FindScript(scriptPath); return scriptIterator != m_LoadedScripts.end() && scriptIterator->second == true; }
229236

230237
/// <summary>
231238
/// Enable the script at the given path on this MO.
@@ -268,7 +275,7 @@ ENTITYALLOCATION(MovableObject)
268275
/// 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.
269276
/// </summary>
270277
/// <returns>Whether or not the object's scripts have been succesfully initialized.</returns>
271-
bool ObjectScriptsInitialized() { return !m_ScriptObjectName.empty() && m_ScriptObjectName != "ERROR"; }
278+
bool ObjectScriptsInitialized() const { return !m_ScriptObjectName.empty() && m_ScriptObjectName != "ERROR"; }
272279

273280
//////////////////////////////////////////////////////////////////////////////////////////
274281
// Virtual method: GetClass
@@ -1776,7 +1783,7 @@ ENTITYALLOCATION(MovableObject)
17761783
/// Gets a vector containing the script function names this class supports.
17771784
/// </summary>
17781785
/// <returns>A vector containing the script function names this class supports.</returns>
1779-
virtual const std::vector<std::string> GetSupportedScriptFunctionNames() { return std::vector<std::string> {"Create", "Destroy", "Update", "OnScriptRemoveOrDisable", "OnScriptEnable", "OnPieMenu"}; }
1786+
virtual const std::vector<std::string> GetSupportedScriptFunctionNames() const { return std::vector<std::string> {"Create", "Destroy", "Update", "OnScriptRemoveOrDisable", "OnScriptEnable", "OnPieMenu", "OnCollideWithTerrain", "OnCollideWithMO"}; }
17801787

17811788
/// <summary>
17821789
/// 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.

0 commit comments

Comments
 (0)