How do I create a save file with dynamic folders.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: 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 =“user://” + tribeName + “/”, FileAccess.WRITE);

It get’s a “file not found error”. However this works:

var tribeFile =“user://” + tribeName + “”, 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.

:bust_in_silhouette: Reply From: evpevdev

The tribeName variable probably has a / at the end of it, so when you do the first one, “user://” + tribeName + “/” would equal “user://name//”, which is not a valid filepath.
but when you remove the slash in the second case it would equal “user://name/”, 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 ="user://" + tribeName.path_join(""), FileAccess.WRITE)

it does not. It gives the same error when I hard code the name in there.

var tribeFile =“user://tribeName/”, FileAccess.WRITE);

gives same error.

Frost Dragon Games | 2023-04-20 02:20

:bust_in_silhouette: Reply From: jgodfrey

I assume this:

"user://" + tribeName + "/"

Results in:


And, this:

"user://" + tribeName + "",

Results in:


So, the first is a file named in a subfolder named tribeName and the second is just an (incorrectly) concatenated file named 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.

Frost Dragon Games | 2023-04-20 03:21