From cd30047e4aacbd022eef660786b6eb508251ba7a Mon Sep 17 00:00:00 2001 From: Syntriax Date: Wed, 28 May 2025 16:55:38 +0300 Subject: [PATCH] feat: GetOrAddBehaviour with fallback type added --- .../Extensions/BehaviourControllerExtensions.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Engine.Core/Extensions/BehaviourControllerExtensions.cs b/Engine.Core/Extensions/BehaviourControllerExtensions.cs index 6778065..3f71685 100644 --- a/Engine.Core/Extensions/BehaviourControllerExtensions.cs +++ b/Engine.Core/Extensions/BehaviourControllerExtensions.cs @@ -39,6 +39,19 @@ public static class BehaviourControllerExtensions public static T GetOrAddBehaviour(this IBehaviourController behaviourController, params object?[]? args) where T : class, IBehaviour => behaviourController.GetBehaviour() ?? behaviourController.AddBehaviour(args); + /// + /// Gets an existing of the specified type, or adds and returns the fallback type if it doesn't exist. + /// + /// The type of to get. + /// The type of to add. It must be assignable from + /// The to search in. + /// Optional arguments to pass to the constructor of the if a new one is added. + /// The existing or newly added of the specified type. + public static TOriginal GetOrAddBehaviour(this IBehaviourController behaviourController, params object?[]? args) + where TOriginal : class + where TFallback : class, IBehaviour, TOriginal + => behaviourController.GetBehaviour() ?? behaviourController.AddBehaviour(args); + /// /// Tries to get a of the specified type in it's 's parents recursively. ///