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

Commit 5bb7bd3

Browse files
committed
Added methods to Add and Remove scripts (primarily for lua purposes) and to Enable and Disable scripts, and check if a script is Enabled. Enabling and Disabling preserves script ordering and is faster to do than reloading the full script. All scripts, enabled or disabled, will always run their create function when they're added (if other create functions have been run already) or when create functions are normally run.
Added lua bindings for these methods, and helper luaman method for running script functions.
1 parent d95e6b6 commit 5bb7bd3

File tree

3 files changed

+120
-1
lines changed

3 files changed

+120
-1
lines changed

Entities/MovableObject.cpp

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "MovableObject.h"
1515
#include "PresetMan.h"
1616
#include "SceneMan.h"
17+
#include "ConsoleMan.h"
1718
#include "SettingsMan.h"
1819
#include "LuaMan.h"
1920
#include "Atom.h"
@@ -556,11 +557,79 @@ int MovableObject::ReloadScripts() {
556557

557558
return status;
558559
}
560+
561+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
562+
563+
bool MovableObject::AddScript(std::string const &scriptPath) {
564+
switch (LoadScript(scriptPath)) {
565+
case 0:
566+
// If we have a ScriptObjectName that means Create has already been run for pre-existing scripts. Run it right away for this one.
567+
if (!m_ScriptObjectName.empty() &&g_LuaMan.RunFunctionInPresetScript("Create", scriptPath, m_ScriptPresetName, m_ScriptObjectName) < 0) {
568+
g_ConsoleMan.PrintString("ERROR: Failed to run Create function for newly added script with path " + scriptPath);
569+
return false;
570+
}
571+
return true;
572+
case -1:
573+
g_ConsoleMan.PrintString("ERROR: The script path was empty.");
574+
break;
575+
case -2:
576+
g_ConsoleMan.PrintString("ERROR: The script path " + scriptPath + " is already loaded onto this object.");
577+
break;
578+
case -3:
579+
g_ConsoleMan.PrintString("ERROR: Failed to do necessary setup to add scripts while attempting to add the script with path " + scriptPath + ". This has nothing to do with your script, please report it to a developer.");
580+
break;
581+
default:
582+
RTEAbort("Reached default case while adding script. This should never happen!");
583+
break;
584+
}
585+
return false;
586+
}
587+
588+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
589+
590+
bool MovableObject::RemoveScript(std::string const &scriptPath) {
591+
auto scriptEntryIterator = FindScript(scriptPath);
592+
if (scriptEntryIterator != m_LoadedScripts.end()) {
593+
m_LoadedScripts.erase(scriptEntryIterator);
594+
return true;
595+
}
596+
return false;
597+
}
598+
599+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
600+
601+
bool MovableObject::EnableScript(std::string const &scriptPath) {
602+
auto scriptEntryIterator = FindScript(scriptPath);
603+
if (scriptEntryIterator != m_LoadedScripts.end() && scriptEntryIterator->second == false) {
604+
scriptEntryIterator->second = true;
605+
606+
if (g_LuaMan.RunFunctionInPresetScript("OnEnableScript", scriptPath, m_ScriptPresetName, m_ScriptObjectName) < 0) {
607+
g_ConsoleMan.PrintString("ERROR: Failed to run OnEnableScript function for newly enabled script with path " + scriptPath);
608+
return false;
609+
}
610+
return true;
559611
}
612+
return false;
613+
}
614+
615+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
616+
617+
bool MovableObject::DisableScript(std::string const &scriptPath) {
618+
auto scriptEntryIterator = FindScript(scriptPath);
619+
if (scriptEntryIterator != m_LoadedScripts.end() && scriptEntryIterator->second == true) {
620+
scriptEntryIterator->second = false;
560621

561-
return error;
622+
if (g_LuaMan.RunFunctionInPresetScript("OnDisableScript", scriptPath, m_ScriptPresetName, m_ScriptObjectName) < 0) {
623+
g_ConsoleMan.PrintString("ERROR: Failed to run OnDisableScript function for newly disabled script with path " + scriptPath);
624+
return false;
625+
}
626+
return true;
627+
}
628+
return false;
562629
}
563630

