gloot icon indicating copy to clipboard operation
gloot copied to clipboard

After clear() cant interact with inventoryGrid

Open juliusspeak opened this issue 2 years ago • 21 comments

juliusspeak avatar Feb 02 '24 21:02 juliusspeak

When I fill the inventory completely and then clear it using sort(), then after that I cannot interact with the inventory. Nothing can be moved there and nothing can be added in code so that it is visible in the grid.

juliusspeak avatar Feb 02 '24 21:02 juliusspeak

I can interact with inventory only after this: var protoset = craftGrid.item_protoset var size = craftGrid.size craftGrid.reset() craftGrid.item_protoset = protoset craftGrid.size = size

juliusspeak avatar Feb 02 '24 22:02 juliusspeak

clear it using sort()

This is kinda weird, sort() should only reorder the items and should not clear the inventory: sort

Can you share some code, or even better, a simple project that demonstrates the issue?

peter-kish avatar Feb 03 '24 08:02 peter-kish

clear it using sort()

This is kinda weird, sort() should only reorder the items and should not clear the inventory: sort sort

Can you share some code, or even better, a simple project that demonstrates the issue?

I'm sorry. I meant clear()

juliusspeak avatar Feb 03 '24 12:02 juliusspeak

Hmm.. I tried it again with clear(), but I'm still unable to reproduce the issue: clear

peter-kish avatar Feb 03 '24 13:02 peter-kish

Can you show the code please?

juliusspeak avatar Feb 03 '24 13:02 juliusspeak

Sure, i just modified one of the examples:

extends Control

@onready var ctrl_inventory_left: CtrlInventoryGrid = $VBoxContainer/HBoxContainer/VBoxContainer/PanelContainer/CtrlInventoryGridLeft
@onready var ctrl_inventory_right: CtrlInventoryGrid = $VBoxContainer/HBoxContainer/VBoxContainer2/PanelContainer2/CtrlInventoryGridRight
@onready var btn_add_left: Button = $VBoxContainer/HBoxContainer/VBoxContainer/BtnAddLeft
@onready var btn_clear_left: Button = $VBoxContainer/HBoxContainer/VBoxContainer/BtnClearLeft
@onready var btn_add_right: Button = $VBoxContainer/HBoxContainer/VBoxContainer2/BtnAddRight
@onready var btn_clear_right: Button = $VBoxContainer/HBoxContainer/VBoxContainer2/BtnClearRight


func _ready() -> void:
    btn_add_left.pressed.connect(_on_btn_add.bind(ctrl_inventory_left))
    btn_clear_left.pressed.connect(_on_btn_clear.bind(ctrl_inventory_left))
    btn_add_right.pressed.connect(_on_btn_add.bind(ctrl_inventory_right))
    btn_clear_right.pressed.connect(_on_btn_clear.bind(ctrl_inventory_right))


func _on_btn_clear(ctrl_inventory: CtrlInventoryGrid) -> void:
    ctrl_inventory.inventory.clear()


func _on_btn_add(ctrl_inventory: CtrlInventoryGrid) -> void:
    ctrl_inventory.inventory.create_and_add_item("item_2x2")

peter-kish avatar Feb 03 '24 14:02 peter-kish

example

Actually I have two problems: 1 - no way to interact with inventory after .clear() 2. Some inventory cells cannot be filled from another inventory. You must first move an item from the same inventory to this cell.

juliusspeak avatar Feb 03 '24 22:02 juliusspeak

Weird... I haven't seen similar issues before. Any chance you could put together a small example that has the same problems, so I can look deeper into it?

peter-kish avatar Feb 04 '24 08:02 peter-kish

extends Control

@onready var brewButton = $brewMenuPanel/brewButton
@onready var craftGrid = $"../../inventoryWorks/craftInventoryGrid"
@onready var inventory = $"../../inventoryWorks/InventoryGrid"
@onready var riskBar = $brewMenuPanel/riskVarLabel
@onready var succesBar = $brewMenuPanel/succesVarLabel
var riskRate = 0
var succesRate = 0
var riskResult = 0
var succesResult = 0

