I found a tutorial on saving/loading that seemed to fit my needs. Unfortunately, it just doesn’t work. ‘glo.difficulty’ doesn’t get set once the file is loaded.
var savelocation = "user://SaveFile.json"
var saves : Dictionary = {
"difficulty": glo.difficulty
}
func _ready() -> void: #Only saves when returning to title. I have accounted for this.
if glo.firstopen == 0: #to prevent the save file from being overwritten instantly.
glo.firstopen = 1
else:
savefile()
loadfile()
func savefile():
var save : FileAccess = FileAccess.open(savelocation, FileAccess.WRITE)
save.store_var(saves.duplicate())
save.close()
func loadfile():
if FileAccess.file_exists(savelocation):
var save : FileAccess = FileAccess.open(savelocation, FileAccess.READ)
var data : Dictionary = save.get_var()
var saver = data.duplicate()
saves = saver
save.close()
Have you tried verifying the data you are saving and loading? Try and check what is inside save, both in savefile() and loadfile().
I haven’t personally used store_var() before – only store_line(). In the documentation you can find a json serialization example that saves and loads files. That is what I’ve used in the past. The code example is a little long but it should do the trick.
Could it be a type-casting error when you convert the variant from save.get_var() into a Dictionary? Have you checked that data is a valid Dictionary; can you print it to the console?
It successfully saves and reads the difficulty. The issue appears to be in reversing the data.
“difficulty” is 3, but glo.difficulty isn’t “difficulty,” even if literally and directly set as such
You mean it’s an Autoload? I assume this means you defined difficulty like this:
const difficulty = 3
If this is the case, it makes perfect sense why your savefile contains 3. That’s literally what you’re inserting into the file. What’s the confusion here; and what’s your desired result? I suddenly don’t understand what your problem is.
Maybe you can explain this in a more clear manner:
Kindly provide more context about your program so we can move forward. There is no way I can debug your problem remotely without having access to, at least, the code that you’re a referring to. Provide the following:
The difficulty definition
The entire script for the save/load logic (if the previous code snippet is not complete)
…any other relevant information.
Also, is it possible that you have previously saved a value of 1 to the file and then never overwritten it? I’m not sure how your _ready() code works in terms of preventing an “instant overwritten save file”.
func example():
var y=3
var x=y
y=4
print(x)
print(y)
3
4
Assigning an integer value copies the value, it doesn’t link the variables together. Some variable types, like arrays, get passed by reference instead of value by default.
You can read more about this here:
I might have misunderstood, but this seems like the thing that might be the issue.
I don’t really like your attitude. I’m trying to get you to be cooperative so we can solve the problem, but you insist on giving me one-sentence replies.
Go read the documentation (such as the link I initially provided). It could help you if you are willing to put in the effort.
I was on that track, but I tried to just directly set it backwards
glo.difficulty = data.get(“difficulty”)
and it just… didn’t work. Even though “difficulty” was the variable I wanted
The documentation specifically tells you not to use ini files as save files. I recommend reading the docs that explain saving and loading a game, it was already linked in here.
I also recommend giving more context because “it didn’t work” won’t tell us much.