diff options
author | Yuri Sizov <11782833+YuriSizov@users.noreply.github.com> | 2023-04-10 20:09:52 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-10 20:09:52 +0200 |
commit | b5e784298961deca89762b4d3c391ca1013643c8 (patch) | |
tree | 1720294b173199463fb0734b3af418eb60980c0b /doc | |
parent | c48219f51e3d33a56e8a142db0ec73a30df8fb91 (diff) | |
parent | 96ab31a7117c110da025a39390bdecf36892e7fa (diff) | |
download | redot-engine-b5e784298961deca89762b4d3c391ca1013643c8.tar.gz |
Merge pull request #75625 from AThousandShips/min_max_example
Document how to make custom min/max for Array
Diffstat (limited to 'doc')
-rw-r--r-- | doc/classes/Array.xml | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/doc/classes/Array.xml b/doc/classes/Array.xml index cf542f333b..1c761f9f1a 100644 --- a/doc/classes/Array.xml +++ b/doc/classes/Array.xml @@ -441,12 +441,23 @@ <return type="Variant" /> <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. + 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> </method> <method name="min" qualifiers="const"> <return type="Variant" /> <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. + See also [method max] for an example of using a custom comparator. </description> </method> <method name="pick_random" qualifiers="const"> |