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.
This commit is contained in:
Raul Santos 2022-02-25 01:08:09 +01:00
parent 7924d643e5
commit 665621aa1b
No known key found for this signature in database
GPG key ID: B532473AE3A803E4

View file

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