From cc44e1ea69af8412811781ec510042551c163839 Mon Sep 17 00:00:00 2001 From: Syntriax Date: Sun, 3 Nov 2024 19:58:57 +0300 Subject: [PATCH] fix: OnEnterHierarchy Not Being Called Correctly --- Engine.Core/Behaviour.cs | 4 ++++ Engine.Core/GameManager.cs | 12 ++++++------ Engine.Core/GameObject.cs | 1 - 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/Engine.Core/Behaviour.cs b/Engine.Core/Behaviour.cs index 600feb2..4b20863 100644 --- a/Engine.Core/Behaviour.cs +++ b/Engine.Core/Behaviour.cs @@ -29,7 +29,11 @@ public abstract class Behaviour : BehaviourBase BehaviourController.OnUpdate += Update; BehaviourController.GameObject.OnEnteredHierarchy += EnteredHierarchy; BehaviourController.GameObject.OnExitedHierarchy += ExitedHierarchy; + OnInitialize(); + + if (GameObject.IsInHierarchy) + EnteredHierarchy(GameObject); } protected virtual void OnFinalize() { } diff --git a/Engine.Core/GameManager.cs b/Engine.Core/GameManager.cs index d19c3e3..73e25a7 100644 --- a/Engine.Core/GameManager.cs +++ b/Engine.Core/GameManager.cs @@ -61,12 +61,12 @@ public class GameManager : BaseEntity, IGameManager Register(gameObject); else { + if (!hierarchyObject.Initialize()) + throw new Exception($"{nameof(hierarchyObject)} can't be initialized"); + _hierarchyObjects.Add(hierarchyObject); hierarchyObject.EnterHierarchy(this); - if (!hierarchyObject.Initialize()) - throw new Exception($"{nameof(hierarchyObject)} can't be finalized"); - OnHierarchyObjectRegistered?.Invoke(this, hierarchyObject); } } @@ -140,15 +140,15 @@ public class GameManager : BaseEntity, IGameManager gameObject.OnFinalized += OnGameObjectFinalize; gameObject.OnExitedHierarchy += OnGameObjectExitedHierarchy; + if (!gameObject.Initialize()) + throw new Exception($"{nameof(gameObject)} can't be initialized"); + _gameObjects.Add(gameObject); _hierarchyObjects.Add(gameObject); if (!gameObject.EnterHierarchy(this)) throw new Exception($"{nameof(gameObject)} can't enter the hierarchy"); - if (!gameObject.Initialize()) - throw new Exception($"{nameof(gameObject)} can't be initialized"); - OnHierarchyObjectRegistered?.Invoke(this, gameObject); OnGameObjectRegistered?.Invoke(this, gameObject); } diff --git a/Engine.Core/GameObject.cs b/Engine.Core/GameObject.cs index 814936e..a82e692 100644 --- a/Engine.Core/GameObject.cs +++ b/Engine.Core/GameObject.cs @@ -46,7 +46,6 @@ public class GameObject : BaseEntity, IGameObject NotAssignedException.Check(this, _transform); NotAssignedException.Check(this, _behaviourController); - NotAssignedException.Check(this, _gameManager); } public void Update()