Image and Audio Files not Loading on Export

@gertkeno I did not want to hijack this thread as it’s not mine and already solved. However I am interested in what you said.

Here’s my code:

func get_random_voice_line(index: int) -> AudioStream:
	var audio_path: String = "res://assets/sound/dialogue/"

	var file_path: String = audio_path + avatars[index] + "/character_selection"
	var dir: DirAccess = DirAccess.open(file_path)
	if not dir:
		return
	
	var filenames: Array[String]
	var file_list: PackedStringArray = dir.get_files()
	for file_name: String in file_list:
		if file_name.ends_with(".import"):
			filenames.append(file_name.trim_suffix(".import"))
	
	var sound_file: AudioStream = load(file_path + "/" + filenames.pick_random())
	return sound_file

Changed from this:

Summary
func get_random_voice_line(index: int) -> AudioStream:
	var audio_path: String = "res://assets/sound/dialogue/"

	var file_path: String = audio_path + avatars[index] + "/character_selection"
	var dir: DirAccess = DirAccess.open(file_path)
	if not dir:
		return
	
	var filenames: Array[String]
	var file_list: PackedStringArray = dir.get_files()
	for file_name: String in file_list:
		if file_name.ends_with(".wav"):
			filenames.append(file_name)
	
	var sound_file: AudioStream = load(file_path + "/" + filenames.pick_random())
	return sound_file

But the ResourceLoader doesn’t use DirAccess. So would you suggest using ResourceLoader to traverse the directory and then DirAccess to get the files? Or convert the whole thing to use ResourceLoader?

I’d propose these changes to make use .list_directory()

func get_random_voice_line(index: int) -> AudioStream:
	var audio_path: String = "res://assets/sound/dialogue/"

	var file_path: String = audio_path + avatars[index] + "/character_selection"
-	var dir: DirAccess = DirAccess.open(file_path)
-	if not dir:
+	if not DirAccess.dir_exists_absolute(file_path):
		return
	
	var filenames: Array[String]
-	var file_list: PackedStringArray = dir.get_files()
+	var file_list: PackedStringArray = ResourceLoader.list_directory(file_path)
	for file_name: String in file_list:
		if file_name.ends_with(".wav"):
			filenames.append(file_name)
	
	var sound_file: AudioStream = load(file_path + "/" + filenames.pick_random())
	return sound_file

ResourceLoader.list_directory doesn’t use DirAccess, it’s only intended to be used with “res://” paths so traversal should be simple and export-time known as is in your code.

1 Like

That works and is cleaner. Thank you @gertkeno!

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.