How to dynamically load resources from a folder in a release build

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

Hi there,

So I have a game that requires a lot of images, that I can then load at run time. I then go on to choose one of these images and load it. Rather than hardcode the list of image files, I have a function that browses the directory and looks for png files. My code to do so works perfectly while testing in the editor, but if I make a release build by exporting, my code fails to find any images.

Debugging shows that these resources are not included in the exported build (however, their .import files are.)
I have tried to fix this by making sure these images are covered by a glob pattern in the export filters, but this doesn’t change anything.

Now, if I do hardcoded the list of image in code, they all load fine when running a release build, which leads me to believe the code is scanned for references to resources to be included. So how can i make this work?

Here is the code to load the image files. Note the debugging print statement, the output of which is below:

	if dir.open("res://tattoos/") == OK:
        dir.list_dir_begin()
        var file_name = dir.get_next()
        while (file_name != ""):
                print("File name: ", file_name)  # Debugging release builds
                if file_name.ends_with("png"):
                        tattoos.push_back("res://tattoos/" + file_name)
                file_name = dir.get_next()
    return tattoos

If I run this from the editor, I get output like:

File name: anchor.png.import
File name: heart.png
File name: eye.png.import
File name: .
File name: new
File name: tear.png
File name: ..
etc....

But if I run this in an exported build, I get output like:

File name: new
File name: anchor.png.import
File name: book.png.import
File name: cross.png.import
File name: eye.png.import
etc....

My export settings are as follows:
Export settings

For reference, if I replace the above code with something like:

	var tattoos = [
            "res://tattoos/anchor.png",
            "res://tattoos/cross.png",
            "res://tattoos/heart.png",
            "res://tattoos/reeds.png",
            "res://tattoos/snake.png",
            "res://tattoos/book.png"
    ]

It works fine in both builds.

Thanks in advance!

:bust_in_silhouette: Reply From: jgodfrey

See my answer to a similar question here:

https://forum.godotengine.org/59637/cannot-traverse-asset-directory-in-android

Hopefully, that’s helpful…

Thanks @jgodfrey - that works like a charm!

tigerchops | 2020-08-10 19:33