Browse Source

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.
Raul Santos 3 years ago
parent
commit
665621aa1b

+ 2 - 1
modules/mono/glue/GodotSharp/GodotSharp/Core/Extensions/NodeExtensions.cs

@@ -129,7 +129,8 @@ namespace Godot
         /// </returns>
         public T GetChildOrNull<T>(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;
         }
 
         /// <summary>