Elevator sound playing twice, position problem

Godot Version

441

Question

hello,
I have sound that plays when an elevator reaches it position
most of the times it works… in one ocasion the sound plays twice, ( 2X louder )

I cant understand how this is possible, because the code returns to state 0 once the position is reached and the sound is played ?

		0:
			offset.y = 0;
			if ( callBtnsActive == true ):
				print("here 2")
				elevState = 6;
			if ( playerIN == true && Input.is_action_just_pressed("key_up") ):
				elevState = 1;
				Snd.playSnd(Snd.elevEnd);
				
		3:
			position = position.move_toward( MoveToSpawn , speed * delta );
			offset.y = -fixFeetOffset;
			if ( position == MoveToSpawn ):
				print("here 1")
				elevState = 0; ############
				Snd.playSnd(Snd.elevEnd);#################---plays twice

the state is 0 why is the sound playing twice ?

Snd Golbal script

const elevEnd = preload('res://sounds/elevEnd.wav');

func playSnd( _sndEffect ):
	var _snd = audioStr.instantiate();
	add_child(_snd);
	_snd.stream = _sndEffect;
	_snd.play();

print

here 1
here 1
here 2
here 2

What does this code do?

var _snd = audioStr.instantiate();
add_child(_snd);

You didn’t include a definition for audioStr, but I suspect that is a reference to AudioStreamPlayer. If that is the case, you don’t have any safeguard against creating multiple instances. When you are hearing two sounds, it may be because multiple players are running at the same time.

You are also playing the same sound in multiple states. The code that prints “here 2” does not prevent the other conditional from being evaluated, and if that is seen as true then the elevEnd sound will be played.

thanks
problem was i had 2 elevators in the level…
And they were both coming from the same y position -96.0 when the player entered the level…

and i was checking if playerPosition.y < -20, elevState = 3;
So the 2 elevators were both moving down and playing the sound when they reached the position…

ive already fixed it with rect2; problem was in elevState 2…


func is_player_in_elevator_zone() -> bool:
	var rect = Rect2(-100, -200, 300, 400)
	return rect.has_point(to_local(Player.global_position))


	match elevState:
		
		0:
			offset.y = 0;
			if ( callBtnsActive == true ):
				elevState = 6;
			if ( playerIN == true && Input.is_action_just_pressed("key_up") ):
				elevState = 1;
				Snd.playSnd(Snd.elevEnd);
		1:
			position = position.move_toward( MoveTo , speed * delta );
			offset.y = fixFeetOffset;
			
		2: # spawnToMoveDown
			if is_player_in_elevator_zone():
				Player.position = position + Vector2( 70, 31 );
				elevState = 3;
			else:
				position = MoveToSpawn;
				elevState = 0;
		3:
			position = position.move_toward( MoveToSpawn , speed * delta );
			offset.y = -fixFeetOffset;
			if ( position == MoveToSpawn ):
				elevState = 0;
				Snd.playSnd(Snd.elevEnd);

As a total aside, equality checks on float (or things made of float, like Vector2) are unreliable. You’re better off doing something like:

   if(isZeroApprox(position.distance_to(MoveToSpawn))):

That is, check if the length of the distance between the two positions is within a really tiny distance from zero. It’s a good habit to get into; floating point uses sliding precision, and that can cause unexpected bugs.