pick_random() for dictionaries #5796
Replies: 3 comments 1 reply
-
You should also be able to do pick_random_value() |
Beta Was this translation helpful? Give feedback.
-
I'd say that this isn't really worth adding a whole new method for, as it's very easy to work around with the suggested methods, and doesn't offer any performance benefits as you still need to iterate over the entries, it'd only save a very little performance margin over just using Further I'd argue that it's a very rare thing for people to need, further supporting that this isn't needed to in core, as it can be worked around so very easily |
Beta Was this translation helpful? Give feedback.
-
The PR godotengine/godot#104473 will make this possible. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
Recently the
pick_random()
method for arrays was introduced to the core (#3454). A shortcut for the patternarray[randi() % array.size()]
which we lived happily without for years. So now we have it an I welcome the outcome.From experience I immediately thought about having the same for dictionaries, where the same pattern is also frequently used. It would basically be a shortcut for
dict[ dict.keys().pick_random() ]
with the new syntax, ordict.get( dict.keys() [randi() % dict.size()] )
.Typical use case is spawning random events, where the event id and their correcponding scenes to be instanced are in a dict of the form
A short exchange in the commit section for 3.x occurred and suggested a few things:
pick_random()
functionality implemented for dictspick_random()
toget_random()
as it is effectively a shortcut of a getterHence this discussion, instead of a proposal, since I am (sorry, but I am) not going to implement any of this. I hereby suggest to get the same function for dicts with the semantics explained above:
dict.pick_random() == dict[ dict.keys() [randi() % dict.size() ] ]
.This discussion is therefore a thumbs up for the new shortcut for arrays and a suggestion to get it (right) also for dicts.
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions