浏览代码

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 年之前
父节点
当前提交
665621aa1b
共有 1 个文件被更改,包括 2 次插入1 次删除
  1. 2 1
      modules/mono/glue/GodotSharp/GodotSharp/Core/Extensions/NodeExtensions.cs

+ 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>