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