0 votes

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():

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

in Engine by (330 points)
edited by

2 Answers

+2 votes
Best answer

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():

func load_level():

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

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

func save_level():

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

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

by (10,265 points)
selected by

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.

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.

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():

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?

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.

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:

0 votes

Try this:

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

  var save_file = File.new()
  save_file.open("user://savegame.save", File.WRITE)
by (98 points)

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.