I am trying to set up my game to play a specific song during a match, and then switch to different songs at the end depending on which character won (those being just placeholders here for now). I have this code in a script that I plan to call methods from in the script for the main game loop as needed:
extends AudioStreamPlayer
class_name Sounds
enum CURRENT_SONG {MATCH, CHAR1, CHAR2}
@onready var matchMusic: AudioStreamPlayer = $MatchMusic
func playMusic(song := CURRENT_SONG.MATCH) -> void:
match song:
CURRENT_SONG.MATCH:
matchMusic.play()
CURRENT_SONG.CHAR1:
pass
CURRENT_SONG.CHAR2:
pass
I have an AudioStreamPlayer node in the scene tree named "MatchMusic" with a placeholder song attached.
I watched this tutorial, where something similar to what I'm doing above worked, and changed things around to attach sound playback to certain steps in the gameplay loop instead of just clicking a button. Instead, when I attempt to run my game, I get the error "Cannot call method 'play' on a null value." I tested commenting out the matchMusic.play() method, replacing it with a print statement, and running it then, to make sure it's not just a problem with how I'm trying to call my playMusic() function somehow- that part seems to be working fine.
Is your node path correct? The message means that the matchMusic variable resolved to null. It's possible there is a typo in the name, or the node is located elsewhere. $MatchMusic is equivalent to the get_node("MatchMusic") call, so it looks for the node with that name in the children of the node which has your script attached to it.