- Edited
So I did an save system for my game but when I save something and then load it works but everythine I save the game it looks it creates and new save and on load it randomly choose the one of many times I saved the game. Can anyone help me?
So I did an save system for my game but when I save something and then load it works but everythine I save the game it looks it creates and new save and on load it randomly choose the one of many times I saved the game. Can anyone help me?
One problem is that you're loading the variables in a different order than you're storing them.
When using the store_*
and load_*
methods, the order has to be consistent.
DaveTheCoder Thank you verry much
DaveTheCoder Okay it worked but now its not saving
`func _ready():
load_game()
func _process(delta):
%Score.text = "Clicks: " + str(clicks)
save()`
Do you have something better to load and save automaticly.
_process() is called every few milliseconds. It would be better save the file less frequently, or only when the data that's being saved has changed.
When using FileAccess.open(), you should check the result to see if it was successful. If it failed, print the error code.
You should also call FileAccess.close().
var path: String = "..."
var file := FileAccess.open(path, FileAccess.WRITE)
var err: Error = OK if file != null else FileAccess.get_open_error()
if err != OK:
print_debug("Failed to open file '%s' for writing, error=%d" % [path, err])
func save():
var file = FileAccess.open(save_path, FileAccess.WRITE)
# --- VARIABLES TO STORE ---
file.store_var(cps)
file.store_var(clicks)
file.store_var(cps_price)
func load_game():
if FileAccess.file_exists(save_path):
var file = FileAccess.open(save_path, FileAccess.READ)
# --- VARIABLES TO LOAD ---
cps = file.get_var(cps)
clicks = file.get_var(clicks)
cps_price = file.get_var(cps_price)
else:
cps = 0
clicks = 0
cps_price = 0
This is my saving code could you fix it to save and load. Thanks
I would do it like this:
var cps: int
var clicks: int
var cps_price: int
func save(save_path: String) -> void:
var file := FileAccess.open(save_path, FileAccess.WRITE)
var err: Error = OK if file != null else FileAccess.get_open_error()
if err != OK:
print_debug("Failed to open file '%s' for writing, error=%d" % [save_path, err])
return
file.store_var(cps)
file.store_var(clicks)
file.store_var(cps_price)
file.close()
func load_game(save_path: String) -> void:
if FileAccess.file_exists(save_path):
var file := FileAccess.open(save_path, FileAccess.READ)
var err: Error = OK if file != null else FileAccess.get_open_error()
if err != OK:
print_debug("Failed to open file '%s' for reading, error=%d" % [save_path, err])
return
cps = file.get_var(cps)
clicks = file.get_var(clicks)
cps_price = file.get_var(cps_price)
file.close()
else:
cps = 0
clicks = 0
cps_price = 0
I'm guessing that the three variables are integers. If they're not, change their declarations.
This doesn't address the problem of saving too frequently in _process(). I can't tell you how to fix that without knowing more about your project.
DaveTheCoder you should run a timer, like save every 5s or something.. no point in saving every frame, and its impossible, especially on windows. it gets fucky real fast.
kuligs2 you
You're replying to the wrong person.
DaveTheCoder Thank you so much .