Add an Array.pop_at()
method to pop an element at an arbitrary index
Negative indices are supported to pop an element relative from the end.
This commit is contained in:
parent
98d55ba66f
commit
60116b17b5
4 changed files with 38 additions and 3 deletions
|
@ -535,8 +535,8 @@ void Array::push_front(const Variant &p_value) {
|
|||
|
||||
Variant Array::pop_back() {
|
||||
if (!_p->array.is_empty()) {
|
||||
int n = _p->array.size() - 1;
|
||||
Variant ret = _p->array.get(n);
|
||||
const int n = _p->array.size() - 1;
|
||||
const Variant ret = _p->array.get(n);
|
||||
_p->array.resize(n);
|
||||
return ret;
|
||||
}
|
||||
|
@ -545,13 +545,38 @@ Variant Array::pop_back() {
|
|||
|
||||
Variant Array::pop_front() {
|
||||
if (!_p->array.is_empty()) {
|
||||
Variant ret = _p->array.get(0);
|
||||
const Variant ret = _p->array.get(0);
|
||||
_p->array.remove(0);
|
||||
return ret;
|
||||
}
|
||||
return Variant();
|
||||
}
|
||||
|
||||
Variant Array::pop_at(int p_pos) {
|
||||
if (_p->array.is_empty()) {
|
||||
// Return `null` without printing an error to mimic `pop_back()` and `pop_front()` behavior.
|
||||
return Variant();
|
||||
}
|
||||
|
||||
if (p_pos < 0) {
|
||||
// Relative offset from the end
|
||||
p_pos = _p->array.size() + p_pos;
|
||||
}
|
||||
|
||||
ERR_FAIL_INDEX_V_MSG(
|
||||
p_pos,
|
||||
_p->array.size(),
|
||||
Variant(),
|
||||
vformat(
|
||||
"The calculated index %s is out of bounds (the array has %s elements). Leaving the array untouched and returning `null`.",
|
||||
p_pos,
|
||||
_p->array.size()));
|
||||
|
||||
const Variant ret = _p->array.get(p_pos);
|
||||
_p->array.remove(p_pos);
|
||||
return ret;
|
||||
}
|
||||
|
||||
Variant Array::min() const {
|
||||
Variant minval;
|
||||
for (int i = 0; i < size(); i++) {
|
||||
|
|
|
@ -97,6 +97,7 @@ public:
|
|||
void push_front(const Variant &p_value);
|
||||
Variant pop_back();
|
||||
Variant pop_front();
|
||||
Variant pop_at(int p_pos);
|
||||
|
||||
Array duplicate(bool p_deep = false) const;
|
||||
|
||||
|
|
|
@ -1807,6 +1807,7 @@ static void _register_variant_builtin_methods() {
|
|||
bind_method(Array, has, sarray("value"), varray());
|
||||
bind_method(Array, pop_back, sarray(), varray());
|
||||
bind_method(Array, pop_front, sarray(), varray());
|
||||
bind_method(Array, pop_at, sarray("position"), varray());
|
||||
bind_method(Array, sort, sarray(), varray());
|
||||
bind_method(Array, sort_custom, sarray("func"), varray());
|
||||
bind_method(Array, shuffle, sarray(), varray());
|
||||
|
|
|
@ -393,6 +393,14 @@
|
|||
<description>
|
||||
</description>
|
||||
</method>
|
||||
<method name="pop_at">
|
||||
<return type="Variant" />
|
||||
<argument index="0" name="position" type="int" />
|
||||
<description>
|
||||
Removes and returns the element of the array at index [code]position[/code]. If negative, [code]position[/code] is considered relative to the end of the array. Leaves the array untouched and returns [code]null[/code] if the array is empty or if it's accessed out of bounds. An error message is printed when the array is accessed out of bounds, but not when the array is empty.
|
||||
[b]Note:[/b] On large arrays, this method can be slower than [method pop_back] as it will reindex the array's elements that are located after the removed element. The larger the array and the lower the index of the removed element, the slower [method pop_at] will be.
|
||||
</description>
|
||||
</method>
|
||||
<method name="pop_back">
|
||||
<return type="Variant" />
|
||||
<description>
|
||||
|
|
Loading…
Reference in a new issue