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

Commit d89cc44

Browse files
committed
Updated Actor::LoadScript and UpdateAIScripted so they work with multiscripts
Added SupportedScriptFunctionNames with extra special one for for Actor so UpdateAI is handled Missed LuaMan header change in last commit
1 parent 5bb7bd3 commit d89cc44

File tree

4 files changed

+64
-71
lines changed

4 files changed

+64
-71
lines changed

Entities/Actor.cpp

Lines changed: 37 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -487,41 +487,22 @@ void Actor::Destroy(bool notInherited)
487487
Clear();
488488
}
489489

490+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
490491

491-
//////////////////////////////////////////////////////////////////////////////////////////
492-
// Virtual method: LoadScripts
493-
//////////////////////////////////////////////////////////////////////////////////////////
494-
// Description: Loads the preset scripts of this object, from a specified path.
495-
496-
int Actor::LoadScripts(string scriptPath)
497-
{
498-
if (scriptPath.empty())
499-
return -1;
500-
501-
int error = 0;
502-
503-
// Clear the temporary variable names that will hold the functions read in from the file
504-
if ((error = g_LuaMan.RunScriptString("UpdateAI = nil;")) < 0)
505-
return error;
506-
507-
// Read in the Lua script function definitions for this preset
508-
if ((error = MovableObject::LoadScripts(scriptPath)) < 0)
509-
return error;
510-
511-
// Add the UpdateAI function, if it exists.. if it doesn't, that's not a problem! We'll just be using the old C++ implementation
512-
if (g_LuaMan.GlobalIsDefined("UpdateAI"))
513-
{
514-
// Mark that we have a Lua override for the UpdateAI function
515-
m_ScriptedAIUpdate = true;
516-
if ((error = g_LuaMan.RunScriptString("if UpdateAI then " + m_ScriptPresetName + ".UpdateAI = UpdateAI; end;")) < 0)
517-
return error;
492+
int Actor::LoadScript(std::string const &scriptPath, bool loadAsEnabledScript) {
493+
int status = MOSRotating::LoadScript(scriptPath, loadAsEnabledScript);
494+
if (status < 0) {
495+
return status;
518496
}
519-
else
520-
m_ScriptedAIUpdate = false;
521497

522-
return error;
498+
// If UpdateAI existed it'll be in the lua global namespace, so we can check that to know whether or not to use Lua AI
499+
m_ScriptedAIUpdate = g_LuaMan.GlobalIsDefined("UpdateAI");
500+
501+
return status;
523502
}
524503

504+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
505+
525506

526507
//////////////////////////////////////////////////////////////////////////////////////////
527508
// Virtual method: GetMass
@@ -1301,52 +1282,47 @@ bool Actor::UpdateMovePath()
13011282
return true;
13021283
}
13031284

1285+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
13041286

1305-
//////////////////////////////////////////////////////////////////////////////////////////
1306-
// Virtual method: UpdateAIScripted
1307-
//////////////////////////////////////////////////////////////////////////////////////////
1308-
// Description: Updates this' AI state with the provided scripted AI Update function.
1309-
1310-
bool Actor::UpdateAIScripted()
1311-
{
1312-
// This preset doesn't seem to have any script file defined, so then just report we couldn't run scripted AI
1313-
if (!m_ScriptedAIUpdate || m_ScriptPath.empty() || m_ScriptPresetName.empty())
1287+
bool Actor::UpdateAIScripted() {
1288+
if (!m_ScriptedAIUpdate || (m_LoadedScripts.empty()) || m_ScriptPresetName.empty()) {
13141289
return false;
1315-
1316-
int error = 0;
1290+
}
13171291

13181292
// Check to make sure the preset of this is still defined in the Lua state. If not, re-create it and recover gracefully
1319-
if (!g_LuaMan.ExpressionIsTrue(m_ScriptPresetName, false))
1320-
ReloadScripts();
1293+
if (!g_LuaMan.ExpressionIsTrue(m_ScriptPresetName, false)) {
1294+
ReloadScripts(); //TODO test if this should be here, I think it's junk cause for any cases where there's AI, update will always be called first. If it shouldn't, change the early return above.
1295+
}
13211296

1322-
// First see if we even have a representation stored in the Lua state, and if not, create one
1323-
if (m_ScriptObjectName.empty())
1324-
{
1325-
// Get the unique object identifier for this object and construct the object isntance name in Lua that points to this object so we can pass it into the preset functions
1297+
// If we don't have a Lua representation for this object instance, create one and call the Lua Create function on it
1298+
if (m_ScriptObjectName.empty()) {
1299+
//TODO test if this should be here, I think it's junk cause for any cases where there's AI, update will always be called first. If it shouldn't, change the early return above.
13261300
m_ScriptObjectName = GetClassName() + "s." + g_LuaMan.GetNewObjectID();
13271301

1328-
// Give access to this in the Lua state
1302+
// Give Lua access to this object, then use that access to set up the object's Lua representation
13291303
g_MovableMan.SetScriptedEntity(this);
1330-
// Create the Lua variable which will hold the object instance of this instance for as long as it exists
1331-
if ((error = g_LuaMan.RunScriptString(m_ScriptObjectName + " = To" + GetClassName() + "(MovableMan.ScriptedEntity);")) < 0)
1304+
if (g_LuaMan.RunScriptString(m_ScriptObjectName + " = To" + GetClassName() + "(MovableMan.ScriptedEntity);") < 0) {
13321305
return false;
1306+
}
13331307

1334-
// Call the scripted creation function, but only after first checking if it and this instance's Lua representation really exists
1335-
if ((error = g_LuaMan.RunScriptString("if " + m_ScriptPresetName + ".Create and " + m_ScriptObjectName + " then " + m_ScriptPresetName + ".Create(" + m_ScriptObjectName + "); end")) < 0)
1336-
return false;
1308+
for (std::pair<std::string, bool> scriptEntry : m_LoadedScripts) {
1309+
if (g_LuaMan.RunFunctionInPresetScript("Create", scriptEntry.first, m_ScriptPresetName, m_ScriptObjectName) < 0) {
1310+
return -3;
1311+
}
1312+
}
13371313
}
13381314

1339-
// Call the defined function, but only after first checking if it and this instance's Lua representation exists
1340-
13411315
g_FrameMan.StartPerformanceMeasurement(FrameMan::PERF_ACTORS_AI);
1342-
error = g_LuaMan.RunScriptString("if " + m_ScriptPresetName + ".UpdateAI and " + m_ScriptObjectName + " then " + m_ScriptPresetName + ".UpdateAI(" + m_ScriptObjectName + "); end");
1316+
bool aiUpdateSucceeded = true;
1317+
for (std::pair<std::string, bool> scriptEntry : m_LoadedScripts) {
1318+
if (scriptEntry.second == true) {
1319+
aiUpdateSucceeded = g_LuaMan.RunFunctionInPresetScript("UpdateAI", scriptEntry.first, m_ScriptPresetName, m_ScriptObjectName) >= 0;
1320+
}
1321+
if (!aiUpdateSucceeded) { break; }
1322+
}
13431323
g_FrameMan.StopPerformanceMeasurement(FrameMan::PERF_ACTORS_AI);
13441324

1345-
if (error < 0)
1346-
return false;
1347-
1348-
// We made a successful scripted AI update!
1349-
return true;
1325+
return aiUpdateSucceeded;
13501326
}
13511327

13521328

Entities/Actor.h

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -192,16 +192,13 @@ ENTITYALLOCATION(Actor)
192192

193193
virtual void Destroy(bool notInherited = false);
194194

195-
196-
//////////////////////////////////////////////////////////////////////////////////////////
197-
// Virtual method: LoadScripts
198-
//////////////////////////////////////////////////////////////////////////////////////////
199-
// Description: Loads the preset scripts of this object, from a specified path.
200-
// Arguments: None.
201-
// Return value: An error return value signaling sucess or any particular failure.
202-
// Anything below 0 is an error signal.
203-
204-
virtual int LoadScripts(std::string scriptPath);
195+
/// <summary>
196+
/// Loads the script at the given script path onto the object, checking for appropriately named functions within it.
197+
/// </summary>
198+
/// <param name="scriptPath">The path to the script to load.</param>
199+
/// <param name="loadAsEnabledScript">Whether or not the script should load as enabled. Defaults to true.</param>
200+
/// <returns>An error return value signaling sucess or any particular failure. Anything below 0 is an error signal.</returns>
201+
virtual int LoadScript(std::string const &scriptPath, bool loadAsEnabledScript = false);
205202

206203

207204
//////////////////////////////////////////////////////////////////////////////////////////
@@ -1330,6 +1327,11 @@ ENTITYALLOCATION(Actor)
13301327

13311328
protected:
13321329

1330+
/// <summary>
1331+
/// Gets a vector containing the script function names this class supports.
1332+
/// </summary>
1333+
/// <returns>A vector containing the script function names this class supports.</returns>
1334+
virtual const std::vector<std::string> GetSupportedScriptFunctionNames() override { auto functionNames = MOSRotating::GetSupportedScriptFunctionNames(); functionNames.push_back("UpdateAI"); return functionNames; }
13331335

13341336
// Member variables
13351337
static Entity::ClassInfo m_sClass;

Entities/MovableObject.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1741,6 +1741,11 @@ ENTITYALLOCATION(MovableObject)
17411741

17421742
protected:
17431743

1744+
/// <summary>
1745+
/// Gets a vector containing the script function names this class supports.
1746+
/// </summary>
1747+
/// <returns>A vector containing the script function names this class supports.</returns>
1748+
virtual const std::vector<std::string> GetSupportedScriptFunctionNames() { return std::vector<std::string> {"Create", "Destroy", "Update", "OnPieMenu", "OnEnableScript", "OnDisableScript"}; }
17441749

17451750
//////////////////////////////////////////////////////////////////////////////////////////
17461751
// Virtual method: UpdateChildMOIDs

Managers/LuaMan.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,16 @@ enum ServerResult
227227

228228
bool ExpressionIsTrue(std::string expression, bool consoleErrors);
229229

230+
/// <summary>
231+
/// Helper method to run the given function in all of the given preset instance's scripts on the given object instance. Note that this method does not do any safety checks.
232+
/// </summary>
233+
/// <param name="functionName">The name of the function to run.</param>
234+
/// <param name="scriptPath">The path to the script whose function is being run. Used as an identifier in Lua.</param>
235+
/// <param name="presetLuaInstanceName">The name of the lua variable that holds the representation of the preset instance whose function is being run.</param>
236+
/// <param name="objectLuaInstanceName">The name of the lua variable that holds the representation of the object instance the function is being run on.</param>
237+
/// <returns>Returns less than zero if any errors encountered when running this script. To get the actual error string, call GetLastError.</returns>
238+
int LuaMan::RunFunctionInPresetScript(std::string const &functionName, std::string const &scriptPath, std::string const &presetLuaInstanceName, std::string const &objectLuaInstanceName);
239+
230240

231241
//////////////////////////////////////////////////////////////////////////////////////////
232242
// Method: RunScriptString

0 commit comments

Comments
 (0)