|
|
|
|
Reply From: |
Jayman2000 |
I got your code to work. Here’s the problems that I noticed and how I fixed them:
Problem 1
When you call directory.make_dir(), it returns an Error. I added an if statement to tell me about the error:
const DIR_ERROR = 'ERROR: Failed to create directory "%s". Error code %s.'
...
func _save():
var directory = Directory.new()
var DIR_PATH = "user://save/" + UserInput.playername + "/"
if directory.dir_exists(DIR_PATH):
print("Directory exists!")
else:
var error_code = directory.make_dir(DIR_PATH)
if error_code != OK:
printerr(DIR_ERROR % [DIR_PATH, error_code])
Specifically, the Error was 20, ERR_CANT_CREATE. The Error was being caused by the fact that the “save” directory didn’t exist, so the UserInput.playername directory couldn’t be created inside of it. Here’s two possible solutions to problem 1:
const DIR_ERROR = 'ERROR: Failed to create directory "%s". Error code %s.'
# This is only needed for potential solution 1.
func try_to_create_dir(directory, path):
if not directory.dir_exists(path):
var error_code = directory.make_dir(path)
if error_code != OK:
printerr(DIR_ERROR % [path, error_code])
func _save():
var directory = Directory.new()
# Potential solution 1:
try_to_create_dir(directory, "user://save")
try_to_create_dir(directory, "user://save/" + UserInput.playername)
# Potential solution 2:
var DIR_PATH = "user://save/" + UserInput.playername + "/"
if directory.dir_exists(DIR_PATH):
print("Directory exists!")
else:
var error_code = directory.make_dir_recursive(DIR_PATH)
if error_code != OK:
printerr(DIR_ERROR % [DIR_PATH, error_code])
Problem 2
When you call new_file.open(), it returns an Error. I added an if statement to tell me about the error:
const OPEN_ERROR = 'ERROR: Failed to open "%s". Error code %s.'
...
func _save():
...
var error_code = new_file.open(FILE_NAME, File.READ_WRITE)
if error_code == OK:
new_file.seek_end()
#Store the data and close the file
new_file.store_line(data)
new_file.close()
print("file saved!")
else:
printerr(OPEN_ERROR % [FILE_NAME, error_code])
Specifically, the Error was 7, ERR_FILE_NOT_FOUND. Here’s what Godot’s documentation says about opening a file for writing:
Opens the file for write operations. The file is created if it does not exist, and truncated if it does.
Here’s what it says about opening a file for reading and writing:
Opens the file for read and write operations. Does not truncate the file. The cursor is positioned at the beginning of the file.
Since you’re opening the file for reading and writing, the file’s not getting created when it doesn’t exist. Here’s my solution for that problem:
const OPEN_ERROR = 'ERROR: Failed to open "%s". Error code %s.'
...
func _save():
var error_code
if new_file.file_exists(FILE_NAME):
error_code = new_file.open(FILE_NAME, File.READ_WRITE)
else:
error_code = new_file.open(FILE_NAME, File.WRITE)
if error_code == OK:
new_file.seek_end()
#Store the data and close the file
new_file.store_line(data)
new_file.close()
print("file saved!")
else:
printerr(OPEN_ERROR % [FILE_NAME, error_code])
Conclusion
In the end, here’s what my code loked like:
const OPEN_ERROR = 'ERROR: Failed to open "%s". Error code %s.'
const DIR_ERROR = 'ERROR: Failed to create directory "%s". Error code %s.'
func _save():
var directory = Directory.new()
# Potential solution 1:
try_to_create_dir(directory, "user://save")
try_to_create_dir(directory, "user://save/" + UserInput.playername)
# Potential solution 2:
var DIR_PATH = "user://save/" + UserInput.playername + "/"
if directory.dir_exists(DIR_PATH):
print("Directory exists!")
else:
var error_code = directory.make_dir_recursive(DIR_PATH)
if error_code != OK:
printerr(DIR_ERROR % [DIR_PATH, error_code])
var FILE_NAME = DIR_PATH + str(month) + "_" + str(day) + "_" + str(year) + ".txt"
var data = ""
data += "Time: " + str(hour) + ":" + str(minute)
data += "Level: " + str(level)
data += "Target_Objects_Points: " + str(targetObjectsPoints)
data += "\n"
data += "Distractor_Objects_Points: " + str(distractorObjectsPoints)
data += "\n"
data += "Missed_Objects: " + str(missedObjectsPoints)
data += "\n"
var new_file = File.new()
var error_code
if new_file.file_exists(FILE_NAME):
error_code = new_file.open(FILE_NAME, File.READ_WRITE)
else:
error_code = new_file.open(FILE_NAME, File.WRITE)
if error_code == OK:
new_file.seek_end()
#Store the data and close the file
new_file.store_line(data)
new_file.close()
print("file saved!")
else:
printerr(OPEN_ERROR % [FILE_NAME, error_code])
wow. This worked! I went with potential solution 2, as it made more sense to me. I just want to say thank you SO much and this is absolutely fabulous. Your generosity is truly appreciated
miss_bisque | 2021-02-26 02:35