Loading an OGG or WAV file from res://sfx/ in gdscript

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By dodgyville

Hi,

I have a bunch of sound files in res://sfx/ and in my code I use Directory to get a list of the files, basically:

var ext = ".ogg"
dir.open(path)
dir.list_dir_begin()
while true:
    var file = dir.get_next()
    if file.ends_with(ext):
        files.append(file)
return files 

The I use sfx = load(audio_file)

It works when running from the editor but when the game is packaged up in a PCK file, the directory no longer contains OGG or WAV files. It still has ogg.import and wave.import files. Not sure what’s going on.

Any suggestions appreciated!

Under Project > Export > Resources try adding *.ogg, *.wav to the first text box, should be labelled “Filters to export non-resource files”.

literalcitrus | 2018-02-14 22:22

Ah, thank you very much!

I guess my other question would be: Am I handling sound files the right way? If they’re being imported into the engine as a resource, is there some way I should be calling them other than loading the ogg or wav file direct?

dodgyville | 2018-02-14 22:26

:bust_in_silhouette: Reply From: joshsegall

This also happened to me when exporting to iOS and cost me several hours of frustrating debugging. For anyone struggling with “missing” files in a folder or directory when exporting to a PCK file on iOS, here’s how I solved the problem.

**TL;DR: look for the .ext.import extension and then remove the extension to load the imported .ext file.

const SOUND_DIR = 'res://sound' 
var samples = {}

var dir = Directory.new()
var err = dir.open(SOUND_DIR)
if err == OK:
	dir.list_dir_begin()
	var filename = dir.get_next()
	while filename != '':
		if filename.ends_with('.wav.import'):
			filename = filename.left(len(filename) - len('.import'))
			var asp = AudioStreamPlayer.new()
			asp.set_stream(load(SOUND_DIR + '/' + filename))
			add_child(asp)
			var arr = filename.split('/')
			var name = arr[arr.size()-1].split('.')[0]
			samples[name] = asp
		filename = dir.get_next()

Note that the SOUND_DIR does not have a trailing slash. Godot is forgiving of that when running in the editor but the iOS builds are not.

So here’s what’s happening as background: When you add a file to the res folder it imports it into the .import directory, creates a filename.import file to point to it, and leaves the original file. So if you add mysound.wav to the sound folder and import it you’ll get something like:

sound/mysound.wav
sound/mysound.wav.import
.import/mysound.wav-1234567890abcdef1234567890abcdef.md5    
.import/mysound.wav-1234567890abcdef1234567890abcdef.sample

The imported sample and .import pointer file are exported to your PCK file. The original .wav file is not. However, calling load(‘res:://sound/mysound.wav’) will find the .import file and load the imported sample just fine. Overall this makes sense but you can confuse yourself by looking only for .wav extensions in the directory when looking at the original unexported directory.

If you try to export the original .wav or .ogg files as well it might work but you will end up with duplicate files in the build, which is probably not what you want.

Anyway, I hope this saves someone some debugging time in the future.

1 Like