const info_offset: Vector2 = Vector2(20, 0)
@onready var itemInfoPanel = $brewMenuPanel/itemInfoPanel
@onready var itemNameLabel = $brewMenuPanel/itemInfoPanel/itemNameLabel
@onready var itemDescriptionLabel = $brewMenuPanel/itemInfoPanel/itemDescriptionLabel
@onready var ctrlInventory: CtrlInventoryGridEx = $brewMenuPanel/CtrlInventoryGridEx


func _ready():
	randomize()
	brewButton.disabled = true
	
	ctrlInventory.item_mouse_entered.connect(_on_item_mouse_entered)
	ctrlInventory.item_mouse_exited.connect(_on_item_mouse_exited)

func brewBeer():
	setRiskSucces()
	var beerType = ""
	var newPrice = 0
	var priceMod = succesResult-riskResult
	
	if craftGrid.has_item_by_id("dogFoodNormal") or craftGrid.has_item_by_id("dogFoodMedium") or craftGrid.has_item_by_id("dogFoodBest"):
		if priceMod >= 500:
			beerType = "bestBeer"
		if priceMod < 500 and priceMod > 250:
			beerType = "goodBeer"
		if priceMod <= 250:
			beerType = "defaultBeer"
	else:
		beerType = "badBeer"

	#var protoset = craftGrid.item_protoset
	#var gridSize = craftGrid.size
	#craftGrid.reset()
	#craftGrid.item_protoset = protoset
	#craftGrid.size = gridSize
	
	craftGrid.clear()
	
	var beer = craftGrid.create_and_add_item(beerType)
	newPrice = beer.get_property("sellPrice") + priceMod
	beer.set_property("sellPrice", newPrice)
	var newName = beer.get_property("name") + " | Цена: " + str(newPrice)
	beer.set_property("name", newName)

func checkCraftGrid():
	if isPossibleBrew():
		brewButton.disabled = false
	else:
		brewButton.disabled = true
	
	
	setRates()
	setBarsToVars()

func isPossibleBrew():
	var acces = false
	if craftGrid.has_item_by_id("wheat") and craftGrid.has_item_by_id("hop") and craftGrid.has_item_by_id("yeast"):
		acces = true
	return acces

func setBarsToVars():
	riskBar.text = str(riskRate)
	succesBar.text = str(succesRate)

func setRates():
	riskRate = 0
	succesRate = 0
	var craftArray = craftGrid.get_items()
	for i in craftArray.size():
		if craftArray[i].get_property("riskRate"):
			riskRate += craftArray[i].get_property("riskRate")
			succesRate += craftArray[i].get_property("succesRate")

func setRiskSucces():
	riskResult = 0
	succesResult = 0
	var rng = RandomNumberGenerator.new()
	rng.randomize()
	var num = 0

	var craftArray = craftGrid.get_items()
	for i in craftArray.size():
		num = rng.randi_range(0, 1)
		if craftArray[i].get_property("riskRate"):
			if num:
				riskResult += craftArray[i].get_property("riskRate")
			else:
				succesResult += craftArray[i].get_property("succesRate")



func _on_item_mouse_entered(item: InventoryItem) -> void:
	itemInfoPanel.show()
	itemNameLabel.text = item.get_property("name")
	itemDescriptionLabel.text = item.get_property("description")


func _on_item_mouse_exited(_item: InventoryItem) -> void:
	itemInfoPanel.hide()
	


func _input(event: InputEvent) -> void:
	if !(event is InputEventMouseMotion):
		return

	itemInfoPanel.set_global_position(get_global_mouse_position() + info_offset)

juliusspeak avatar Feb 04 '24 15:02 juliusspeak

For reasons unknown to me, clear() now works as expected

upd And now the mistake has happened again

juliusspeak avatar Feb 04 '24 15:02 juliusspeak

Hey, sorry for the late reply. I don't see anything wrong with the code you posted, though of course, I wasn't able to debug it.

One more thing you can try is to place a breakpoint in addons/gloot/ui/ctrl_drop_zone.gd - line 22 after the inventory is cleared. It should trigger when an item is dropped onto the empty inventory. If it doesn't, I'm afraid I'll have to dig deeper.

peter-kish avatar Feb 08 '24 08:02 peter-kish

@peter-kish Might be related to this; I can take the item out of ItemSlot and into InventoryGrid but not put it back. InventoryGrid and ItemSlot both contain same item with same id.. etc. I can interact with InventoryGrid but drag and dropping item onto ItemSlot doesn't work. If I start with an item in ItemSlot I can switch it to another ItemSlot without issue. image

