AudioStreamPlayer2D stop playing sound after moving it

Godot Version

Godot Engine v4.5.1.stable.steam.f62fdbde1

Question

I had a firing sound and played it directly in the Player node code.
Now I added a weapon_component to Player node, which uses a weapon object that plays the code there and I don’t hear any sound anymore.

class_name Weapon extends Node2D

@export var weapon_name : String = “Test Weapon”;

@onready var audio_stream_player_2d: AudioStreamPlayer2D = $AudioStreamPlayer2D

func _init():
audio_stream_player_2d = AudioStreamPlayer2D.new()
print(“Weapon: Constructor”);
pass;

func fire(origin : Vector2, aim : Vector2):
audio_stream_player_2d.play();
print(“Weapon: fire!”);

I know that both _init() and fire() are called successfully since the message show up in output.

What is the issue here?

The sound is attenuated with distance to the listener. By default, it should be the center of the screen, so if your node is located too far, you won’t hear anything, probably.

Unless you have a specific reason to use AudioStreamPlayer2D, maybe a simple AudioStreamPlayer would be a better solution.

1 Like

thank you!

The issue is, the distance doesn’t seem not be the problem.
I reworked the code and tried several other approaches, added an AudioListener2D, used make_current() it does not work.
It seems the issue is, that the AudioStreamPlayer2D in children (of player) is not really played at all or needs some special setting etc.

Here is the basic structure of my project:

  • main (top node)

    • tactical map
      • player
      • enemy
    • ui
  • player

    • weaponcomponent
    • camera2d
  • weaponcomponent

    • AudioStreamPlayer2D
    • AnimatedSprite2D
      (the weapon sprite is clearly visible on the player when the game runs, so it is not positioning issue)

In weaponcomponent I assign the firesound and call play

@onready var audio_listener_2d: AudioListener2D = $AudioListener2D
@export var fireSound : AudioStream; #has a mp3 set properly in the Inspector

func _init():
audio_stream_player_2d = AudioStreamPlayer2D.new()
audio_stream_player_2d.set_stream(fireSound);

func fire():
audio_stream_player_2d.play();

but no sound, I also tried using AudioStreamPlayer in weaponcomponent, but it doesn’t seem to work. Yet, when I use it in player, it works.

You are creating a new AudioStreamPlayer2D node, that never gets added to your scene tree. According to the scene you described above, you already have an AudioStreamPlayer2D as child of “weaponcomponent”, so why don’t you use that one? (Or was that meant to be the AudioListener2D?)

1 Like

sorry, I forgot to add:

@onready var audio_stream_player_2d: AudioStreamPlayer2D = $AudioStreamPlayer2D

But this means the order of events is:

  • in _init() you create a new AudioStreamPlayer2D and set its stream to fireSound
  • @onready you assign the child AudioStreamPlayer2D to audio_stream_player_2d (losing the reference to the newly created player)
  • then you call play() on the child node, which never got its stream set

You shouldn’t create a new AudioStreamPlayer2D if you already have one. Set the audio stream in _ready() instead of _init(), so you can reference the child node.

1 Like

thanks, I think I found the underlying issue thanks to your help.

I had an object in the player class for WeaponCompoment, but it wasn’t attached to the scene. Actually worse, I had one attached, but created a new one in the code. I am still not really used to the whole scene etc. stuff.

I am coming from a world without all the scene trees etc. which really confuses the hell out of me :slight_smile: