First off - you probably don't want to put that in the _process
function. You might want to define your own functions instead.
Also, a slightly cleaner and less error-prone way of choosing a random one out of a fixed number of choices would be to put them in an array
, shuffle()
it, and access the first entry.
onready var players: Array = [$s1, $s2, $s3, $s4] # We use the onready keyword because we're dealing with referencing pre-existing nodes, I think
func _ready():
randomize()
func _random_player() -> AudioStreamPlayer:
players.shuffle()
return players[0]
To play from these four sounds non-stop, you could use a while
loop.
func play_sounds():
var i := 1
while i == 1:
var player := _random_player()
player.play()
# yield waits for the specified object (player) to emit the specified signal ("finished") before moving on
yield(player, "finished")
Call play_sounds()
in _ready()
for immediate autoplay when the parent node gets loaded. Also you didn't need to use a Control
node for audio, that's usually used for GUI stuff, a regular plain Node
would suffice.