From 89b39ae9b1b7efbd19345898a2f25081e4a821e9 Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Thu, 15 Aug 2024 23:21:31 +0200 Subject: [PATCH] Also saving level resource with savegame. --- model/save_game.gd | 3 ++- root_ui.gd | 4 ++++ scenes/main.gd | 9 +++++---- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/model/save_game.gd b/model/save_game.gd index 3cd32c0..3f8fbab 100644 --- a/model/save_game.gd +++ b/model/save_game.gd @@ -1,7 +1,8 @@ class_name SaveGame extends Resource -@export var player_transform:Transform3D +@export var level:String @export var player_name:String @export var quest_states:Dictionary +@export var player_transform:Transform3D @export var inventory:Array[Item] diff --git a/root_ui.gd b/root_ui.gd index d1a30d0..91ce83d 100644 --- a/root_ui.gd +++ b/root_ui.gd @@ -15,6 +15,8 @@ var _root_ui_state:ROOT_UI_STATE = ROOT_UI_STATE.UNDEFINED var _all_recipes:Array[Recipe] = [] var _player:Player = null +var current_level_resource:String + # Called when the node enters the scene tree for the first time. func _ready(): for file in DirAccess.get_files_at("res://data/recipes"): @@ -47,7 +49,9 @@ func load_scene(scene_resource:PackedScene): child.queue_free() child.get_parent().remove_child(child) + print("Loading level '" + scene_resource.resource_path + "'") scene.add_child(scene_resource.instantiate()) + current_level_resource = scene_resource.resource_path _player = scene.find_child("Player", true, false) diff --git a/scenes/main.gd b/scenes/main.gd index 106213f..a1112e5 100644 --- a/scenes/main.gd +++ b/scenes/main.gd @@ -4,8 +4,6 @@ extends Node3D @onready var message_container:Control = %MessagesContainer @onready var message_textedit:TextEdit = %MessageTextEdit -@onready var builder_missing_tool = %BuilderMissingTool - @onready var root_ui:RootUI = %RootUI func save_game(): @@ -17,6 +15,7 @@ func save_game(): var save_game:SaveGame = SaveGame.new() + save_game.level = root_ui.current_level_resource save_game.player_transform = player.global_transform for node:Node in get_tree().get_nodes_in_group("quest_state"): @@ -33,13 +32,15 @@ func save_game(): root_ui.activate_ui_panel(%GameUI) func load_game(): + var save_game:SaveGame = load("user://savegame.tres") as SaveGame + var level_resource:PackedScene = load(save_game.level) + root_ui.load_scene(level_resource) + var player:Player = find_child("Player", true, false) if player == null: push_error("Cannot load game: no player found!") return - - var save_game:SaveGame = load("user://savegame.tres") as SaveGame player.global_transform = save_game.player_transform