631+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
632+
564633
/*
565634
//////////////////////////////////////////////////////////////////////////////////////////
566635
// Constructor: MovableObject

Entities/MovableObject.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,40 @@ ENTITYALLOCATION(MovableObject)
206206
/// <returns>Whether or not the script is on this MO.</returns>
207207
virtual bool const HasScript(std::string const &scriptPath) { return FindScript(scriptPath) != m_LoadedScripts.end(); }
208208

209+
/// <summary>
210+
/// Adds the script at the given path as one of the scripts on this MO.
211+
/// </summary>
212+
/// <param name="scriptPath">The path to the script to add.</param>
213+
/// <returns>Whether or not the script was successfully added.</returns>
214+
virtual bool AddScript(std::string const &scriptPath);
209215

216+
/// <summary>
217+
/// Removes the script at the given path so it will no longer be one of the scripts on this MO.
218+
/// </summary>
219+
/// <param name="scriptPath">The path to the script to remove.</param>
220+
/// <returns>Whether or not the script was successfully removed.</returns>
221+
virtual bool RemoveScript(std::string const &scriptPath);
210222

223+
/// <summary>
224+
/// Checks if the script at the given path is one of the enabled scripts on this MO.
225+
/// </summary>
226+
/// <param name="scriptPath">The path to the script to check.</param>
227+
/// <returns>Whether or not the script is enabled on this MO.</returns>
228+
virtual bool const ScriptEnabled(std::string const &scriptPath) { auto scriptIterator = FindScript(scriptPath); return scriptIterator != m_LoadedScripts.end() && scriptIterator->second == true; }
229+
230+
/// <summary>
231+
/// Enable the script at the given path on this MO.
232+
/// </summary>
233+
/// <param name="scriptPath">The path to the script to enable.</param>
234+
/// <returns>Whether or not the script was succesfully enabled.</returns>
235+
virtual bool EnableScript(std::string const &scriptPath);
236+
237+
/// <summary>
238+
/// Disables the script at the given path for this MO.
239+
/// </summary>
240+
/// <param name="scriptPath">The path to the script to disable.</param>
241+
/// <returns>Whether or not the script was succesfully disabled..</returns>
242+
virtual bool DisableScript(std::string const &scriptPath);
211243

212244
//////////////////////////////////////////////////////////////////////////////////////////
213245
// Virtual method: GetClass

Managers/LuaMan.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,12 @@ int LuaMan::Create()
584584
ABSTRACTLUABINDING(MovableObject, SceneObject)
585585
.property("Material", &MovableObject::GetMaterial)
586586
.def("ReloadScripts", &MovableObject::ReloadScripts)
587+
.def("HasScript", &MovableObject::HasScript)
588+
.def("AddScript", &MovableObject::AddScript)
589+
.def("RemoveScript", &MovableObject::RemoveScript)
590+
.def("ScriptEnabled", &MovableObject::ScriptEnabled)
591+
.def("EnableScript", &MovableObject::EnableScript)
592+
.def("DisableScript", &MovableObject::DisableScript)
587593
.property("Mass", &MovableObject::GetMass, &MovableObject::SetMass)
588594
.property("Pos", &MovableObject::GetPos, &MovableObject::SetPos)
589595
.property("Vel", &MovableObject::GetVel, &MovableObject::SetVel)
@@ -2413,6 +2419,18 @@ bool LuaMan::ExpressionIsTrue(string expression, bool consoleErrors)
24132419
return result;
24142420
}
24152421

2422+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2423+
2424+
int LuaMan::RunFunctionInPresetScript(std::string const &functionName, std::string const &scriptPath, std::string const &presetLuaInstanceName, std::string const &objectLuaInstanceName) {
2425+
std::string presetAndFunctionName = presetLuaInstanceName + "." + functionName;
2426+
return RunScriptString(
2427+
"if " + presetAndFunctionName + " and " + objectLuaInstanceName + " then " +
2428+
presetAndFunctionName + "[\""+scriptPath+"\"](" + objectLuaInstanceName + "); " +
2429+
"end"
2430+
);
2431+
}
2432+
2433+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
24162434

24172435
//////////////////////////////////////////////////////////////////////////////////////////
24182436
// Method: RunScriptString

0 commit comments

Comments
 (0)