From 1bede86ff60c9a3070f3bb46b7dea0bc4e6ae600 Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Thu, 22 Sep 2022 17:25:43 +0800 Subject: [PATCH] Awake(), Start() and Update(), OnDestroy() for scripts now all run as intended --- SHADE_Application/src/Scenes/SBTestScene.cpp | 9 ++++---- SHADE_Application/src/Scenes/SBTestScene.h | 1 + SHADE_Managed/src/Scripts/ScriptStore.cxx | 24 +++----------------- 3 files changed, 9 insertions(+), 25 deletions(-) diff --git a/SHADE_Application/src/Scenes/SBTestScene.cpp b/SHADE_Application/src/Scenes/SBTestScene.cpp index 072a15de..c59fa234 100644 --- a/SHADE_Application/src/Scenes/SBTestScene.cpp +++ b/SHADE_Application/src/Scenes/SBTestScene.cpp @@ -38,18 +38,19 @@ namespace Sandbox auto matInst = graphicsSystem->AddMaterialInstance(); // Create entity and add mesh - auto entity = SHADE::SHEntityManager::CreateEntity(); - auto& renderable = *SHADE::SHComponentManager::GetComponent_s(entity); + testObj = SHADE::SHEntityManager::CreateEntity(); + auto& renderable = *SHADE::SHComponentManager::GetComponent_s(testObj); renderable.Mesh = CUBE_MESH; renderable.SetMaterial(matInst); renderable.TransformMatrix.Translate(0.0f, 0.0f, 2.0f); SHADE::SHScriptEngine* scriptEngine = static_cast(SHADE::SHSystemManager::GetSystem()); - scriptEngine->AddScript(*SHADE::SHEntityManager::GetEntityByID(entity), "TestScript"); + scriptEngine->AddScript(*SHADE::SHEntityManager::GetEntityByID(testObj), "TestScript"); } void SBTestScene::Update(float dt) { (void)dt; - + if (GetKeyState(VK_SPACE) & 0x8000) + SHADE::SHEntityManager::DestroyEntity(testObj); } void SBTestScene::Render() diff --git a/SHADE_Application/src/Scenes/SBTestScene.h b/SHADE_Application/src/Scenes/SBTestScene.h index 6776c671..4d4a3207 100644 --- a/SHADE_Application/src/Scenes/SBTestScene.h +++ b/SHADE_Application/src/Scenes/SBTestScene.h @@ -9,6 +9,7 @@ namespace Sandbox { private: EntityID camera; + EntityID testObj; public: diff --git a/SHADE_Managed/src/Scripts/ScriptStore.cxx b/SHADE_Managed/src/Scripts/ScriptStore.cxx index 089da002..252ab071 100644 --- a/SHADE_Managed/src/Scripts/ScriptStore.cxx +++ b/SHADE_Managed/src/Scripts/ScriptStore.cxx @@ -287,14 +287,7 @@ namespace SHADE } void ScriptStore::RemoveAllScripts(Entity entity) { - SAFE_NATIVE_CALL_BEGIN - // Check if entity exists - if (!EntityUtils::IsValid(entity)) - { - Debug::LogError("[ScriptStore] Attempted to remove Scripts from an invalid Entity!"); - return; - } - + SAFE_NATIVE_CALL_BEGIN // Check if entity exists in the script storage if (!scripts.ContainsKey(entity)) return; @@ -311,13 +304,6 @@ namespace SHADE void ScriptStore::RemoveAllScriptsImmediately(Entity entity, bool callOnDestroy) { SAFE_NATIVE_CALL_BEGIN - // Check if entity exists - if (!EntityUtils::IsValid(entity)) - { - Debug::LogError("[ScriptStore] Attempted to remove Scripts from an invalid Entity!"); - return; - } - // Check if entity exists in the script storage if (!scripts.ContainsKey(entity)) return; @@ -386,10 +372,7 @@ namespace SHADE while (disposalQueue.Count > 0) { Script^ script = disposalQueue.Dequeue(); - /*if (Application::IsPlaying) - { - script->OnDestroy(); - }*/ + script->OnDestroy(); auto entity = script->Owner.GetEntity(); auto scriptList = scripts[script->Owner.GetEntity()]; scriptList->Remove(script); @@ -496,7 +479,6 @@ namespace SHADE // Check if entity exists, otherwise nothing if (!EntityUtils::IsValid(entity)) return true; - // Check if entity exists in the script storage if (!scripts.ContainsKey(entity)) @@ -667,7 +649,7 @@ namespace SHADE // Entity Validity Check if (nativeEntity == nullptr) - throw gcnew System::InvalidOperationException("Attempted to get native Component to an invalid Entity."); + return false; // Check active state return nativeEntity->GetActive();