Cannot Check FileAccess.file_exists on a string with variables included when game is exported

Godot Version

4.4.stable

Question

I have set up this system so that when the player reaches the end of the level, they can press space to continue to the next. It checks if the level file exists and just adds to it. The problem is, this seems to only work in editor. Whenever I export it, the line:

(if FileAccess.file_exists("res://Levels/Level " + str(Global.level) + “.tscn”):slight_smile:

and everything below it is completely ignored. Is there a better method to do what I’m trying to do or is this just not possible or dangerous or something?

func _process(_delta):
if finish:
if Input.is_action_pressed(“next_level”) and Global.level < Global.MAX_LEVEL:
Global.level += 1
if FileAccess.file_exists("res://Levels/Level " + str(Global.level) + “.tscn”):
get_tree().change_scene_to_file("res://Levels/Level " + str(Global.level) + “.tscn”)
else:
print(“ERROR! MAX LEVEL REACHED!”)

When you add a Resource (texture, sound, model,…) to your game godot will import it in a better suited format internally (inside the .godot/imported folder) and the original files won’t be added to the pck file when exporting the game as they aren’t needed.

The same happens with text Resourceslike *.tscn (scenes) or *.tres (other Resource types). They will be converted to binary files (they are originally human readable text files) when exporting the game unless you uncheck the editor/export/convert_text_resources_to_binary in the Project Settings (It’s enabled by default).

FileAccess has no knowledge about this so it won’t follow the *.remap/*.import files in the exported game. You can use ResourceLoader.exists() instead which will know and follow the remaps.

Change the FileAccess line to if ResourceLoader.exists("res://Levels/Level " + str(Global.level) + “.tscn”): and it should work.

1 Like