This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

Hello, I am trying to encrypt the save file for my game, and when doing so I am getting errors Unicode error: invalid skip, or Unicode error: invalid utf8, sometimes two or three of them. These errors do not come up when I didn't have any encryption. Here is my code and I hope someone has an idea. Thanks in advance :)

var file_path_old = "user://save.tres"
func _save():
    var data = ""
    var player = get_node("/root/Lvl" + str(int(get_tree().current_scene.name)) + "/Player")
    data += "maxhealth " + str(player.MAXHEALTH)
    data += "\n"
    data += "position " + str(player.position)
    var new_file = File.new()
    var err = new_file.open_encrypted_with_pass(file_path_old, File.WRITE, "123")
    if err == OK:
        new_file.store_line(data)
        new_file.close()
        setlvlsave()
        print("Saved Save File")
Godot version 3.4.3
in Engine by (60 points)

1 Answer

+1 vote
Best answer

Since your save path is "user://save.tres"(obviously it is a .tres file), your data should be a Resource rather than a String.
Take a look at this video, it's a tutorial about how to save as a .tres file.

Update following...
If you want to save as JSON file, for example like this

var file_path = "user://save.json"

func save_game():
    var player:Object # get_node("your_player_path")
    # note that save_data should be a dictionary type variable.
    var save_data = {
        "maxhealth": player.MAXHEALTH,
        "position": player.position
    }
    var file = File.new()
    file.open(file_path, File.WRITE)
    file.store_line(to_json(save_data))
    file.close()


func load_game():
    var file = File.new()
    if ! file.file_exists(file_path):
        print("No such file!")
        return
    file.open(file_path,file.READ)
    var text = file.get_as_text()
    var load_data = parse_json(text)
    print(load_data)
    file.close()

Then if we called save_game() and load_game(), we will get a print like this in Godot:
{maxhealth:100, position:(258, 141)}

New Update following...

In your case after call function load_game() we already have a load_data. We only need to put it back to the player.

I can give you a simple way to do this.

First of all, use Json to deal with vector2 is a problem, so I will prefer to store as an Array.
I change the code, now use Array to store the Vector2, and then you can easilly put the variable back on the player.

var file_path = "user://save.json"
onready  var player:Object # get_node("your_player_path")

func save_game():
    # note : save_data is a Dictionary.
    var save_data = {
        "maxhealth": player.MAXHEALTH,
        "position": [player.position.x, player.position.y]
    }

    var file = File.new()
    file.open_encrypted_with_pass(file_path, File.WRITE, "pass_code")
    file.store_line(to_json(save_data))
    file.close()

    print("Save game OK!")  # Just for test!

func load_game():
    var file = File.new()

    if ! file.file_exists(file_path):
        print("No such file!")
        return
    file.open_encrypted_with_pass(file_path, file.READ, "pass_code")
    var text = file.get_as_text()
    var load_data = parse_json(text)
    print(load_data)
    file.close()

    use_load_data(load_data)

func use_load_data(load_data: Dictionary):
    player.MAXHEALTH = load_data["maxhealth"]
    player.position.x = load_data["position"][0]
    player.position.y = load_data["position"][1]
by (526 points)
edited by

Thanks, do you know how I would go about saving this type of data then loading it through JSON? I have tried and the past and it has never worked for me.

ok, you can save as a .json file like the code I update in my answer.

Thanks for your help so far and this worked great for me :) The other thing I am not sure how to and I haven't found any tutorials for is how to take the dictionary and put the values of the dictionary back to the player. It would be a huge help if you could even set me in the right direction.

ok, new update in the answer.

Tysm this has been such a pain in my side up until now. I hope you have a good rest of your day/night.

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 Frequently asked questions and 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 [email protected] with your username.