Images loaded by code not loading in exported build

Godot 4.2.1

I’m using ResourceLoader.load() to assign a texture to a Sprite2D via code, and this works fine in the editor, but when I export the project into a .exe and run it, those images won’t show up.

This is my code:
var img = “res://images/” + dialog[phraseNum][“Name”] + dialog[phraseNum][“Emotion”] + “.png”
if FileAccess.file_exists(img):
$Portrait.texture = ResourceLoader.load(img)
else: $Portrait.texture = null

I guess it’s not finding the proper path for some reason.
I’ve read other posts of people with similar problems but none of the solutions given have worked in my case.

I found out that if I remove the FileAccess.file_exists(img) condition, the problem will be fixed but I would like to keep it nevertheless. Is there any alternative to it or should I just make sure I don’t try to load any resource I shouldn’t?

Use ResourceLoader.exists in place of FileAccess.file_exists for checking the existence of a file or resource in the “res://” virtual directory.


On export, Godot will convert resource files to binary like “.tscn” → “.scn”, image files to a texture format like “.png” → “.ctex” and store them in a special directory “.godot” or packed structure “.pck”.

ResourceLoader is aware of these conversions and remaps original paths to their converted path.


This fixed my problem, thank you so much for the explanation!

In general, it’s a bad idea to check if a file exists before opening it. Why? Because in the short time between checking if the file exists and opening the file, the file could be moved, modified, or deleted. In other words, if you rely on a check before opening the file, then your code contains a race condition (which is a serious bug). Much better to just attempt to open the file and handle the failure when the file fails to open.