From c240b0d74cdf1dd1ca5c5f431844d3cb48c5d903 Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Thu, 15 Aug 2024 23:43:05 +0200 Subject: [PATCH] Saving of pickup items works. --- model/save_game.gd | 1 + objects/pickup_item.gd | 3 +++ objects/pickup_item.tscn | 5 ++++- root_ui.gd | 3 +++ scenes/game.tscn | 2 -- scenes/main.gd | 16 ++++++++++++++++ 6 files changed, 27 insertions(+), 3 deletions(-) diff --git a/model/save_game.gd b/model/save_game.gd index 3f8fbab..47450b3 100644 --- a/model/save_game.gd +++ b/model/save_game.gd @@ -6,3 +6,4 @@ extends Resource @export var quest_states:Dictionary @export var player_transform:Transform3D @export var inventory:Array[Item] +@export var level_pickup_items:Array[NodePath] diff --git a/objects/pickup_item.gd b/objects/pickup_item.gd index 420201f..8dae8e3 100644 --- a/objects/pickup_item.gd +++ b/objects/pickup_item.gd @@ -4,6 +4,8 @@ extends Node3D var editor_child_scene:Node3D = null +signal item_picked_up(node_path:NodePath) + @export var item:Item: get: return item @@ -30,4 +32,5 @@ func _ready(): func _on_area_3d_body_entered(body): if body.has_method("on_item_picked_up"): body.on_item_picked_up(item) + emit_signal("item_picked_up", get_path()) queue_free() diff --git a/objects/pickup_item.tscn b/objects/pickup_item.tscn index 8edc84b..b866538 100644 --- a/objects/pickup_item.tscn +++ b/objects/pickup_item.tscn @@ -1,7 +1,8 @@ -[gd_scene load_steps=4 format=3 uid="uid://da5r82nvypfk4"] +[gd_scene load_steps=5 format=3 uid="uid://da5r82nvypfk4"] [ext_resource type="Script" path="res://objects/pickup_item.gd" id="1_1i8k2"] [ext_resource type="Resource" uid="uid://dmjr6pmb17l2y" path="res://data/items/woodplanks.tres" id="2_dph0g"] +[ext_resource type="PackedScene" uid="uid://ysfnsrvgemi6" path="res://assets/resources/wood_planks.tscn" id="3_fvgmq"] [sub_resource type="CylinderShape3D" id="CylinderShape3D_1ndog"] height = 0.2 @@ -12,6 +13,8 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.00393829, 0) script = ExtResource("1_1i8k2") item = ExtResource("2_dph0g") +[node name="Planks" type="Node3D" parent="." instance=ExtResource("3_fvgmq")] + [node name="Area3D" type="Area3D" parent="."] [node name="CollisionShape3D" type="CollisionShape3D" parent="Area3D"] diff --git a/root_ui.gd b/root_ui.gd index 91ce83d..103b977 100644 --- a/root_ui.gd +++ b/root_ui.gd @@ -53,6 +53,9 @@ func load_scene(scene_resource:PackedScene): scene.add_child(scene_resource.instantiate()) current_level_resource = scene_resource.resource_path + for pickup_item in get_tree().get_nodes_in_group("pickup_item"): + pickup_item.connect("item_picked_up", get_parent().on_item_pickup) + _player = scene.find_child("Player", true, false) if _player: diff --git a/scenes/game.tscn b/scenes/game.tscn index 83966de..79eadf2 100644 --- a/scenes/game.tscn +++ b/scenes/game.tscn @@ -124,5 +124,3 @@ script = ExtResource("14_g3xjj") [node name="ScaredToWalkInTheDark" type="Node" parent="Quests" groups=["quest_state"]] script = ExtResource("18_7v3bk") - -[connection signal="visibility_changed" from="." to="." method="_on_visibility_changed"] diff --git a/scenes/main.gd b/scenes/main.gd index a1112e5..ab02760 100644 --- a/scenes/main.gd +++ b/scenes/main.gd @@ -6,6 +6,8 @@ extends Node3D @onready var root_ui:RootUI = %RootUI +var _picked_up_level_items:Array[NodePath] = [] + func save_game(): var player:Player = find_child("Player", true, false) @@ -16,6 +18,7 @@ func save_game(): var save_game:SaveGame = SaveGame.new() save_game.level = root_ui.current_level_resource + save_game.level_pickup_items = _picked_up_level_items save_game.player_transform = player.global_transform for node:Node in get_tree().get_nodes_in_group("quest_state"): @@ -32,6 +35,8 @@ func save_game(): root_ui.activate_ui_panel(%GameUI) func load_game(): + _picked_up_level_items.clear() + var save_game:SaveGame = load("user://savegame.tres") as SaveGame var level_resource:PackedScene = load(save_game.level) root_ui.load_scene(level_resource) @@ -44,14 +49,25 @@ func load_game(): player.global_transform = save_game.player_transform + # Quest state for node:Node in get_tree().get_nodes_in_group("quest_state"): for property in node.get_property_list(): if (property.usage & PROPERTY_USAGE_STORAGE != 0) and (property.usage & PROPERTY_USAGE_SCRIPT_VARIABLE) != 0: node.set(property.name, save_game.quest_states[node.name][property.name]) + # Inventory player.inventory.clear() for item in save_game.inventory: player.inventory.add_item(item) + # Picked up items + for item_path in save_game.level_pickup_items: + if get_tree().root.has_node(item_path): + var item_node = get_tree().root.get_node(item_path) + _picked_up_level_items.append(item_path) + item_node.queue_free() + root_ui.activate_ui_panel(%GameUI) +func on_item_pickup(node_path:NodePath): + _picked_up_level_items.append(node_path)