Exported project is missing textures imported via gdscript

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

Big bad errors

I’m building a simple character creator.

The character is its own scene, with each customizable part being a sprite with a script attached. In the script I have a function which imports all files in a given path, and stores the files in an array - so for example the hair:

get_files("res://4_Hair/braids")

The directory contains a around 5-6 png files with different color variations of the hair. File 1 is array[0], file 2 is array[1], and so on.

I then have a button in the main scene which will increment the array index by one, and apply the texture. (it loops back to 0 if it goes too far).

This works well when I run the project from the editor, however if I run the exported exe, the files are not imported and the array will be empty, throwing the error in the screenshot. I’ve tried exporting all files in the project but I still have the same issue.

Any idea whats going on here?

:bust_in_silhouette: Reply From: exuin

Hi, the docs for Directory say:

Note: Many resources types are imported (e.g. textures or sound files), and their source asset will not be included in the exported game, as only the imported version is used. Use ResourceLoader to access imported resources.

This means that your files are no longer found in their original location. You must look for the import files instead.

Like this:

if not dir.current_is_dir() and file_name.ends_with(".import"):
    array.append(load(path + file_name.replace(".import, "")))

Ok, changing the file check to .import in my function worked a charm. thanks mate!
One thing that I’m not quite clear about though, what does the dir.current_is_dir() in your if statement do? Mine seems to work without it.

Working code for anyone interested:

# path is the system path in string format
func get_files(path):

   # files stores all files in the path, which I append to the outer array
	var files = [] # stores the files, I add an array 
	var dir = Directory.new()
	
	if dir.open(path) == OK:
		
		dir.list_dir_begin()
		var current_file = dir.get_next()
		
		while current_file != "":
			if current_file.ends_with(".import"):
				var new_file_object = load(path + "/" + current_file.replace(".import",""))
				files.append(new_file_object)

			current_file = dir.get_next()
			
	
	item_dic.append(files)

Spiderpiggie | 2021-09-13 19:33

Yeah, you don’t need it. It just checks if the current file in the directory is a folder or not.

exuin | 2021-09-13 19:36