Also saving level resource with savegame.

main
Martin Felis 2024-08-15 23:21:31 +02:00
parent 4612e79e1f
commit 89b39ae9b1
3 changed files with 11 additions and 5 deletions

View File

@ -1,7 +1,8 @@
class_name SaveGame class_name SaveGame
extends Resource extends Resource
@export var player_transform:Transform3D @export var level:String
@export var player_name:String @export var player_name:String
@export var quest_states:Dictionary @export var quest_states:Dictionary
@export var player_transform:Transform3D
@export var inventory:Array[Item] @export var inventory:Array[Item]

View File

@ -15,6 +15,8 @@ var _root_ui_state:ROOT_UI_STATE = ROOT_UI_STATE.UNDEFINED
var _all_recipes:Array[Recipe] = [] var _all_recipes:Array[Recipe] = []
var _player:Player = null var _player:Player = null
var current_level_resource:String
# Called when the node enters the scene tree for the first time. # Called when the node enters the scene tree for the first time.
func _ready(): func _ready():
for file in DirAccess.get_files_at("res://data/recipes"): for file in DirAccess.get_files_at("res://data/recipes"):
@ -47,7 +49,9 @@ func load_scene(scene_resource:PackedScene):
child.queue_free() child.queue_free()
child.get_parent().remove_child(child) child.get_parent().remove_child(child)
print("Loading level '" + scene_resource.resource_path + "'")
scene.add_child(scene_resource.instantiate()) scene.add_child(scene_resource.instantiate())
current_level_resource = scene_resource.resource_path
_player = scene.find_child("Player", true, false) _player = scene.find_child("Player", true, false)

View File

@ -4,8 +4,6 @@ extends Node3D
@onready var message_container:Control = %MessagesContainer @onready var message_container:Control = %MessagesContainer
@onready var message_textedit:TextEdit = %MessageTextEdit @onready var message_textedit:TextEdit = %MessageTextEdit
@onready var builder_missing_tool = %BuilderMissingTool
@onready var root_ui:RootUI = %RootUI @onready var root_ui:RootUI = %RootUI
func save_game(): func save_game():
@ -17,6 +15,7 @@ func save_game():
var save_game:SaveGame = SaveGame.new() var save_game:SaveGame = SaveGame.new()
save_game.level = root_ui.current_level_resource
save_game.player_transform = player.global_transform save_game.player_transform = player.global_transform
for node:Node in get_tree().get_nodes_in_group("quest_state"): for node:Node in get_tree().get_nodes_in_group("quest_state"):
@ -33,14 +32,16 @@ func save_game():
root_ui.activate_ui_panel(%GameUI) root_ui.activate_ui_panel(%GameUI)
func load_game(): 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) var player:Player = find_child("Player", true, false)
if player == null: if player == null:
push_error("Cannot load game: no player found!") push_error("Cannot load game: no player found!")
return return
var save_game:SaveGame = load("user://savegame.tres") as SaveGame
player.global_transform = save_game.player_transform player.global_transform = save_game.player_transform
for node:Node in get_tree().get_nodes_in_group("quest_state"): for node:Node in get_tree().get_nodes_in_group("quest_state"):