Hello everyone,
I have completely finished the save and load functionality for my game, but then I encountered a very strange error. The game doesn’t have too much to save and load; it’s just the general option settings and key bindings. After everything was set up, I noticed a huge discrepancy in the key bindings. If I select keys like “A-B-C----X-Y-Z” from the keyboard, they are saved and loaded correctly. However, if I choose keys like 1-2-3—9 or on the numpad 1-2----9, they are saved correctly, but the data becomes corrupted during loading. For example, the physical code for key 1 is 49, but during loading, it gets loaded as 491. Similarly, the code for the numpad 8 key is 4194446, but it gets assigned as 41944468, and the numpad 4 key’s code is 4194442, which gets saved as 41944424. Essentially, each key is getting its own numeric value appended to the end. How can I fix this?
Here is my code for saving and loading the game.
extends Node
const SETTINGS_SAVE_PATH : String = "user://SettingsData.save"
var settings_data_dict : Dictionary = {}
func _ready():
SettingsSignalBus.set_settings_dicitionary.connect(on_settings_save)
load_settings_data()
func on_settings_save(data : Dictionary) -> void:
var save_settings_data_file = FileAccess.open(SETTINGS_SAVE_PATH, FileAccess.WRITE)
var json_data_string = JSON.stringify(data)
save_settings_data_file.store_line(json_data_string)
func load_settings_data() -> void:
if not FileAccess.file_exists(SETTINGS_SAVE_PATH):
return
var save_settings_data_file = FileAccess.open(SETTINGS_SAVE_PATH, FileAccess.READ)
var loaded_data : Dictionary = {}
while save_settings_data_file.get_position() < save_settings_data_file.get_length():
var json_string = save_settings_data_file.get_line()
var json = JSON.new()
var _parsed_result = json.parse(json_string)
loaded_data = json.get_data()
SettingsSignalBus.emit_load_settings_data(loaded_data)
loaded_data = {}
Also if you dont need JSON for interoperability with other systems, I would highly recommend you switch to a custom resource which has built-in serialization and deserialization
Also please clarify what is your data structure (one line = one JSON blob ?)
thank for your input. I cant say that I am expert on save and load. But I can say my json data is working just fine. It only happen on spesific keybinds. Which is wierd beacuse I store alot of other data also other than keybinds. But anyway I think I will switch all of my saving process to tres.
Just wondering if the issue is in your load_settings_data() function where you read back and parse the data line by line (I’m not quite sure what you’re trying to achieve there to be honest).
I have similar code in my game and I do the loading like this:
var f = FileAccess.open("user://config.json", FileAccess.READ)
if f:
var json = JSON.new()
var err = json.parse(f.get_as_text())
if err == OK:
var dict = json.get_data()
thanks. I just tried your way but your input bro but it did no difference. I have forged this save and load mechanic throuh lost of different guide and cant say even I understand why every part is where it is. But the magic is, it was working until this wierd bug. I think I will convert all of it in to resources and spend some time on it. I want to be able save and load many thing and gamer should be able to make saved files of game in my next game. Probably I will start working on solid .tres stracture tomorrow after work.
I would not dream of telling you what to do here, but if something weird is happening and you don’t quite understand it, you learn the most by trying to figure out what exactly is going on. So in this case, set a breakpoint and single step through each line of code, making sure all variables are what you expect and the flow is what you expect. If you have a scenario that you can reproduce and that fails, that is what I would do.