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

Commit 3aff5e1

Browse files
committed
Fixed adding more supported script function names in Actor, and added OnAttach and OnDetach in attachable
Added TempEntityVector to LuaMan, which is used to cleverly pass arguments from cpp to lua for these new MO functions. Also made LuaMan::TempEntity readonly from lua
1 parent 810d35b commit 3aff5e1

File tree

6 files changed

+37
-18
lines changed

6 files changed

+37
-18
lines changed

Entities/Actor.cpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,18 +1285,20 @@ bool Actor::UpdateMovePath()
12851285
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
12861286

12871287
bool Actor::UpdateAIScripted() {
1288-
if (!m_ScriptedAIUpdate || (m_LoadedScripts.empty()) || m_ScriptPresetName.empty()) {
1288+
if (!m_ScriptedAIUpdate || m_LoadedScripts.empty() || m_ScriptPresetName.empty()) {
12891289
return false;
12901290
}
12911291

12921292
// Check to make sure the preset of this is still defined in the Lua state. If not, re-create it and recover gracefully
12931293
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.
1294+
RTEAbort("Trying to run UpdateAIScripted with no ScriptPresetName. This shouldn't happen, please let a dev know.")
1295+
//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.
12951296
}
12961297

12971298
// If we don't have a Lua representation for this object instance, create one and call the Lua Create function on it
12981299
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.
1300+
RTEAbort("Trying to run UpdateAIScripted with no ScriptObjectName. This shouldn't happen, please let a dev know.")
1301+
/*//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.
13001302
m_ScriptObjectName = GetClassName() + "s." + g_LuaMan.GetNewObjectID();
13011303
13021304
// Give Lua access to this object, then use that access to set up the object's Lua representation
@@ -1309,17 +1311,11 @@ bool Actor::UpdateAIScripted() {
13091311
if (g_LuaMan.RunFunctionInPresetScript("Create", scriptEntry.first, m_ScriptPresetName, m_ScriptObjectName) < 0) {
13101312
return -3;
13111313
}
1312-
}
1314+
}*/
13131315
}
13141316

13151317
g_FrameMan.StartPerformanceMeasurement(FrameMan::PERF_ACTORS_AI);
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-
}
1318+
bool aiUpdateSucceeded = RunScriptedFunctionInAppropriateScripts("UpdateAI", false, true) < 0;
13231319
g_FrameMan.StopPerformanceMeasurement(FrameMan::PERF_ACTORS_AI);
13241320

13251321
return aiUpdateSucceeded;

Entities/Actor.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,12 +1327,6 @@ ENTITYALLOCATION(Actor)
13271327

13281328
protected:
13291329

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; }
1335-
13361330
// Member variables
13371331
static Entity::ClassInfo m_sClass;
13381332

@@ -1540,6 +1534,12 @@ ENTITYALLOCATION(Actor)
15401534
// Timer for measuring interval between height checks
15411535
Timer m_FallTimer;
15421536

1537+
/// <summary>
1538+
/// Gets a vector containing the script function names this class supports.
1539+
/// </summary>
1540+
/// <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; }
1542+
15431543
//////////////////////////////////////////////////////////////////////////////////////////
15441544
// Private member variable and method declarations
15451545

Entities/Attachable.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,10 @@ void Attachable::Attach(MOSRotating *pParent)
273273

274274
// Reset the attachables timers so things that have been sitting in inventory don't make backed up emissions
275275
ResetAllTimers();
276+
277+
if (m_pParent != NULL) {
278+
RunScriptedFunctionInAppropriateScripts("OnAttach", false, false, {m_pParent});
279+
}
276280
}
277281

278282

@@ -290,6 +294,7 @@ void Attachable::Detach()
290294
}
291295

292296
m_Team = -1;
297+
MOSRotating *temporaryParent = m_pParent;
293298
m_pParent = 0;
294299
// Since it's no longer atteched it should belong to itself
295300
m_RootMOID = m_MOID;
@@ -300,6 +305,10 @@ void Attachable::Detach()
300305
#endif
301306

302307
m_RestTimer.Reset();
308+
309+
if (temporaryParent != NULL) {
310+
RunScriptedFunctionInAppropriateScripts("OnDetach", false, false, {temporaryParent});
311+
}
303312
}
304313

305314

Entities/Attachable.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,12 @@ ENTITYALLOCATION(Attachable)
700700
// Whether this attachable currently has terrain collisions enabled while it's attached to a parent.
701701
bool m_IsCollidingWithTerrainWhileAttached;
702702

703+
/// <summary>
704+
/// Gets a vector containing the script function names this class supports.
705+
/// </summary>
706+
/// <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; }
708+
703709

704710
//////////////////////////////////////////////////////////////////////////////////////////
705711
// Private member variable and method declarations

Managers/LuaMan.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2197,7 +2197,8 @@ int LuaMan::Create()
21972197
.property("ScreenSize", &ConsoleMan::GetConsoleScreenSize, &ConsoleMan::SetConsoleScreenSize),
21982198

21992199
class_<LuaMan>("LuaManager")
2200-
.property("TempEntity", &LuaMan::GetTempEntity, &LuaMan::SetTempEntity)
2200+
.property("TempEntity", &LuaMan::GetTempEntity)
2201+
.def_readonly("TempEntities", &LuaMan::m_TempEntityVector, return_stl_iterator)
22012202
.def("FileOpen", &LuaMan::FileOpen)
22022203
.def("FileClose", &LuaMan::FileClose)
22032204
.def("FileReadLine", &LuaMan::FileReadLine)

Managers/LuaMan.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,11 @@ enum ServerResult
295295

296296
Entity * GetTempEntity() const { return m_pTempEntity; }
297297

298+
/// <summary>
299+
/// Sets a temporary vector of entities that can be accessed in the Lua state
300+
/// </summary>
301+
/// <param name="entityVector">The temporary vector of entities. Ownership is NOT transferred.</param>
302+
void SetTempEntityVector(std::vector<Entity *> entityVector) { m_TempEntityVector = entityVector; }
298303

299304
//////////////////////////////////////////////////////////////////////////////////////////
300305
// Method: Update
@@ -398,6 +403,8 @@ enum ServerResult
398403
long m_NextObjectID;
399404
// Temporary holder for an Entity object that we want to pass into the Lua state without fuss
400405
Entity *m_pTempEntity;
406+
// Temporary holder for a vector of Entities that we want to pass into the Lua state without a fuss. Usually used to pass arguments to special Lua functions.
407+
std::vector<Entity *> m_TempEntityVector;
401408

402409

403410
//////////////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)