mirror of
https://github.com/godotengine/godot.git
synced 2025-11-01 06:01:14 +00:00
Added max() and min() functions to array to return greater or lesser element (or null if data is not of compatible type or empty array). Closes #15697
This commit is contained in:
parent
df4dadafb7
commit
adc0188d9f
3 changed files with 54 additions and 0 deletions
|
|
@ -355,11 +355,58 @@ Variant Array::pop_front() {
|
|||
return Variant();
|
||||
}
|
||||
|
||||
Variant Array::min() const {
|
||||
|
||||
Variant minval;
|
||||
for (int i = 0; i < size(); i++) {
|
||||
if (i == 0) {
|
||||
minval = get(i);
|
||||
} else {
|
||||
bool valid;
|
||||
Variant ret;
|
||||
Variant test = get(i);
|
||||
Variant::evaluate(Variant::OP_LESS, test, minval, ret, valid);
|
||||
if (!valid) {
|
||||
return Variant(); //not a valid comparison
|
||||
}
|
||||
if (bool(ret)) {
|
||||
//is less
|
||||
minval = test;
|
||||
}
|
||||
}
|
||||
}
|
||||
return minval;
|
||||
}
|
||||
|
||||
Variant Array::max() const {
|
||||
|
||||
Variant maxval;
|
||||
for (int i = 0; i < size(); i++) {
|
||||
if (i == 0) {
|
||||
maxval = get(i);
|
||||
} else {
|
||||
bool valid;
|
||||
Variant ret;
|
||||
Variant test = get(i);
|
||||
Variant::evaluate(Variant::OP_GREATER, test, maxval, ret, valid);
|
||||
if (!valid) {
|
||||
return Variant(); //not a valid comparison
|
||||
}
|
||||
if (bool(ret)) {
|
||||
//is less
|
||||
maxval = test;
|
||||
}
|
||||
}
|
||||
}
|
||||
return maxval;
|
||||
}
|
||||
|
||||
Array::Array(const Array &p_from) {
|
||||
|
||||
_p = NULL;
|
||||
_ref(p_from);
|
||||
}
|
||||
|
||||
Array::Array() {
|
||||
|
||||
_p = memnew(ArrayPrivate);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue