diff --git a/SHADE_Engine/src/Scripting/SHScriptEngine.cpp b/SHADE_Engine/src/Scripting/SHScriptEngine.cpp index 56f93812..fada5b70 100644 --- a/SHADE_Engine/src/Scripting/SHScriptEngine.cpp +++ b/SHADE_Engine/src/Scripting/SHScriptEngine.cpp @@ -16,9 +16,14 @@ of DigiPen Institute of Technology is prohibited. // Standard Library #include // std::fstream #include // std::filesystem::canonical, std::filesystem::remove +#include // std::shared_ptr // Project Headers #include "Tools/SHLogger.h" #include "Tools/SHStringUtils.h" +#include "ECS_Base/Events/SHEntityDestroyedEvent.h" +#include "Events/SHEvent.h" +#include "Events/SHEventReceiver.h" +#include "Events/SHEventManager.hpp" namespace SHADE { @@ -54,15 +59,9 @@ namespace SHADE // Initialise the CSharp Engine csEngineInit(); - - // Link events - // - Entity Destruction - /*onEntityDestroy = [this](const SHEntity& e) - { - csScriptsRemoveAll(e.GetEID()); - csGOLibNotifyDestroyEntity(e.GetEID()); - }; - ECS::OnEntityDestroy += onEntityDestroy;*/ + + // Register entity creation events + registerEvents(); } void SHScriptEngine::UnloadScriptAssembly() { @@ -257,13 +256,23 @@ namespace SHADE std::ofstream file(path); if (!file.is_open()) throw std::runtime_error("Unable to create CsProj file!"); - + // Fill the file file << FILE_CONTENTS; - + // Close file.close(); - } + } + + /*-----------------------------------------------------------------------------------*/ + /* Event Handler Functions */ + /*-----------------------------------------------------------------------------------*/ + SHEventHandle SHScriptEngine::onEntityDestroyed(SHEventPtr eventPtr) + { + auto eventData = reinterpret_cast*>(eventPtr.get()); + csScriptsRemoveAll(eventData->data->eid); + return eventData->handle; + } /*-----------------------------------------------------------------------------------*/ /* Helper Functions */ @@ -385,6 +394,17 @@ namespace SHADE );*/ } + void SHScriptEngine::registerEvents() + { + // Register for entity destroyed event + std::shared_ptr> destroyedEventReceiver + { + std::make_shared>(this, &SHScriptEngine::onEntityDestroyed) + }; + ReceiverPtr receiver = std::dynamic_pointer_cast(destroyedEventReceiver); + SHEventManager::SubscribeTo(SH_ENTITY_DESTROYED_EVENT, receiver); + } + void SHScriptEngine::dumpBuildLog(const std::string_view& buildLogPath) { std::ifstream buildLog(buildLogPath); @@ -495,5 +515,4 @@ namespace SHADE oss << " -o \"./tmp/\" -fl -flp:LogFile=build.log;Verbosity=quiet"; return oss.str(); } - } diff --git a/SHADE_Engine/src/Scripting/SHScriptEngine.h b/SHADE_Engine/src/Scripting/SHScriptEngine.h index fa205266..0994bb5d 100644 --- a/SHADE_Engine/src/Scripting/SHScriptEngine.h +++ b/SHADE_Engine/src/Scripting/SHScriptEngine.h @@ -15,12 +15,14 @@ of DigiPen Institute of Technology is prohibited. #include // Project Headers +#include "SH_API.h" #include "SHDotNetRuntime.h" #include "ECS_Base/SHECSMacros.h" #include "ECS_Base/Entity/SHEntity.h" #include "ECS_Base/System/SHSystem.h" #include "ECS_Base/System/SHSystemRoutine.h" -#include "SH_API.h" +#include "Events/SHEventDefines.h" +#include "Events/SHEvent.h" namespace SHADE { @@ -242,6 +244,11 @@ namespace SHADE /*ECS::EntityEvent::Delegate onEntityCreate; ECS::EntityEvent::Delegate onEntityDestroy;*/ + /*-----------------------------------------------------------------------------*/ + /* Event Handler Functions */ + /*-----------------------------------------------------------------------------*/ + SHEventHandle onEntityDestroyed(SHEventPtr eventPtr); + /*-----------------------------------------------------------------------------*/ /* Helper Functions */ /*-----------------------------------------------------------------------------*/ @@ -250,6 +257,10 @@ namespace SHADE /// void loadFunctions(); /// + /// Registers events for the scripting system + /// + void registerEvents(); + /// /// Reads the file via the specified path that represents a build log of error /// and warning messages. /// diff --git a/TempScriptsFolder/TestScript.cs b/TempScriptsFolder/TestScript.cs index 0e4823a2..c1ed5bd5 100644 --- a/TempScriptsFolder/TestScript.cs +++ b/TempScriptsFolder/TestScript.cs @@ -4,8 +4,20 @@ public class TestScript : Script { public TestScript(GameObject gameObj) : base(gameObj) {} + protected override void awake() + { + Debug.Log("TestScript.Awake()"); + } + protected override void start() + { + Debug.Log("TestScript.Start()"); + } protected override void update() { Debug.Log("TestScript.Update()"); } + protected override void onDestroy() + { + Debug.Log("TestScript.OnDestroy()"); + } } \ No newline at end of file