I'm trying to go through every .wav file in my game's audio folder and turn that into an AudioStreamPlayer
.
So I tried to do this with the help of the sample code in the documentation for the Directory
class. The documentation also warns you that the source asset of most resources won't be included in the exported version of the game and to "use ResourceLoader
to access imported resources," and I assumed that meant I could just do ResourceLoader.load()
instead of load()
so here's the code:
var directory = Directory.new()
if directory.open("res://assets/audio/") != OK:
return
directory.list_dir_begin(true)
var file = directory.get_next()
while file != "":
if file.get_extension() == "wav":
var sound = AudioStreamPlayer.new()
add_child(sound)
sound.name = file.get_slice(".", 0)
sound.stream = ResourceLoader.load(directory.get_current_dir() + file)
file = directory.get_next()
This works fine when testing in editor except, like the documentation warned, the audio doesn't get loaded in on an exported version of the game. And I can't find anything in the documentation that tells me how to load the "imported" versions of the resources. So... how can I get the imported versions of resources rather than their "source assets?" The only thing I found on Google just appears to be a bandage solution.