Replies: 2 comments 1 reply
-
It sounds like this can be implemented fairly effectively in GDScript. While this will be a bit slower than a C++ implementation, I don't think the performance of a GDScript implementation will end up being a bottleneck in most cases. |
Beta Was this translation helpful? Give feedback.
1 reply
-
Now that the |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I propose adding
sample
andsample_size
methods toArray
. They would work like this:Output:
sample
would return one random element of the array.sample_size(n)
would return a randomly sampled array containingn
unique elements up to the size of the array.sample_size
is the more important of these two. Single sample can be get byarr[randi() % arr.size()]
, but I think that adding a method also for getting a single sample would be good for completeness sake. I think these kind of methods are very suitable for a game engine and they would have a lot of use.I am making a game where I need to spawn 3 items to random free cells in a grid. A nice way to pick the 3 cells would be to collect the free cells to an array and then pick the 3 cells using
sample_size
method. There is ashuffle
method which can be used for this. First shuffle the array and then pick 3 first items. But that would do a lot of unnecessary work as the whole array would be shuffled. A well implementedsample_size
method would shuffle only 3 elements instead of all the elements.For reference, Lodash (a Javascript library) has these
sample
andsampleSize
functions, here is the implementation ofsampleSize
: https://github.com/lodash/lodash/blob/2f79053d7bc7c9c9561a30dda202b3dcd2b72b90/sampleSize.jsBeta Was this translation helpful? Give feedback.
All reactions