Unicode Error When Encrypting Save

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

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 :slight_smile:

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")
:bust_in_silhouette: Reply From: ponponyaya

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]

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.

DaZel77 | 2022-03-12 17:09

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

ponponyaya | 2022-03-12 21:41

Thanks for your help so far and this worked great for me :slight_smile: 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.

DaZel77 | 2022-03-13 01:12

ok, new update in the answer.

ponponyaya | 2022-03-13 02:44

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

DaZel77 | 2022-03-13 03:47