Audiostream is_playing variable does not function how expected

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

you are currently using your own boolean variable is_playing, which is set to false initially but it is never changed to true.

what you might want to do instead is using the playing variable which is already part of the AudioStreamPlayer class. In this case the check would look like this:

if not playing: 
	#...

and that, ladies and gentlemen, is why I shouldn’t be allowed to code at 3am. I had initially fixed this issue with that function and then whenever I used ctrl+z to undo some other stuff I undid to the point at which I was using my own variable. Sorry for wasting the forum’s time with such a stupid issue.

don’t worry – sometimes you just need a second pair of eyes to have a look at your code

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.