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

Commit

Permalink
Send messages to activity
Browse files Browse the repository at this point in the history
  • Loading branch information
Causeless committed Nov 5, 2023
1 parent 3d51de1 commit 31aa270
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 28 deletions.
46 changes: 19 additions & 27 deletions Activities/GAScripted.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,8 @@ int GAScripted::ReadProperty(const std::string_view &propName, Reader &reader) {
// later recreation with Create(Reader &reader);

int GAScripted::Save(Writer &writer) const {
// Call the script OnSave() function, if it exists
auto saveItr = m_ScriptFunctions.find("OnSave");
if (saveItr != m_ScriptFunctions.end()) {
g_LuaMan.GetMasterScriptState().RunScriptFunctionObject(saveItr->second.get(), "_G", m_LuaClassName, {}, {}, {});
}
// Hmm. We should probably be calling this prior to the writer Save, instead of const-casting.
const_cast<GAScripted*>(this)->RunLuaFunction("OnSave");

GameActivity::Save(writer);

Expand Down Expand Up @@ -307,16 +304,13 @@ int GAScripted::Start() {
}

// Run the file that specifies the Lua functions for this' operating logic
if (ReloadScripts() < 0) {
if ((error = ReloadScripts()) < 0) {
return error;
}

// Call the create function, but only after first checking if it exists
auto createItr = m_ScriptFunctions.find("StartActivity");
if (createItr != m_ScriptFunctions.end()) {
if ((error = g_LuaMan.GetMasterScriptState().RunScriptFunctionObject(createItr->second.get(), "_G", m_LuaClassName, {}, { initialActivityState == ActivityState::NotStarted ? "true" : "false" }, {})) < 0) {
return error;
}
// Call the create function
if ((error = RunLuaFunction("StartActivity", {}, { initialActivityState == ActivityState::NotStarted ? "true" : "false" }, {})) < 0) {
return error;
}

// Clear active global scripts
Expand Down Expand Up @@ -358,11 +352,7 @@ int GAScripted::Start() {
void GAScripted::SetPaused(bool pause) {
GameActivity::SetPaused(pause);

// Call the defined function, but only after first checking if it exists
auto pauseItr = m_ScriptFunctions.find("PauseActivity");
if (pauseItr != m_ScriptFunctions.end()) {
g_LuaMan.GetMasterScriptState().RunScriptFunctionObject(pauseItr->second.get(), "_G", m_LuaClassName, {}, { pause ? "true" : "false" }, {});
}
RunLuaFunction("PauseActivity", {}, { pause ? "true" : "false" }, {});

// Pause all global scripts
for (std::vector<GlobalScript *>::iterator sItr = m_GlobalScriptsList.begin(); sItr < m_GlobalScriptsList.end(); ++sItr) {
Expand All @@ -381,11 +371,7 @@ void GAScripted::SetPaused(bool pause) {
void GAScripted::End() {
GameActivity::End();

// Call the defined function, but only after first checking if it exists
auto endItr = m_ScriptFunctions.find("EndActivity");
if (endItr != m_ScriptFunctions.end()) {
g_LuaMan.GetMasterScriptState().RunScriptFunctionObject(endItr->second.get(), "_G", m_LuaClassName, {}, {}, {});
}
RunLuaFunction("EndActivity");

// End all global scripts
for (std::vector<GlobalScript *>::iterator sItr = m_GlobalScriptsList.begin(); sItr < m_GlobalScriptsList.end(); ++sItr) {
Expand Down Expand Up @@ -442,11 +428,7 @@ void GAScripted::Update() {
if (m_ActivityState != ActivityState::Over) {
AddPieSlicesToActiveActorPieMenus();

// Call the defined function, but only after first checking if it exists
auto updateItr = m_ScriptFunctions.find("UpdateActivity");
if (updateItr != m_ScriptFunctions.end()) {
g_LuaMan.GetMasterScriptState().RunScriptFunctionObject(updateItr->second.get(), "_G", m_LuaClassName, {}, {}, {});
}
RunLuaFunction("UpdateActivity");

UpdateGlobalScripts(false);
}
Expand Down Expand Up @@ -488,6 +470,16 @@ void GAScripted::Draw(BITMAP *pTargetBitmap, const Vector &targetPos) {
GameActivity::Draw(pTargetBitmap, targetPos);
}

int GAScripted::RunLuaFunction(const std::string& functionName, const std::vector<const Entity*>& functionEntityArguments, const std::vector<std::string_view>& functionLiteralArguments, const std::vector<LuabindObjectWrapper*>& functionObjectArguments) {
// Call the defined function, but only after first checking if it exists
auto funcItr = m_ScriptFunctions.find(functionName);
if (funcItr == m_ScriptFunctions.end()) {
return 0;
}

return g_LuaMan.GetMasterScriptState().RunScriptFunctionObject(funcItr->second.get(), "_G", m_LuaClassName, functionEntityArguments, functionLiteralArguments, functionObjectArguments);
}


//////////////////////////////////////////////////////////////////////////////////////////
// Virtual method: CollectRequiredAreas
Expand Down
3 changes: 2 additions & 1 deletion Activities/GAScripted.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class GAScripted : public GameActivity {

public:

ScriptFunctionNames("StartActivity", "UpdateActivity", "PauseActivity", "EndActivity", "OnSave", "CraftEnteredOrbit");
ScriptFunctionNames("StartActivity", "UpdateActivity", "PauseActivity", "EndActivity", "OnSave", "CraftEnteredOrbit", "OnMessage");

// Concrete allocation and cloning definitions
EntityAllocation(GAScripted);
Expand Down Expand Up @@ -245,6 +245,7 @@ ClassInfoGetters;

void Draw(BITMAP *pTargetBitmap, const Vector& targetPos = Vector()) override;

int RunLuaFunction(const std::string& functionName, const std::vector<const Entity*>& functionEntityArguments = std::vector<const Entity*>(), const std::vector<std::string_view>& functionLiteralArguments = std::vector<std::string_view>(), const std::vector<LuabindObjectWrapper*>& functionObjectArguments = std::vector<LuabindObjectWrapper*>());

//////////////////////////////////////////////////////////////////////////////////////////
// Protected member variable and method declarations
Expand Down
7 changes: 7 additions & 0 deletions Lua/LuaAdapterDefinitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,13 @@ namespace RTE {
};
#pragma endregion

#pragma region Activity Lua Adapters
struct LuaAdaptersActivity {
static void SendMessage1(Activity *luaSelfObject, const std::string &message);
static void SendMessage2(Activity *luaSelfObject, const std::string &message, luabind::object context);
};
#pragma endregion

#pragma region MovableObject Lua Adapters
struct LuaAdaptersMovableObject {
static bool HasScript(MovableObject *luaSelfObject, const std::string &scriptPath);
Expand Down
19 changes: 19 additions & 0 deletions Lua/LuaAdapters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,25 @@ namespace RTE {
return luaSelfObject->GetTotalValue(nativeModule, foreignMult, 1.0F);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void LuaAdaptersActivity::SendMessage1(Activity *luaSelfObject, const std::string &message) {
GAScripted* scriptedActivity = dynamic_cast<GAScripted*>(luaSelfObject);
if (scriptedActivity) {
scriptedActivity->RunLuaFunction("OnMessage", {}, { message });
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void LuaAdaptersActivity::SendMessage2(Activity*luaSelfObject, const std::string &message, luabind::object context) {
GAScripted* scriptedActivity = dynamic_cast<GAScripted*>(luaSelfObject);
if (scriptedActivity) {
LuabindObjectWrapper wrapper(&context, "", false);
scriptedActivity->RunLuaFunction("OnMessage", {}, { message }, { &wrapper });
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

bool LuaAdaptersMovableObject::HasScript(MovableObject *luaSelfObject, const std::string &scriptPath) {
Expand Down
2 changes: 2 additions & 0 deletions Lua/LuaBindingsActivities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ namespace RTE {
.def("LoadString", &Activity::LoadString)
.def("SaveNumber", &Activity::SaveNumber)
.def("LoadNumber", &Activity::LoadNumber)
.def("SendMessage", &LuaAdaptersActivity::SendMessage1)
.def("SendMessage", &LuaAdaptersActivity::SendMessage2)

.enum_("Players")[
luabind::value("PLAYER_NONE", Players::NoPlayer),
Expand Down

0 comments on commit 31aa270

Please sign in to comment.