how to sort a variable from a array in godot

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By random_developer

So, I think the title speaks for itself.

I just want to know how I randomly choose a variable from array in godot…

extra: I wanted to know more about the randomize() function

:bust_in_silhouette: Reply From: Gluon

You can use something like this

arrayname[randi() % arrayname.size()]

You can see all the documentation for random number generation in the below link

it really worked, thanks for the answer and the documentation

random_developer | 2022-11-29 22:47

You are welcome good luck with your game

Gluon | 2022-11-29 23:12

Also, FYI… In Godot 4, this is even easier. The Array object itself has apick_random() function for exactly this purpose.

Array — Godot Engine (latest) documentation in English

jgodfrey | 2022-11-29 23:59

good to know that, i dont know if i gonna use godot 4 soon but, i will keep that in mind… thanks.

random_developer | 2022-11-30 02:16