Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functions Array::find_first() and Array::get_first() #70727

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions core/variant/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,56 @@ bool Array::any(const Callable &p_callable) const {
return false;
}

int Array::find_first(const Callable &p_callable, const int p_from) const {
const Variant *argptrs[1];

if (_p->array.size() == 0) {
return -1;
}

for (int i = p_from; i < size(); i++) {
argptrs[0] = &get(i);

Variant result;
Callable::CallError ce;
p_callable.callp(argptrs, 1, result, ce);
if (ce.error != Callable::CallError::CALL_OK) {
ERR_FAIL_V_MSG(false, "Error calling method from 'find first': " + Variant::get_callable_error_text(p_callable, argptrs, 1, ce));
}

if (result.operator bool()) {
return i;
}
}

return -1;
}

Variant Array::get_first(const Callable &p_callable, const Variant &p_default, const int p_from) const {
const Variant *argptrs[1];

if (_p->array.size() == 0 || p_from < 0) {
return -1;
}

for (int i = p_from; i < size(); i++) {
argptrs[0] = &get(i);

Variant result;
Callable::CallError ce;
p_callable.callp(argptrs, 1, result, ce);
if (ce.error != Callable::CallError::CALL_OK) {
ERR_FAIL_V_MSG(false, "Error calling method from 'get first': " + Variant::get_callable_error_text(p_callable, argptrs, 1, ce));
}

if (result.operator bool()) {
return get(i);
}
}

return p_default;
}

bool Array::all(const Callable &p_callable) const {
const Variant *argptrs[1];
for (int i = 0; i < size(); i++) {
Expand Down
2 changes: 2 additions & 0 deletions core/variant/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ class Array {
Array map(const Callable &p_callable) const;
Variant reduce(const Callable &p_callable, const Variant &p_accum) const;
bool any(const Callable &p_callable) const;
int find_first(const Callable &p_callable, const int p_from = 0) const;
Variant get_first(const Callable &p_callable, const Variant &p_default, const int p_from = 0) const;
bool all(const Callable &p_callable) const;

bool operator<(const Array &p_array) const;
Expand Down
2 changes: 2 additions & 0 deletions core/variant/variant_call.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2216,6 +2216,8 @@ static void _register_variant_builtin_methods() {
bind_method(Array, map, sarray("method"), varray());
bind_method(Array, reduce, sarray("method", "accum"), varray(Variant()));
bind_method(Array, any, sarray("method"), varray());
bind_method(Array, find_first, sarray("method", "from"), varray());
bind_method(Array, get_first, sarray("method", "default", "from"), varray());
bind_method(Array, all, sarray("method"), varray());
bind_method(Array, max, sarray(), varray());
bind_method(Array, min, sarray(), varray());
Expand Down
21 changes: 20 additions & 1 deletion doc/classes/Array.xml
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@
func greater_than_5(number):
return number &gt; 5
[/codeblock]
See also [method all], [method filter], [method map] and [method reduce].
See also [method all], [method filter], [method find_first], [method map] and [method reduce].
[b]Note:[/b] Unlike relying on the size of an array returned by [method filter], this method will return as early as possible to improve performance (especially with large arrays).
[b]Note:[/b] For an empty array, this method always returns [code]false[/code].
</description>
Expand Down Expand Up @@ -302,13 +302,32 @@
Searches the array for a value and returns its index or [code]-1[/code] if not found. Optionally, the initial search index can be passed.
</description>
</method>
<method name="find_first" qualifiers="const">
<return type="int" />
<param index="0" name="method" type="Callable" />
<param index="1" name="from" type="int" />
<description>
Returns the index of the first element that returns [code]true[/code] when passed to the provided [Callable].
If the [Callable] returns [code]false[/code] for all elements in the array, this method returns [code]-1[/code].
</description>
</method>
<method name="front" qualifiers="const">
<return type="Variant" />
<description>
Returns the first element of the array. Prints an error and returns [code]null[/code] if the array is empty.
[b]Note:[/b] Calling this function is not the same as writing [code]array[0][/code]. If the array is empty, accessing by index will pause project execution when running from the editor.
</description>
</method>
<method name="get_first" qualifiers="const">
<return type="Variant" />
<param index="0" name="method" type="Callable" />
<param index="1" name="default" type="Variant" />
<param index="2" name="from" type="int" />
<description>
Returns the first element that returns [code]true[/code] when passed to the provided [Callable].
If the [Callable] returns [code]false[/code] for all elements in the array, this method returns the given default value.
</description>
</method>
<method name="get_typed_builtin" qualifiers="const">
<return type="int" />
<description>
Expand Down