How to create save in folder

Godot Version

4.3

So I want to have a feature where saves files are in a folder in the user://

however I get this error

Attempt to call function ‘store_var’ in base ‘null instance’ on a null instance.

my code:

extends Node

var save_path = "user://saves/variable.save"
var clicksVar = 0

func save():
    var file = FileAccess.open(save_path, FileAccess.WRITE)
    file.store_var(1)
    

func load_data():
    if FileAccess.file_exists(save_path):
        var file = FileAccess.open(save_path, FileAccess.READ)
        clicksVar = file.get_var(clicksVar)
    else:
        print("No data found...")
        clicksVar = 0
        
    

func clear_data():
    if FileAccess.file_exists(save_path):
        DirAccess.remove_absolute(save_path)
        get_tree().quit()
    else:
        print("No Save")

Does the folder exist? It won’t create the parent path, so user://saves/ must exist

would I have to create the folder in code or do I just go to the directory and make a new folder.

You do so in code, using DirAccess.make_dir_absolute("user://saves") for example

i get this error. where exactly would I use that method

image_2024-11-02_104112707

In the method you create the save, i.e. in save, you can’t call methods where you call them

that seemed to do the trick, thanks!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.