0 votes

When I try to save a file that has a folder in its path...

    var file = File.new()
    file.open("user://data/save_game.txt", File.WRITE)

...I get an error:

ERROR: File must be opened before use.
onPrintError @ index.js:400
putchar @ index.js:9
write @ index.js:9
write @ index.js:9
doWritev @ index.js:9
write @ index.js:9
$func24319 @ 03499712:0x6d99d3
$func7980 @ 03499712:0x21305c
$func8162 @ 03499712:0x21fb4b
$func25950 @ 03499712:0x72f85b
$func7235 @ 03499712:0x1df568
$func25954 @ 03499712:0x72f9d2
$func25948 @ 03499712:0x72f764
$func637 @ 03499712:0x28393
$func25073 @ 03499712:0x712a15
$func13712 @ 03499712:0x3e02c7
$func7727 @ 03499712:0x2021b8
$func7758 @ 03499712:0x203dbd
$func2740 @ 03499712:0xade46
$func11815 @ 03499712:0x35f6d8
$func23092 @ 03499712:0x6bbce0
$func1341 @ 03499712:0x57cd2
$func976 @ 03499712:0x3cab1
$func20041 @ 03499712:0x576719
$func20041 @ 03499712:0x5766c1
$func5660 @ 03499712:0x16f085
$func40766 @ 03499712:0xa95d64
$gk @ 03499712:0x4c67ac
$hk @ 03499712:0x6cbb4b
Module.main @ index.js:9
callMain @ index.js:9
(anonymous) @ index.js:736
(anonymous) @ index.js:731
Promise.then (async)
(anonymous) @ index.js:730
Promise.then (async)
start @ index.js:709
(anonymous) @ index.js:772
Promise.then (async)
startGame @ index.js:771
(anonymous) @ (index):289
setTimeout (async)
(anonymous) @ (index):286
setTimeout (async)
(anonymous) @ (index):283
Promise.then (async)
(anonymous) @ (index):280
(anonymous) @ (index):318
Show 3 more frames
00:32:55.144 index.js:400 at: store
string (core/bind/core_bind.cpp:2182) - Condition "!f" is true.

I also previously tried creating the folder...

    var dir = Directory.new()

... the error is still the same.

What did I forget to do?

Godot version 3.5
in Engine by (14 points)

My mistake. Issue resolved. It was just necessary to create the directory like this before writing the file:

var dir = Directory.new()

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.