Can't figure out saving/loading

Godot Version

4.5

Question

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.

You can find it here:

I have, yeah. It all checks out.

I’ve looked at the example there but it’s way too complicated and doesn’t seem to be for my use case

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?

I’m not great at GDscript, so…

I can print to the console, yes.

Would you mind providing this context in a readable manner? It makes it a lot easier to help you.

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

Try casting the data back into an enum when you load it from the file. Enums are just numbers with names (see GDScript basics | Enums).

Whatever the name for your enum is (glo?), try to cast the number into that.

“glo” is the global variable setup. Also, I don’t know how to cast into an enum

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:

Please provide the definition for glo.difficulty.

The problem is that the savefile and the actual variable don’t actually align. The goal is “3,” but what I get is “1”

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”.

“The entire script for the save/load logic (if the previous code snippet is not complete)”

That, uh… is, everything.

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

my JSON was just borked lmao. fixed it

nevermind, all deleting the JSON got me was a single-use save file.
I give up. I’m going to try something else. or just hire somebody. probably that.

real solution: all hail the .ini file

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.