Godot Version
All versions
Question
This works in the engine, but not after exporting.
When I thought “hmm, that’s odd why?” I remember reading something some months ago that came to mind. If I take the liberty to paraphrase that, it was something like: “After export, the game doesn’t actually use the res://path/to/file structure”.
Why doesn’t this work and what is the proper way to keep track of where everything is? I want to just be able to add a new sprite to a folder and it will automatically be known in a variable, without having to manually hardcode it every time…
Here is what tried to do:
var resource_sprites = get_filenames_of_type("assets/sprites/resources", "png")
# Returns a dictionary with file name as key
func get_filenames_of_type(folder, suffix = null):
if not folder.begins_with("res://"):
folder = "res://" + folder
if not folder.ends_with("/"):
folder += "/"
var dir = DirAccess.open(folder)
var file_names = {}
if dir:
dir.list_dir_begin()
var file_name = dir.get_next()
while file_name != "":
if not dir.current_is_dir():
var append_file = true
if suffix and not file_name.ends_with(suffix):
append_file = false
if append_file:
var file_name_parts = file_name.split(".")
file_names[file_name_parts[0]] = {
"full_path": folder + file_name_parts[0] + "." + file_name_parts[1],
"folder": folder,
"suffix": file_name_parts[1]
}
file_name = dir.get_next()
dir.list_dir_end()
return file_names
Example output:
print(resource_sprites)
=> {
"Silver": {
"full_path": "res://assets/sprites/resources/Silver.png",
"folder": "res://assets/sprites/resources/",
"suffix": "png"
},
"Gold": {
"full_path": "res://assets/sprites/resources/Gold.png",
"folder": "res://assets/sprites/resources/",
"suffix": "png"
}
}