rewired signals and added savemanager and resource saving
This commit is contained in:
parent
0f5045dc22
commit
f694d5c4f8
13 changed files with 138 additions and 87 deletions
|
|
@ -1,70 +0,0 @@
|
|||
extends Node
|
||||
# save_game.gd - Responsible for saving and loading the game using a SaveData resource
|
||||
|
||||
const SAVE_GAME_PATH := "user://savegame.save"
|
||||
const VERSION: int = 1
|
||||
|
||||
func save() -> void:
|
||||
var save_file = FileAccess.open(SAVE_GAME_PATH, FileAccess.WRITE)
|
||||
|
||||
# Save the meta data like save game version and timestamps
|
||||
var meta_data = {
|
||||
"meta": {
|
||||
"version" : VERSION,
|
||||
"timestamp" : Time.get_datetime_string_from_system()
|
||||
}
|
||||
}
|
||||
var meta_json_string = JSON.stringify(meta_data)
|
||||
save_file.store_line(meta_json_string)
|
||||
|
||||
# Save the player node's position
|
||||
var player = get_tree().get_root().find_child("Player", true, false)
|
||||
var player_node_data = player.save()
|
||||
var player_json = JSON.stringify(player_node_data)
|
||||
save_file.store_line(player_json)
|
||||
|
||||
# Save all other stuff from the SaveData
|
||||
var save_data = SaveData.call("save")
|
||||
var json_string = JSON.stringify(save_data)
|
||||
save_file.store_line(json_string)
|
||||
|
||||
func load() -> void:
|
||||
if not FileAccess.file_exists(SAVE_GAME_PATH):
|
||||
print("No save file exists.")
|
||||
return
|
||||
|
||||
# NEXT STEP: How to load correctly and in which order?
|
||||
# first the current world, then the player and their position and then the inventory?
|
||||
|
||||
# Load the save file line by line and process the dictionary to restore
|
||||
# the object it represents
|
||||
var save_file = FileAccess.open(SAVE_GAME_PATH, FileAccess.READ)
|
||||
while save_file.get_position() < save_file.get_length():
|
||||
var json_string = save_file.get_line()
|
||||
|
||||
# Creates the helper class to interact with JSON
|
||||
var json = JSON.new()
|
||||
|
||||
# Check if there is any error while parsing the JSON string, skip in case of failure
|
||||
var parse_result = json.parse(json_string)
|
||||
if not parse_result == OK:
|
||||
print("JSON Parse Error: ", json.get_error_message(), " in ", json_string, " at line", json.get_error_line())
|
||||
continue
|
||||
|
||||
# Get the data from the JSON object
|
||||
var node_data = json.get_data()
|
||||
|
||||
if node_data.has("meta"):
|
||||
print("Meta data ignored.")
|
||||
|
||||
# handle the player data (name, money, etc.)
|
||||
if node_data.has("player_data"):
|
||||
var world_to_load = load(node_data["player_data"].world)
|
||||
var instance = world_to_load.instantiate()
|
||||
instance.add_child(load("res://scenes/player/player.tscn").instantiate())
|
||||
var level_container = get_tree().get_root().find_child("CurrentLevel", true, false)
|
||||
level_container.add_child(instance)
|
||||
|
||||
|
||||
func savegame_exists() -> bool:
|
||||
return FileAccess.file_exists(SAVE_GAME_PATH)
|
||||
Loading…
Add table
Add a link
Reference in a new issue