Shortest posible example for saving one variable

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Godot_Starter

I already read this docs:

But I still dont get it, how I can save things. So can somebody give me a example, for the following situation: I have a variable:

var Level = 1

During the game the value of the variable changes. After the value changes I will call a function:

func Save():
       pass

And then I will close the game and open it again and than I want, that the variable has the same value as before, when I closed the game.

What do I have to write in the function and what do I have to do when the game starts?

PS: Sorry for bad Englisch

:bust_in_silhouette: Reply From: blockchan

Try this:

func save(level):
  var data = {
    "level": level
  }

  var save_file = File.new()
  save_file.open("user://savegame.save", File.WRITE)
  save_file.store_line(to_json(data))
  save_file.close()

can you tell me how you would get that to load because if you give level a value it will reset every time you run the code

Shub_r16 | 2021-05-25 22:26

:bust_in_silhouette: Reply From: njamster

As you don’t mention what exactly you didn’t get, I can just provide you with the most minimal implementation possible. Of course you still have to call save_level:

var level = 1

func _ready():
	load_level()

func load_level():
	print("Loading...")

	var save_file = File.new()
	if not save_file.file_exists("user://savefile.save"):
		print("Aborting, no savefile")
		return

	save_file.open("user://savefile.save", File.READ)
	level = save_file.get_line()
	save_file.close()

func save_level():
	print("Saving...")

	var save_file = File.new()
	save_file.open("user://savefile.save", File.WRITE)
	save_file.store_line(level)
	save_file.close()

It’s basically the same code as in the documentation, just less general.

Thats one big thing I dont understand: The first step in the docs was Serializing and the second step save and reading. But in the save and reading step the serializing step wasnt used any more. Whats the sense behind serializing? Or is Serializing just a second way of saving things? Because the first answer on this question looks like serializing, whatever this is.

Godot_Starter | 2020-05-12 12:04

Whats the sense behind serializing?

Serialization is the transformation of an object into a stream of bytes that can be used to reconstruct the object later on. It’s commonly seen in networking (when you want to transfer an object from one client to another) and - like here - to store data.

But in the save and reading step the serializing step wasnt used any more.

That’s not true, it is used:

# Call the node's save function
var node_data = node.call("save")

The save-method of a node determines which information about the node is saved and how it is labeled. If you only want to store one variable, you don’t need to this: you can simply write it’s value into a file and load the file content if the file exists. However, if you store multiple things in the same file, it’s a lot easier to organize the content in a hierarchy and label stuff for easier excess then “remembering” what the value in line 43 of your savefile is supposed to mean. The save-method does exactly that by grouping all save-worthy information about a certain node in a dictionary.

njamster | 2020-05-12 12:36

Ok thanks for this instruction. I copied your code from above, and I added two buttons and used them in code like this:

func _on_Button_pressed():
    level = level + 1


func _on_Save_pressed():
    save_level()

But when I pressed the first button with the

level = level + 1

I got an error:

Invalid operands ‘String’ and ‘int’ in operator ‘+’.

What did I wrong?

Godot_Starter | 2020-05-12 14:13

My bad, change this line:

level = save_file.get_line()

into this one:

level = int(save_file.get_line())

The get_line-method returns a string. Wrapping it inside the int-method is a so-called type-cast, converting the string into an integer. Adding one to a string isn’t defined (thus the warning), however adding one to an integer is.

njamster | 2020-05-12 18:35

Thanks for that, now it works great.
But I tried it with multiple variables and than I got a problem again. I made a new question for that:
Shortest posible example for saving multiple variables - Archive - Godot Forum

Godot_Starter | 2020-05-13 07:49