Godot Version
4.3 (.net)
Question
Hi! I have an AudiostreamPlayer meant to play a footstep sound when you are walking. To prevent the sound from playing over itself (the polyphony is 1) I added an if statement that would prevent it from playing a new sound effect if one was already in the process of being played. The following code; however, results in the sound effect being played constantly while walking which constantly overrides the currently playing sound.
extends AudioStreamPlayer
var step_sound : AudioStreamWAV
enum step_type {WALKING,CROUCHING, SPRINTING}
var step := step_type.WALKING
var is_playing := false
var playlist_length := 1
@export_category("Footstep Playlists")
@export var snow_footsteps : Array[AudioStreamWAV]
@export var stair_footsteps : Array[AudioStreamWAV]
@export var waterpark_footsteps : Array[AudioStreamWAV]
# Called every frame. 'delta' is the elapsed time since the previous frame.
func play_step():
if not is_playing:
find_step_sound()
stream = step_sound
play()
func find_step_sound():
#IF HAVE TIME CONVERT EACH TO AN ARRAY OF SOUNDS OF THAT TYPE AND THEN PLAY ONE AT RANDOM
match GlobalData.level_id :
0:
step_sound = snow_footsteps.pick_random()
1:
step_sound = stair_footsteps.pick_random()
2:
step_sound = waterpark_footsteps.pick_random()
match step:
step_type.WALKING:
volume_db = 2
step_type.CROUCHING:
volume_db = 0
step_type.SPRINTING:
volume_db = 4
playlist_length = step_sound.MAX_STREAMS