Added faulty registration to entityDestroyed event

This commit is contained in:
Kah Wei 2022-09-22 16:40:49 +08:00
parent 5bc24b09d4
commit 3b533ac03d
3 changed files with 56 additions and 14 deletions

View File

@ -16,9 +16,14 @@ of DigiPen Institute of Technology is prohibited.
// Standard Library // Standard Library
#include <fstream> // std::fstream #include <fstream> // std::fstream
#include <filesystem> // std::filesystem::canonical, std::filesystem::remove #include <filesystem> // std::filesystem::canonical, std::filesystem::remove
#include <memory> // std::shared_ptr
// Project Headers // Project Headers
#include "Tools/SHLogger.h" #include "Tools/SHLogger.h"
#include "Tools/SHStringUtils.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 namespace SHADE
{ {
@ -54,15 +59,9 @@ namespace SHADE
// Initialise the CSharp Engine // Initialise the CSharp Engine
csEngineInit(); csEngineInit();
// Link events // Register entity creation events
// - Entity Destruction registerEvents();
/*onEntityDestroy = [this](const SHEntity& e)
{
csScriptsRemoveAll(e.GetEID());
csGOLibNotifyDestroyEntity(e.GetEID());
};
ECS::OnEntityDestroy += onEntityDestroy;*/
} }
void SHScriptEngine::UnloadScriptAssembly() void SHScriptEngine::UnloadScriptAssembly()
{ {
@ -257,13 +256,23 @@ namespace SHADE
std::ofstream file(path); std::ofstream file(path);
if (!file.is_open()) if (!file.is_open())
throw std::runtime_error("Unable to create CsProj file!"); throw std::runtime_error("Unable to create CsProj file!");
// Fill the file // Fill the file
file << FILE_CONTENTS; file << FILE_CONTENTS;
// Close // Close
file.close(); file.close();
} }
/*-----------------------------------------------------------------------------------*/
/* Event Handler Functions */
/*-----------------------------------------------------------------------------------*/
SHEventHandle SHScriptEngine::onEntityDestroyed(SHEventPtr eventPtr)
{
auto eventData = reinterpret_cast<const SHEventSpec<SHEntityDestroyedEvent>*>(eventPtr.get());
csScriptsRemoveAll(eventData->data->eid);
return eventData->handle;
}
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
/* Helper Functions */ /* Helper Functions */
@ -385,6 +394,17 @@ namespace SHADE
);*/ );*/
} }
void SHScriptEngine::registerEvents()
{
// Register for entity destroyed event
std::shared_ptr<SHEventReceiverSpec<SHScriptEngine>> destroyedEventReceiver
{
std::make_shared<SHEventReceiverSpec<SHScriptEngine>>(this, &SHScriptEngine::onEntityDestroyed)
};
ReceiverPtr receiver = std::dynamic_pointer_cast<SHEventReceiver>(destroyedEventReceiver);
SHEventManager::SubscribeTo(SH_ENTITY_DESTROYED_EVENT, receiver);
}
void SHScriptEngine::dumpBuildLog(const std::string_view& buildLogPath) void SHScriptEngine::dumpBuildLog(const std::string_view& buildLogPath)
{ {
std::ifstream buildLog(buildLogPath); std::ifstream buildLog(buildLogPath);
@ -495,5 +515,4 @@ namespace SHADE
oss << " -o \"./tmp/\" -fl -flp:LogFile=build.log;Verbosity=quiet"; oss << " -o \"./tmp/\" -fl -flp:LogFile=build.log;Verbosity=quiet";
return oss.str(); return oss.str();
} }
} }

View File

@ -15,12 +15,14 @@ of DigiPen Institute of Technology is prohibited.
#include <filesystem> #include <filesystem>
// Project Headers // Project Headers
#include "SH_API.h"
#include "SHDotNetRuntime.h" #include "SHDotNetRuntime.h"
#include "ECS_Base/SHECSMacros.h" #include "ECS_Base/SHECSMacros.h"
#include "ECS_Base/Entity/SHEntity.h" #include "ECS_Base/Entity/SHEntity.h"
#include "ECS_Base/System/SHSystem.h" #include "ECS_Base/System/SHSystem.h"
#include "ECS_Base/System/SHSystemRoutine.h" #include "ECS_Base/System/SHSystemRoutine.h"
#include "SH_API.h" #include "Events/SHEventDefines.h"
#include "Events/SHEvent.h"
namespace SHADE namespace SHADE
{ {
@ -242,6 +244,11 @@ namespace SHADE
/*ECS::EntityEvent::Delegate onEntityCreate; /*ECS::EntityEvent::Delegate onEntityCreate;
ECS::EntityEvent::Delegate onEntityDestroy;*/ ECS::EntityEvent::Delegate onEntityDestroy;*/
/*-----------------------------------------------------------------------------*/
/* Event Handler Functions */
/*-----------------------------------------------------------------------------*/
SHEventHandle onEntityDestroyed(SHEventPtr eventPtr);
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
/* Helper Functions */ /* Helper Functions */
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
@ -250,6 +257,10 @@ namespace SHADE
/// </summary> /// </summary>
void loadFunctions(); void loadFunctions();
/// <summary> /// <summary>
/// Registers events for the scripting system
/// </summary>
void registerEvents();
/// <summary>
/// Reads the file via the specified path that represents a build log of error /// Reads the file via the specified path that represents a build log of error
/// and warning messages. /// and warning messages.
/// </summary> /// </summary>

View File

@ -4,8 +4,20 @@ public class TestScript : Script
{ {
public TestScript(GameObject gameObj) : base(gameObj) {} 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() protected override void update()
{ {
Debug.Log("TestScript.Update()"); Debug.Log("TestScript.Update()");
} }
protected override void onDestroy()
{
Debug.Log("TestScript.OnDestroy()");
}
} }