#writing to files

11 messages · Page 1 of 1 (latest)

burnt harness
#

i wanna have a save file in json
heres my script:

extends Node

var defaultSF = {
    "achivedEndings": {
        "windhoos": false,
        "tutorialHell": false
    },
    "name": null,
    "learnedBy": {
        "tutorialsWached": 0,
        "forumsRead": 0,
        "askedFriend": 0
    }
}

var SF = null

# Called when the node enters the scene tree for the first time.
func _ready():
    print(SF)
    createSaveFile()
    print(SF)
    loadSaveFile()
    print(SF)
    resetSaveFile()
    print(SF)

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
    pass

func createSaveFile():
    DirAccess.open("user://").make_dir("assets")
    DirAccess.make_dir_absolute("user://assets/saveFiles")
    FileAccess.open("user://assets/saveFiles/sf1.json", FileAccess.WRITE)
    return

func loadSaveFile():
    SF = FileAccess.open("user://assets/saveFiles/sf1.json", FileAccess.WRITE)
    return

func resetSaveFile():
    if (SF == null):
        loadSaveFile()
    
    print(JSON.stringify(defaultSF, "\t"))
    SF.store_string(str(JSON.stringify(defaultSF, "\t")))
    SF.close()
    loadSaveFile()
    return

it creates the file but for some odd reason it does not write anything in it.

wind mist
#

It looks like you just opened the save file for writing after setting it up in createSaveFile(), but you're not actually writing anytjing to it

#

Wait check your indentarion for the SF.store_string around the bottom. It looks a bit off to me

burnt harness
#

wdym "Wait check your indentarion for the SF.store_string around the bottom"?

wind mist
#

This here does not look lined up indentation-wise in the code text you provided

tidal shadow
#

it creates the file but for some odd reason it does not write anything in it.

How are you determining this - from looking at the file on disk? Or are you expecting that print(SF) reads the file?

burnt harness
#

looking at the file.

#

sorry for not responding for so long bdw.

burnt harness
#

how would i change it tho?

#

i mean that error is not on y my screen

#

ya saw my message or not?