Audio crashes and missing on exported project (3.2.3)

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

I have a project which works fine when running from the editor, but exporting to release on Linux results problems. Whenever a sound effect would play, the game will crash. I thought there may be missing files, I exported with all issues, and the problem remains.

Note the game works perfectly fine running from the editor, what could be the issue?

What does the code for playing the sound effect look like?

exuin | 2021-02-27 16:26

Essentially, I have a singleton which loads all the sound effects at runtime, and assigns them to a constant in a dictionary to be accessed, so like:

###Audio singleton
enum {EXAMPLE}
Sounds = {EXAMPLE : "pathtofile"}

_ready():
    Sounds[EXAMPLE] = load(Sounds[EXAMPLE])

func play(sound=EXAMPLE):
    player = AudioStreamPlayer.new()
    ###set bus and stream
    stream = Sounds[sound]
    player.play()

This is very simplified. In reality I have a directory with multiple sound effects, so each constant corresponds to an array containing like hit1, hit2 and hit3. When the function is called, one will play.

When I run from terminal I get invalid index 0 on array errors. To me that says the files aren’t being found or aren’t loading into the array for whatever reason. The thing that confuses me is that it works perfectly fine in the editor, so it’s something to do with how it’s exporting.

psear | 2021-02-27 16:47

Are the paths constant strings or are you using a Directory to find them? Because imported files are not at their original location upon export, they’re in the import folder.

exuin | 2021-02-27 17:24

Ah, I think that might be the source of the issue. The paths are strings to directories which contain the files. Basically when I want to add a bunch of player gets hurt sounds to the game, I make a PLAYER_HURT constant, put it in a dictionary, and assign it to an array of the contents of res://assets/sounds/player/hurt/, for example.

This allows me to call Audio.play(Audio.PLAYER_HURT) from elsewhere, without it always being the same sound, so slightly variable sounds or multiple sound effects for the same event. The singleton randomly picks one from the array.

How do I work around this issue? By just loading every file by hand in the script? Is there a less cumbersome way of accomplishing what I want to do?

psear | 2021-02-27 18:18

:bust_in_silhouette: Reply From: exuin

When exporting the project, the imported files can no longer be found in their original locations and are instead stored in the .import folder. However, the .import files are still in their original locations. To use Directory to look through a folder in order to find files, you need to look for the .import files instead. Then, remove the .import from the file name, and load the resulting path (the ResourceLoader will automatically account for the moved file).

Great! Does this also apply to things like .tscn files? Is there a list of things that should be obtained from a .import file rather than the file itself if it’s taken from a directory?

psear | 2021-02-27 21:52

It doesn’t apply to tscn files, just anything that has an import file appear next to it and has an imported version of it in the .import folder.

exuin | 2021-02-27 22:08