diff options
author | Max Hilbrunner <mhilbrunner@users.noreply.github.com> | 2022-10-21 16:25:52 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-21 16:25:52 +0200 |
commit | 5cefc07d908be50ac88ede8bce5572b030815695 (patch) | |
tree | 7141c95b9024f92d354c198dc54ce5c2e6000c88 /core/variant/array.cpp | |
parent | c4c35ab77a4090b0b8f813bce71fdf3053f65a05 (diff) | |
parent | 9f4dbf415da6d150788915cb35a9c3dc7f04c547 (diff) | |
download | redot-engine-5cefc07d908be50ac88ede8bce5572b030815695.tar.gz |
Merge pull request #67444 from nonunknown/array_pick_random
Add ability to pick random value from array
Diffstat (limited to 'core/variant/array.cpp')
-rw-r--r-- | core/variant/array.cpp | 6 |
1 files changed, 6 insertions, 0 deletions
diff --git a/core/variant/array.cpp b/core/variant/array.cpp index 8b958814db..c6bbd43dc4 100644 --- a/core/variant/array.cpp +++ b/core/variant/array.cpp @@ -31,6 +31,7 @@ #include "array.h" #include "container_type_validate.h" +#include "core/math/math_funcs.h" #include "core/object/class_db.h" #include "core/object/script_language.h" #include "core/templates/hashfuncs.h" @@ -299,6 +300,11 @@ Variant Array::back() const { 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 { ERR_FAIL_COND_V(!_p->typed.validate(p_value, "find"), -1); return _p->array.find(p_value, p_from); |