Created
March 31, 2025 12:17
-
-
Save HungryProton/d0521ab6d41c518378e7efb6ab093bdd to your computer and use it in GitHub Desktop.
Randomly play a bunch of sounds from a single node
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class_name LibraryPlayer3D | |
extends AudioStreamPlayer3D | |
## Randomly play different sounds | |
@export var audio_streams: Array[AudioStream] = [] | |
@export var volume_range := Vector2(-3, 3) | |
func _ready() -> void: | |
for in_stream in audio_streams: | |
var player := AudioStreamPlayer3D.new() | |
player.stream = in_stream | |
player.max_polyphony = max_polyphony | |
player.unit_size = unit_size | |
player.attenuation_model = attenuation_model | |
player.bus = bus | |
add_child(player) | |
func play_random() -> void: | |
var id: int = randi_range(0, get_child_count() - 1) | |
var audio_player: AudioStreamPlayer3D = get_child(id) | |
audio_player.volume_db = randi_range(floori(volume_range.x), ceili(volume_range.y)) | |
audio_player.play() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment