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
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.
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
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?)
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.
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