Awake(), Start() and Update(), OnDestroy() for scripts now all run as intended

This commit is contained in:
Kah Wei 2022-09-22 17:25:43 +08:00
parent 3a908b717b
commit 1bede86ff6
3 changed files with 9 additions and 25 deletions

View File

@ -38,18 +38,19 @@ namespace Sandbox
auto matInst = graphicsSystem->AddMaterialInstance();
// Create entity and add mesh
auto entity = SHADE::SHEntityManager::CreateEntity<SHADE::SHRenderable>();
auto& renderable = *SHADE::SHComponentManager::GetComponent_s<SHADE::SHRenderable>(entity);
testObj = SHADE::SHEntityManager::CreateEntity<SHADE::SHRenderable>();
auto& renderable = *SHADE::SHComponentManager::GetComponent_s<SHADE::SHRenderable>(testObj);
renderable.Mesh = CUBE_MESH;
renderable.SetMaterial(matInst);
renderable.TransformMatrix.Translate(0.0f, 0.0f, 2.0f);
SHADE::SHScriptEngine* scriptEngine = static_cast<SHADE::SHScriptEngine*>(SHADE::SHSystemManager::GetSystem<SHADE::SHScriptEngine>());
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()

View File

@ -9,6 +9,7 @@ namespace Sandbox
{
private:
EntityID camera;
EntityID testObj;
public:

View File

@ -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();