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
74
resources/save_game/save_manager.gd
Normal file
74
resources/save_game/save_manager.gd
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
extends Node
|
||||
|
||||
# The group of nodes to save is named "save_nodes"
|
||||
|
||||
# Reference to the currently loaded save game
|
||||
@onready var current_save : SavedGame = SavedGame.new()
|
||||
|
||||
var world
|
||||
|
||||
func save_game():
|
||||
# Create a new empty save game
|
||||
var saved_game := SavedGame.new()
|
||||
|
||||
# Save the environment data
|
||||
saved_game.world = "res://worlds/debug_level.tscn" #SceneManager.get_world()
|
||||
saved_game.daytime = 12.5
|
||||
saved_game.weather = "this should be an enum"
|
||||
|
||||
# Save the party data
|
||||
for monster in SaveManager.current_save.party:
|
||||
saved_game.party.append(monster)
|
||||
|
||||
# Create an array to store the data of all other nodes
|
||||
var saved_data : Array[SavedData] = []
|
||||
|
||||
# Use a group call to collect all the data from the other objects
|
||||
get_tree().call_group("save_nodes", "on_save_game", saved_data)
|
||||
|
||||
# Store the array in the saved game
|
||||
saved_game.saved_data = saved_data
|
||||
|
||||
# Store it into a file
|
||||
ResourceSaver.save(saved_game, "user://savegame.tres")
|
||||
|
||||
|
||||
func load_game():
|
||||
# Load the saved game file
|
||||
var saved_game : SavedGame = load("user://savegame.tres") as SavedGame
|
||||
|
||||
# Return if no save file is found
|
||||
if saved_game == null:
|
||||
print("No savegame found.")
|
||||
return
|
||||
|
||||
# Save a reference to the current save game
|
||||
current_save = saved_game
|
||||
print(saved_game.party[0].display_name)
|
||||
|
||||
# Clean up all non-static objects from the scene
|
||||
get_tree().call_group("save_nodes", "on_before_load_game")
|
||||
|
||||
# Restore world data
|
||||
# doing it via get_tree, until the SceneManager exists
|
||||
var _world : Node = load(saved_game.world).instantiate()
|
||||
get_node("/root/Game/CurrentLevel").add_child(_world)
|
||||
world = _world
|
||||
#SceneManager.set_world(saved_game.world)
|
||||
|
||||
# Restore all other nodes
|
||||
for node in saved_game.saved_data:
|
||||
# Instantiate the scene for the node
|
||||
var scene := load(node.scene_path) as PackedScene
|
||||
var restored_node = scene.instantiate()
|
||||
|
||||
# Add the item to the world
|
||||
# todo: add parent as value to save
|
||||
world.add_child(restored_node)
|
||||
|
||||
# run a callback to restore the item's state
|
||||
if restored_node.has_method("on_load_game"):
|
||||
restored_node.on_load_game(node)
|
||||
|
||||
func save_exists():
|
||||
return FileAccess.file_exists("user://savegame.tres")
|
||||
Loading…
Add table
Add a link
Reference in a new issue