Loading files from a directory: list_dir_begin() works in the editor but exporting to PC or Android breaks it.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By M. Alkotob
:warning: Old Version Published before Godot 3 was released.

The problematic function is shown below.

The problem strangely only happens after exporting, and when running from the editor everything is done perfectly and I can use the paths to randomly instance things.

I keep getting “Couldn’t start file stream!” message which means it can’t find the directory maybe? (Based on this from the docs)

The value for path is “res://episodes/3_end/”. I also tried “/episodes/3_end/” but then it breaks everywhere.

func list_files_in_directory(path):
var paths = []
var dir = Directory.new()
dir.open(path)
if(dir.list_dir_begin()):
	dmsg("\nCouldn't start file stream!\n")

while true:
	var file = dir.get_next()
	if file == "":
		break
	elif (not file.begins_with(".") and file.extension() == "tscn"):
		paths.append(file.basename()+"." + file.extension())
		
dir.list_dir_end()
return paths

Thank you all.

Everything in res:// doesn’t exist as files when you export a game, because they get bundled into the executable data, this is why it doesn’t find the directory.
But that’s just a guess, I used to use this to detect my levels in an early version of my game, I don’t use it anymore so maybe something got broken in between…

Zylann | 2017-05-13 14:42

Should work to load resources, but try exporting to zip and check the files in there (tscn are converted to “tscn.converted.scn”, by the way)

eons | 2017-05-13 17:23