Godot Version
Version 4.7.1
Question
Before we get started, a few important things to note:
- I do realize that this is already in another question quite similar to mine, but the way that that user set up his inventory system seemed fairly different from mine. I will link the post regardless. Transfer item between player inventory and chest inventory
- There are multiple YouTube tutorials that have to do with this topic, however, the one that i decided to watch did not include this topic, but I got too far into the tutorial now before realizing that. It was a good series regardless. I could redo the inventory from scratch, but whenever you remove very integrated systems in programming it’ll mess a bunch of stuff up, and I would rather try to make this system work before actually doing this. Here is the link to the tutorial: https://www.youtube.com/watch?v=X3J0fSodKgs
- I have not tried a ton of methods so far because I am still fairly new to Godot and have zero clue how to go about this, even though I do understand the inventory system.
So, here is my issue. I am making a game that mostly revolves around having to take stuff out of the fridge (basically, the player opens the fridge, grabs something, carries it outside, drops it off outside, and then repeats the process). I have a single slot player inventory resource set up and a twelve slot fridge inventory resource set up. I also have a cursor object that the player can use to select items that are in the fridge.
Here is the code for the general inventory resource:
extends Resource
class_name Inventory
@export var items: Array[InventoryItem]
Here is the code for the inventory item resources:
extends Resource
class_name InventoryItem
@export var name: String = ""
@export var texture: Texture2D
Here is the code for the fridge’s inventory:
extends Control
@onready var Inventory: Inventory = preload("res://inventory/fridge_inventory.tres")
@onready var slots: Array = $NinePatchRect/GridContainer.get_children()
@export var item: InventoryItem
func _ready():
_update_slots()
func _update_slots():
for i in range(min(Inventory.items.size(), slots.size())):
slots[i]._update(Inventory.items[i])
Here is the code for the fridge inventory slots:
extends Panel
@onready var item_to_display: Sprite2D = $CenterContainer/Panel/ItemDisplay
func _update(item: InventoryItem):
if !item:
item_to_display.visible = false
else:
item_to_display.visible = true
item_to_display.texture = item.texture
Here is the cursor code:
extends CharacterBody2D
var direction
@export var speed := 500
@export var start_x := 515
@export var start_y := 173
func _ready():
position = Vector2(start_x, start_y)
func _process(_delta: float):
pass
func _physics_process(_delta: float):
# Movement script
direction = Input.get_vector("left", "right", "up", "down")
velocity = direction * speed
move_and_slide()
# Select script
if Input.is_action_just_pressed("secondary action"):
print("added 'thing' to inventory")
Here is the player script:
extends CharacterBody2D
class_name Player
@export var speed := 340
@export var Inventory: Inventory
@onready var animated_sprite = $AnimatedSprite2D
var panicked = false
var direction
func _ready():
Global.on_trigger_player_spawn.connect(_on_spawn)
func _process(_delta):
pass
# movement script
func _physics_process(_delta):
direction = Input.get_axis("left", "right")
if direction:
velocity.x = direction * speed
animated_sprite.play("walk_not_panicked")
animated_sprite.flip_h = direction < 0
else:
velocity.x = move_toward(velocity.x, 0, speed)
animated_sprite.play("idle_not_panicked")
move_and_slide()
func _on_spawn(Position: Vector2, _direction: String):
global_position = Position
func _collect(item):
Inventory.insert(item)
If this would be too difficult to insert based on my current code, I have a fallback option that I think I would know how to code, but it may not be quite as nice of a system. Any help is greatly appriciated.