Hey guys! I’m working on a Jukebox system where people can add their songs into the game so they can listen to their favorite tracks dynamically. I’ve run into the problem where when I try to load a track it returns <Object#null>.
Any ways I could make this work? Here’s the code (It’s an autoload).
extends AudioStreamPlayer
#Music being loaded on the titlescreen
const level_music = preload("res://Music/TheBouncerIsGone.ogg")
var currentLevelMusic
var songsInPlaylist
func _play_music(music: AudioStream, volume = 0.0):
if stream == music:
return
stream = music
play()
#Music being loaded in the gameplay
func _get_songs():
songsInPlaylist = DirAccess.get_files_at("user://Jukebox")
print(songsInPlaylist)
#Playing the title screen song
func play_music_level():
_play_music(level_music)
#Playing the songs during gameplay
func play_music_gameplay():
var transformPackedIntoArray = Array(songsInPlaylist)
currentLevelMusic = "user://Jukebox/" + str(transformPackedIntoArray.pick_random())
print(load(currentLevelMusic))
_play_music(load(currentLevelMusic))
func _on_finished():
play_music_gameplay()
And when i need to instantiate something i put a preload reference to it in this script and instantiate on that reference
So to make a jukebox system i suggest having an array of preloaded audio files
const SONGS = [
preload("res://Assets/Songs/song1.mp3"), # Replace these with actual references
preload("res://Assets/Songs/song2.ogg"),
]
You could store this directly in the jukebox script, im not sure what your system is like right now.
And then to play the songs you would do something like this:
@onready var JukeboxAudioPlayer : AudioStreamPlayer = $[path_here]
...
var song = SONGS.pick_random()
JukeboxAudioPlayer.stream = song
JukeboxAudioPlayer.play()
However since your game is multiplayer, you can’t use a MultiplayerSynchronizer node to sync a .stream of an AudioStreamPlayer node
So the best thing to do would be an rpc function that calls the .play() method on all clients/players
In my project i have a function like this:
@rpc("any_peer", "call_local")
func play_sound(sound : NodePath):
var sound_node = get_node_or_null(sound)
if sound_node: sound_node.play()
And do this to call it:
GlobalRPC.play_sound.rpc(footstep_sound.get_path()) # GlobalRPC is an autoloaded script where i hold all my rpc functions
Oh well lmao, might help someone else in the future. So your real issue was putting files in a folder other than res://, but yeah the res folder is where you want to keep all your files and such, as that is the whole point of it
I just realised why you wanted to put your files in the user:// folder, to let players put their own audio files, oops i may have misinformed you quite a bit, sorry