瀏覽代碼

Merge pull request #64781 from raulsntos/csharp_children

Add `includeInternal` to C# NodeExtensions and avoid printing errors in `GetChildOrNull`
Rémi Verschelde 3 年之前
父節點
當前提交
e1266d2f35
共有 2 個文件被更改,包括 16 次插入7 次删除
  1. 1 1
      doc/classes/Node.xml
  2. 15 6
      modules/mono/glue/GodotSharp/GodotSharp/Core/Extensions/NodeExtensions.cs

+ 1 - 1
doc/classes/Node.xml

@@ -241,7 +241,7 @@
 			<description>
 				Returns a child node by its index (see [method get_child_count]). This method is often used for iterating all children of a node.
 				Negative indices access the children from the last one.
-				If [param include_internal] is [code]true[/code], internal children are skipped (see [code]internal[/code] parameter in [method add_child]).
+				If [param include_internal] is [code]false[/code], internal children are skipped (see [code]internal[/code] parameter in [method add_child]).
 				To access a child node via its name, use [method get_node].
 			</description>
 		</method>

+ 15 - 6
modules/mono/glue/GodotSharp/GodotSharp/Core/Extensions/NodeExtensions.cs

@@ -93,8 +93,12 @@ namespace Godot
         /// Negative indices access the children from the last one.
         /// To access a child node via its name, use <see cref="GetNode"/>.
         /// </summary>
-        /// <seealso cref="GetChildOrNull{T}(int)"/>
+        /// <seealso cref="GetChildOrNull{T}(int, bool)"/>
         /// <param name="idx">Child index.</param>
+        /// <param name="includeInternal">
+        /// If <see langword="false"/>, internal children are skipped (see <c>internal</c>
+        /// parameter in <see cref="AddChild(Node, bool, InternalMode)"/>).
+        /// </param>
         /// <exception cref="InvalidCastException">
         /// Thrown when the given the fetched node can't be casted to the given type <typeparamref name="T"/>.
         /// </exception>
@@ -102,9 +106,9 @@ namespace Godot
         /// <returns>
         /// The child <see cref="Node"/> at the given index <paramref name="idx"/>.
         /// </returns>
-        public T GetChild<T>(int idx) where T : class
+        public T GetChild<T>(int idx, bool includeInternal = false) where T : class
         {
-            return (T)(object)GetChild(idx);
+            return (T)(object)GetChild(idx, includeInternal);
         }
 
         /// <summary>
@@ -113,15 +117,20 @@ namespace Godot
         /// Negative indices access the children from the last one.
         /// To access a child node via its name, use <see cref="GetNode"/>.
         /// </summary>
-        /// <seealso cref="GetChild{T}(int)"/>
+        /// <seealso cref="GetChild{T}(int, bool)"/>
         /// <param name="idx">Child index.</param>
+        /// <param name="includeInternal">
+        /// If <see langword="false"/>, internal children are skipped (see <c>internal</c>
+        /// parameter in <see cref="AddChild(Node, bool, InternalMode)"/>).
+        /// </param>
         /// <typeparam name="T">The type to cast to. Should be a descendant of <see cref="Node"/>.</typeparam>
         /// <returns>
         /// The child <see cref="Node"/> at the given index <paramref name="idx"/>, or <see langword="null"/> if not found.
         /// </returns>
-        public T GetChildOrNull<T>(int idx) where T : class
+        public T GetChildOrNull<T>(int idx, bool includeInternal = false) where T : class
         {
-            return GetChild(idx) as T;
+            int count = GetChildCount(includeInternal);
+            return idx >= -count && idx < count ? GetChild(idx, includeInternal) as T : null;
         }
 
         /// <summary>