Document how to make custom min/max for Array
This commit is contained in:
parent
ba27db36e9
commit
96ab31a711
@ -441,12 +441,23 @@
|
|||||||
<return type="Variant" />
|
<return type="Variant" />
|
||||||
<description>
|
<description>
|
||||||
Returns the maximum value contained in the array if all elements are of comparable types. If the elements can't be compared, [code]null[/code] is returned.
|
Returns the maximum value contained in the array if all elements are of comparable types. If the elements can't be compared, [code]null[/code] is returned.
|
||||||
|
To find the maximum value using a custom comparator, you can use [method reduce]. In this example every array element is checked and the first maximum value is returned:
|
||||||
|
[codeblock]
|
||||||
|
func _ready():
|
||||||
|
var arr = [Vector2(0, 1), Vector2(2, 0), Vector2(1, 1), Vector2(1, 0), Vector2(0, 2)]
|
||||||
|
# In this example we compare the lengths.
|
||||||
|
print(arr.reduce(func(max, val): return val if is_length_greater(val, max) else max))
|
||||||
|
|
||||||
|
func is_length_greater(a, b):
|
||||||
|
return a.length() > b.length()
|
||||||
|
[/codeblock]
|
||||||
</description>
|
</description>
|
||||||
</method>
|
</method>
|
||||||
<method name="min" qualifiers="const">
|
<method name="min" qualifiers="const">
|
||||||
<return type="Variant" />
|
<return type="Variant" />
|
||||||
<description>
|
<description>
|
||||||
Returns the minimum value contained in the array if all elements are of comparable types. If the elements can't be compared, [code]null[/code] is returned.
|
Returns the minimum value contained in the array if all elements are of comparable types. If the elements can't be compared, [code]null[/code] is returned.
|
||||||
|
See also [method max] for an example of using a custom comparator.
|
||||||
</description>
|
</description>
|
||||||
</method>
|
</method>
|
||||||
<method name="pick_random" qualifiers="const">
|
<method name="pick_random" qualifiers="const">
|
||||||
|
Loading…
x
Reference in New Issue
Block a user