P.S ItemRefSlot seems to work well only if you have an item equipped to start, so I'll use that in the meantime, although can you set constraints like only boots in this slot? with ref because it doesn't take a protoset like ItemSlot does.

kyle-wannacott avatar Feb 09 '24 14:02 kyle-wannacott

@LeeWannacott Oh, this actually looks like something I fixed in v2.4.1. There's still no 2.4.1 release because I'm still polishing things, but it is available on the dev_v2.4.1 branch and you can give it a try. I hope to release it in the following days and it might fix both of these issues.

can you set constraints like only boots in this slot?

You should be able to extend the ItemSlot class and override the can_hold_item function to define what kind of items the slot can hold. Something like this:

extends ItemSlot
class_name ItemSlotFeet

# ...

func can_hold_item(item: InventoryItem) -> bool:
    if item.get_property("name") != "boot":
        return false # Can only hold boots
    else:
        return super.can_hold_item(item) # Still do the checks from the base class

peter-kish avatar Feb 09 '24 19:02 peter-kish

@LeeWannacott Oh, this actually looks like something I fixed in v2.4.1. There's still no 2.4.1 release because I'm still polishing things, but it is available on the dev_v2.4.1 branch and you can give it a try. I hope to release it in the following days and it might fix both of these issues.

Yep looks like dev_v2.4.1 fixes those issues; you can't see what you are dragging but I assume because its under development.

Something like this:

Thanks for example that's helpful, I don't know if I need to override if I use ItemSlot, although it might be better/ less work than having to maintain a protoset for the type of each slot :thinking:

kyle-wannacott avatar Feb 10 '24 05:02 kyle-wannacott

image

@peter-kish Using a default icon on an ItemSlot doesn't seem to work on v2.41. (I haven't tested 2.4.0)

kyle-wannacott avatar Feb 10 '24 07:02 kyle-wannacott

Thanks for example that's helpful, I don't know if I need to override if I use ItemSlot, although it might be better/ less work than having to maintain a protoset for the type of each slot 🤔

Yeah, ideally, you would have only one protoset per game. Overriding can_hold_item (for ItemSlot and ItemRefSlot) and can_add_item (for Inventory) should be way easier than managing multiple protosets.

peter-kish avatar Feb 10 '24 08:02 peter-kish

Yeah, ideally, you would have only one protoset per game. Overriding can_hold_item (for ItemSlot and ItemRefSlot) and can_add_item (for Inventory) should be way easier than managing multiple protosets.

I don't know if overriding the can_hold_item method is the play (providing you are going for user friendly). I think you should potentially just allow the user to declare a "item_type" property in the users item_protoset used by inventory items and it checks whether the slot can handle the item_type. It could be an array so an item slot can handle different types of items like for example weapon slot would have many different types of weapons (wands, shields, two-handed etc) in its item types. Probably not the best explanation...

kyle-wannacott avatar Feb 10 '24 09:02 kyle-wannacott

Never mind, I think I can pretty much achieve this without it being implemented by the library (by using the item_equipped signal). I can just call clear() after the item has been equipped, it seems to happen that fast you can't tell it has been equipped, it just looks like you can't equip it. :rocket:

func _on_head_slot_item_equipped() -> void:
	var equipped_item =  head_slot.get_item()
	if equipped_item.get_property("item_type") != "helm":
		head_slot.clear()

kyle-wannacott avatar Feb 10 '24 09:02 kyle-wannacott

What can be done with the bug that filling the grid_inventory by dragging item is impossible? - one cell remains inactive.

juliusspeak avatar Feb 14 '24 07:02 juliusspeak

Since I'm still unable to reproduce the issue and debug it, we have two options:

  1. Try and debug it yourself. Basically what wrote previously:

One more thing you can try is to place a breakpoint in addons/gloot/ui/ctrl_drop_zone.gd - line 22 after the inventory is cleared. It should trigger when an item is dropped onto the empty inventory. If it doesn't, I'm afraid I'll have to dig deeper.

  1. Create a minimal project that demonstrates the problem, a.k.a. a minimal reproducible example, that I can debug.

peter-kish avatar Feb 14 '24 08:02 peter-kish

Closed due to staleness

peter-kish avatar Sep 08 '24 10:09 peter-kish