Topic was automatically imported from the old Question2Answer platform.
Asked By
Frost Dragon Games
I am trying to create a file save system to save multiple files for a single save, so I want to put them all in a folder, but when I do this:
var tribeFile = FileAccess.open(“user://” + tribeName + “/tribe_data.save”, FileAccess.WRITE);
It get’s a “file not found error”. However this works:
var tribeFile = FileAccess.open(“user://” + tribeName + “tribe_data.save”, FileAccess.WRITE);
I get why it’s doing this, the folder tribeName doesn’t not exist. My question is how do I make it exist? I have been looking through the docs for a while and I can’t find anything. I thought I remember doing this with and old version of Godot.
The tribeName variable probably has a / at the end of it, so when you do the first one, “user://” + tribeName + “/tribe_data.save” would equal “user://name//tribe_data.save”, which is not a valid filepath.
but when you remove the slash in the second case it would equal “user://name/tribe_data.save”, which works.
To get rid of this weird behaviour and have it work every time, you can use String.path_join( ), which will automatically detect if tribeName has a slash on the end or not, and add one if neccessary:
var tribeFile = FileAccess.open("user://" + tribeName.path_join("tribe_data.save"), FileAccess.WRITE)
it does not. It gives the same error when I hard code the name in there.
var tribeFile = FileAccess.open(“user://tribeName/tribe_data.save”, FileAccess.WRITE);
So, the first is a file named tribe_data.save in a subfolder named tribeName and the second is just an (incorrectly) concatenated file named tribeNametribe_data.save located directly in the user:// folder.
As you guessed, you’ll need to create that non-existent subfolder. To do that, you want the DirAccess class. Specifically, the make_dir() or make_dir_absolute() methods.
Docs here:
Thank you, that’s what I was looking for. Maybe the docs on file saving need a link to this class.