You've already forked godot
mirror of
https://github.com/godotengine/godot.git
synced 2025-11-29 16:16:38 +00:00
add ability to pick random value from array
This commit is contained in:
@@ -31,6 +31,7 @@
|
|||||||
#include "array.h"
|
#include "array.h"
|
||||||
|
|
||||||
#include "container_type_validate.h"
|
#include "container_type_validate.h"
|
||||||
|
#include "core/math/math_funcs.h"
|
||||||
#include "core/object/class_db.h"
|
#include "core/object/class_db.h"
|
||||||
#include "core/object/script_language.h"
|
#include "core/object/script_language.h"
|
||||||
#include "core/templates/hashfuncs.h"
|
#include "core/templates/hashfuncs.h"
|
||||||
@@ -299,6 +300,11 @@ Variant Array::back() const {
|
|||||||
return operator[](_p->array.size() - 1);
|
return operator[](_p->array.size() - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Variant Array::pick_random() const {
|
||||||
|
ERR_FAIL_COND_V_MSG(_p->array.size() == 0, Variant(), "Can't take value from empty array.");
|
||||||
|
return operator[](Math::rand() % _p->array.size());
|
||||||
|
}
|
||||||
|
|
||||||
int Array::find(const Variant &p_value, int p_from) const {
|
int Array::find(const Variant &p_value, int p_from) const {
|
||||||
ERR_FAIL_COND_V(!_p->typed.validate(p_value, "find"), -1);
|
ERR_FAIL_COND_V(!_p->typed.validate(p_value, "find"), -1);
|
||||||
return _p->array.find(p_value, p_from);
|
return _p->array.find(p_value, p_from);
|
||||||
|
|||||||
@@ -79,6 +79,7 @@ public:
|
|||||||
|
|
||||||
Variant front() const;
|
Variant front() const;
|
||||||
Variant back() const;
|
Variant back() const;
|
||||||
|
Variant pick_random() const;
|
||||||
|
|
||||||
void sort();
|
void sort();
|
||||||
void sort_custom(const Callable &p_callable);
|
void sort_custom(const Callable &p_callable);
|
||||||
|
|||||||
@@ -2053,6 +2053,7 @@ static void _register_variant_builtin_methods() {
|
|||||||
bind_method(Array, erase, sarray("value"), varray());
|
bind_method(Array, erase, sarray("value"), varray());
|
||||||
bind_method(Array, front, sarray(), varray());
|
bind_method(Array, front, sarray(), varray());
|
||||||
bind_method(Array, back, sarray(), varray());
|
bind_method(Array, back, sarray(), varray());
|
||||||
|
bind_method(Array, pick_random, sarray(), varray());
|
||||||
bind_method(Array, find, sarray("what", "from"), varray(0));
|
bind_method(Array, find, sarray("what", "from"), varray(0));
|
||||||
bind_method(Array, rfind, sarray("what", "from"), varray(-1));
|
bind_method(Array, rfind, sarray("what", "from"), varray(-1));
|
||||||
bind_method(Array, find_last, sarray("value"), varray());
|
bind_method(Array, find_last, sarray("value"), varray());
|
||||||
|
|||||||
@@ -429,6 +429,16 @@
|
|||||||
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.
|
||||||
</description>
|
</description>
|
||||||
</method>
|
</method>
|
||||||
|
<method name="pick_random" qualifiers="const">
|
||||||
|
<return type="Variant" />
|
||||||
|
<description>
|
||||||
|
Returns a random value from the target array.
|
||||||
|
[codeblock]
|
||||||
|
var array: Array[int] = [1, 2, 3, 4]
|
||||||
|
print(array.pick_random()) # Prints either of the four numbers.
|
||||||
|
[/codeblock]
|
||||||
|
</description>
|
||||||
|
</method>
|
||||||
<method name="pop_at">
|
<method name="pop_at">
|
||||||
<return type="Variant" />
|
<return type="Variant" />
|
||||||
<param index="0" name="position" type="int" />
|
<param index="0" name="position" type="int" />
|
||||||
|
|||||||
Reference in New Issue
Block a user