From 665621aa1b6b3555ec3aceaad3c6e216cdd8cfdc Mon Sep 17 00:00:00 2001 From: Raul Santos Date: Fri, 25 Feb 2022 01:08:09 +0100 Subject: Avoid printing an error in GetChildOrNull `GetChildOrNull` won't print an error when the given index is out of range, similar to how the LINQ `ElementAtOrDefault` method works. --- .../mono/glue/GodotSharp/GodotSharp/Core/Extensions/NodeExtensions.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Extensions/NodeExtensions.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Extensions/NodeExtensions.cs index e9d8b98344..df0e839866 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Extensions/NodeExtensions.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Extensions/NodeExtensions.cs @@ -129,7 +129,8 @@ namespace Godot /// public T GetChildOrNull(int idx, bool includeInternal = false) where T : class { - return GetChild(idx, includeInternal) as T; + int count = GetChildCount(includeInternal); + return idx >= -count && idx < count ? GetChild(idx, includeInternal) as T : null; } /// -- cgit v1.2.3