diff --git a/Engine.Core/Abstract/IBehaviourController.cs b/Engine.Core/Abstract/IBehaviourController.cs index 02415d6..ddef1ca 100644 --- a/Engine.Core/Abstract/IBehaviourController.cs +++ b/Engine.Core/Abstract/IBehaviourController.cs @@ -51,6 +51,15 @@ public interface IBehaviourController : IAssignableGameObject, IEnumerableThe instantiated class after initialization. T AddBehaviour(params object?[]? args) where T : class, IBehaviour; + /// + /// Looks up and returns the that is controlled by the . + /// + /// An implemented class or + /// + /// , if the type of is present in the , if not. + /// + T? GetBehaviour(); + /// /// Looks up and tries to get the that is controlled by the . /// diff --git a/Engine.Core/BehaviourController.cs b/Engine.Core/BehaviourController.cs index 5d96e08..887b9f2 100644 --- a/Engine.Core/BehaviourController.cs +++ b/Engine.Core/BehaviourController.cs @@ -41,19 +41,19 @@ public class BehaviourController : IBehaviourController public T AddBehaviour(params object?[]? args) where T : class, IBehaviour => AddBehaviour(new Factory.BehaviourFactory().Instantiate(_gameObject, args)); - public bool TryGetBehaviour([NotNullWhen(returnValue: true)] out T? behaviour) + public T? GetBehaviour() { foreach (var behaviourItem in behaviours) - { - if (behaviourItem is not T result) - continue; + if (behaviourItem is T result) + return result; - behaviour = result; - return true; - } + return default; + } - behaviour = default; - return false; + public bool TryGetBehaviour([NotNullWhen(returnValue: true)] out T? behaviour) + { + behaviour = GetBehaviour(); + return behaviour is not null; } public IList GetBehaviours()