How to randomize audio

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

I’m making a game and I want it so that when the player dies the enemy taunts the player with 1 of 12 quotes. I wanted the quotes to be random so you don’t hear the same sentece said over and over. But I have zero clue to do that. I have a node called clip with 12 audiostreamplayers as its child all with the name clip with a number after it. I would love some help please

:bust_in_silhouette: Reply From: exuin

So first off you don’t need twelve audio players, you just need one. There’s a doc page that explains rng. Just load all the lines into an array and pick one at random.

:bust_in_silhouette: Reply From: timothybrentwood
var player = AudioStreamPlayer.new()
var audio_stream_array = [all, of, my, audio, stream, objects]
randomize()
var clip_to_play = audio_stream_array[randi() % audio_stream_array.size()]
player.set_stream(clip_to_play)
player.play()

player could be accessed from scene tree using something like $AudioStreamPlayer or get_node("AudioStreamPlayer") if that’s the way you’re used to doing so