peter-kish / gloot

A universal inventory system for the Godot game engine.
MIT License
561 stars 20 forks source link

[Request] Add SlotType properties #223

Closed oOScuByOo closed 1 month ago

oOScuByOo commented 1 month ago

Hello there,

Thanks for your work Peter, it was awesome.

I'm currently looking for a way to add Item's type based slot, like any rpg, if item have the properties for exemple ItemType:String = Helmet, can only be equipped on item slot with property SlotType:String = Helmet.

Thanks for all =)

peter-kish commented 1 month ago

Hi,

You should be able to do that by overriding the can_hold_item method of the ItemSlot class and checking some item property in it. E.g.:

class_name MyItemSlot
extends ItemSlot

@export var slot_type: String

func can_hold_item(item: InventoryItem) -> bool:
    return item.get_property("item_type") == slot_type
LeeWannacott commented 1 month ago

This seems to work, however I get the following errors in the console. image

class_name ItemSlotExtended
extends ItemSlot

@export var slot_type: Array[String]

func can_hold_item(item: InventoryItem) -> bool:
    return slot_type.any(check_allowable_item_types.bind(item))

func check_allowable_item_types(allowable_type:String,item:InventoryItem)->bool:
    return item.get_property("item_type").contains(allowable_type)
peter-kish commented 1 month ago

The errors seem to happen if you inherit from ItemSlot but don't declare your script as a @tool script. Most of the gloot scripts are declared as @tool because they are also run in the editor. This make it possible to equip items in a slot from the editor, add items to inventories etc.

Therefore the example I listed above should be corrected to:

@tool
class_name MyItemSlot
extends ItemSlot

@export var slot_type: String

func can_hold_item(item: InventoryItem) -> bool:
    return item.get_property("item_type") == slot_type