expressobits / inventory-system

Modular inventory system for godot 4 with nodes, compatible with multiplayer, separate logic from the UI, Using items as separate resources.
MIT License
500 stars 32 forks source link

Adding items doesn't work at demo (F1, F2 etc) #137

Closed TheYellowArchitect closed 2 months ago

TheYellowArchitect commented 2 months ago

The class FPSDemo:

class_name FPSDemo
extends Node

@export var item_wood : Item
@export var item_stone : Item
@export var item_grass : Item
@export var database : InventoryDatabase

@onready var main_inventory : Inventory

func _ready():
    #main_inventory = InventorySystem.get_inventory_handler().get_inventory(0)
    $"UI/Inventory System UI".setup($"Player/CharacterInventorySystem")

#
#func _process(_delta):
    #if Input.is_action_just_released("add_item_a"):
        #InventorySystem.get_inventory_handler().add_to_inventory(main_inventory, item_wood, 1)
    #if Input.is_action_just_released("remove_item_a"):
        #main_inventory.remove(item_wood, 1)
#
    #if Input.is_action_just_released("add_item_b"):
        #InventorySystem.get_inventory_handler().add_to_inventory(main_inventory, item_stone, 1)
    #if Input.is_action_just_released("remove_item_b"):
        #main_inventory.remove(item_stone, 1)
#
    #if Input.is_action_just_released("add_item_c"):
        #InventorySystem.get_inventory_handler().add_to_inventory(main_inventory, item_grass, 1)
    #if Input.is_action_just_released("remove_item_c"):
        #main_inventory.remove(item_grass, 1)

It should be this:

class_name FPSDemo
extends Node

@export var item_wood : Item
@export var item_stone : Item
@export var item_grass : Item
@export var database : InventoryDatabase

@onready var main_inventory : Inventory = get_node("Player").get_node("CharacterInventorySystem").get_node("InventoryHandler").get_node("Inventory")

func _ready():
    $"UI/Inventory System UI".setup($"Player/CharacterInventorySystem")

#
func _process(_delta):
    if Input.is_action_just_released("add_item_a"):
        main_inventory.add(item_wood, 1)
    if Input.is_action_just_released("remove_item_a"):
        main_inventory.remove(item_wood, 1)

    if Input.is_action_just_released("add_item_b"):
        main_inventory.add(item_stone, 1)
    if Input.is_action_just_released("remove_item_b"):
        main_inventory.remove(item_stone, 1)
#
    if Input.is_action_just_released("add_item_c"):
        main_inventory.add(item_grass, 1)
    if Input.is_action_just_released("remove_item_c"):
        main_inventory.remove(item_grass, 1)

I hate the deep hardcoded reference for main_inventory but I think it's worth it for the demo, since otherwise the left UI messages don't work (thats horrible for a demo!)