Dup rings in inventory

Godot Version

Godot 4.6

Question

Hi! I’m making a game, and it has an inventory. I have 3 slots for rings, which are easy to fill, but removing the rings becomes a real challenge. The rings are duplicated. I’ve tried to solve this problem myself, but I haven’t come up with a solution. I’m not using AI because I want to write the game myself.

Inventory code:

extends Control

var mouse_inside = null

func _ready() -> void:
	inv_add_item(1)
	inv_add_item(2)
	for i in 4:
		inv_add_item(3)
	for i in 20:
		inv_add_item(0)

func _process(delta: float) -> void:
	if mouse_inside != null:
		if Input.is_action_just_pressed("left_mouse"):
			var item_id = core.antiUse_Equipment(mouse_inside)
			inv_add_item(item_id)
			mouse_inside = null

func inv_add_item(item_id):
	var file = preload("res://INVENTORY/items.tscn").instantiate()
	var item = file.get_child(item_id)
	var item_shab = Button.new()
	item_shab.text = item.item_name + "   X01"
	item_shab.editor_description = item.name
	var for_count = 0
	for i in $Back/ItemsList/Container.get_children():
		for_count += 1
		if i.editor_description == item_shab.editor_description and i.text.right(3) != "199":
			var count_items = i.text.right(2)
			if count_items[1] == "9":
				i.text[i.text.length()-1] = ""
				i.text[i.text.length()-1] = ""
				i.text = i.text + str(int(count_items)+1)
			else:
				if count_items[0] == "0":
					i.text[i.text.length()-1] = ""
					i.text = i.text + str(int(count_items)+1)
				else:
					i.text[i.text.length()-1] = ""
					i.text[i.text.length()-1] = ""
					i.text = i.text + str(int(count_items)+1)
		elif for_count == $Back/ItemsList/Container.get_children().size():
			item_shab.connect("pressed", use, 123)
			$Back/ItemsList/Container.add_child(item_shab)

func use(asd):
	var useless = []
	var useless_count = []
	var can_delete = false

	var file = preload("res://INVENTORY/items.tscn").instantiate()
	var item = file.get_child(int(asd.editor_description))

#---Тут писать обработчики типа предмета---

	if item.item_type == "Poison":
		var ret = await core.Use_Poison(item)
		can_delete = true
	if item.item_type == "Head":
		var ret = core.Use_Equipment(item)
		can_delete = ret
	if item.item_type == "Tors":
		var ret = core.Use_Equipment(item)
		can_delete = ret
	if item.item_type == "Hands":
		var ret = core.Use_Equipment(item)
		can_delete = ret
	if item.item_type == "Gloves":
		var ret = core.Use_Equipment(item)
		can_delete = ret
	if item.item_type == "Trousers":
		var ret = core.Use_Equipment(item)
		can_delete = ret
	if item.item_type == "Boots":
		var ret = core.Use_Equipment(item)
		can_delete = ret
	if item.item_type == "Weapon":
		var ret = core.Use_Equipment(item)
		can_delete = ret
		can_delete = ret
	if item.item_type == "Bracelet":
		var ret = core.Use_Equipment(item)
		can_delete = ret
		can_delete = ret
	if item.item_type == "Ring":
		var ret = core.Use_Equipment(item)
		can_delete = ret

#---Конец---
	if can_delete:
		for i in $Back/ItemsList/Container.get_children():
			if i.editor_description == asd.editor_description:
				useless.append(i)
		for i in useless:
			if i.text[i.text.length()-3] == "X":
				useless_count.append(str(i.text[i.text.length()-2]) + str(i.text[i.text.length()-1]))
			else:
				useless_count.append(str(i.text[i.text.length()-3]) + str(i.text[i.text.length()-2]) + str(i.text[i.text.length()-1]))
		
		var us_min = useless[useless_count.find(useless_count.min())].text
		var integ = us_min.right(2)
		
		if int(integ) == 1 and us_min[us_min.length()-3] == "X":
			useless[useless_count.find(useless_count.min())].queue_free()
		if int(us_min.right(3)) == 100:
			us_min[us_min.length()-1] = ""
			us_min[us_min.length()-1] = ""
			us_min[us_min.length()-1] = ""
			useless[useless_count.find(useless_count.min())].text = us_min + "99"
		elif int(integ) <= 9:
			us_min[us_min.length()-1] = ""
			useless[useless_count.find(useless_count.min())].text = us_min + str(int(integ)-1)
		elif int(integ) >= 10:
			if int(integ) == 10:
				us_min[us_min.length()-1] = ""
				us_min[us_min.length()-1] = ""
				useless[useless_count.find(useless_count.min())].text = us_min + "0" + str(int(integ)-1)
			else:
				us_min[us_min.length()-1] = ""
				us_min[us_min.length()-1] = ""
				useless[useless_count.find(useless_count.min())].text = us_min + str(int(integ)-1)

# --- Обработчики mouse_entered/exited для всех слотов экипировки ---

func _on_head_mouse_entered() -> void:
	if core.Head != null:
		mouse_inside = "Head"
func _on_head_mouse_exited() -> void:
	mouse_inside = null

func _on_tors_mouse_entered() -> void:
	if core.Tors != null:
		mouse_inside = "Tors"
func _on_tors_mouse_exited() -> void:
	mouse_inside = null

func _on_hands_mouse_entered() -> void:
	if core.Hands != null:
		mouse_inside = "Hands"
func _on_hands_mouse_exited() -> void:
	mouse_inside = null

func _on_gloves_mouse_entered() -> void:
	if core.Gloves != null:
		mouse_inside = "Gloves"
func _on_gloves_mouse_exited() -> void:
	mouse_inside = null

func _on_trousers_mouse_entered() -> void:
	if core.Trousers != null:
		mouse_inside = "Trousers"
func _on_trousers_mouse_exited() -> void:
	mouse_inside = null

func _on_boots_mouse_entered() -> void:
	if core.Boots != null:
		mouse_inside = "Boots"
func _on_boots_mouse_exited() -> void:
	mouse_inside = null

func _on_weapon_1_mouse_entered() -> void:
	if core.Weapon1 != null:
		mouse_inside = "Weapon1"
func _on_weapon_1_mouse_exited() -> void:
	mouse_inside = null

func _on_weapon_2_mouse_entered() -> void:
	if core.Weapon2 != null:
		mouse_inside = "Weapon2"
func _on_weapon_2_mouse_exited() -> void:
	mouse_inside = null

func _on_bracelet_1_mouse_entered() -> void:
	if core.Bracelet1 != null:
		mouse_inside = "Bracelet1"
func _on_bracelet_1_mouse_exited() -> void:
	mouse_inside = null

func _on_bracelet_2_mouse_entered() -> void:
	if core.Bracelet2 != null:
		mouse_inside = "Bracelet2"
func _on_bracelet_2_mouse_exited() -> void:
	mouse_inside = null

func _on_ring_1_mouse_entered() -> void:
	if core.Ring1 != null:
		mouse_inside = "Ring1"
func _on_ring_1_mouse_exited() -> void:
	mouse_inside = null

func _on_ring_2_mouse_entered() -> void:
	if core.Ring2 != null:
		mouse_inside = "Ring2"
func _on_ring_2_mouse_exited() -> void:
	mouse_inside = null

func _on_ring_3_mouse_entered() -> void:
	if core.Ring3 != null:
		mouse_inside = "Ring3"
func _on_ring_3_mouse_exited() -> void:
	mouse_inside = null

Part of the core code:

func Use_Equipment(node : Node):
	if node.item_type == "Head" and Head == null:
		Head = node
		Equipment_plus_stat(Head)
		return true
	if node.item_type == "Tors" and Tors == null:
		Tors = node
		Equipment_plus_stat(Tors)
		return true
	if node.item_type == "Hands" and Hands == null:
		Hands = node
		Equipment_plus_stat(Hands)
		return true
	if node.item_type == "Gloves" and Gloves == null:
		Gloves = node
		Equipment_plus_stat(Gloves)
		return true
	if node.item_type == "Trousers" and Trousers == null:
		Trousers = node
		Equipment_plus_stat(Trousers)
		return true
	if node.item_type == "Boots" and Boots == null:
		Boots = node
		Equipment_plus_stat(Boots)
		return true
	if node.item_type == "Weapon" and Weapon1 == null:
		Weapon1 = node
		Equipment_plus_stat(Weapon1)
		return true
	if node.item_type == "Bracelet" and Bracelet1 == null:
		Bracelet1 = node
		Equipment_plus_stat(Bracelet1)
		return true
	if node.item_type == "Ring":
		if Ring1 == null:
			Ring1 = node
			Equipment_plus_stat(Ring1)
		elif Ring2 == null:
			Ring2 = node
			Equipment_plus_stat(Ring2)
		elif Ring3 == null:
			Ring3 = node
			Equipment_plus_stat(Ring3)
		else:
			return false
		return true
	return false

func antiUse_Equipment(item_var : String):
	if item_var == "Head" and Head != null:
		var second_item = Head
		Equipment_minus_stat(Head)
		Head = null
		return second_item.item_id
	if item_var == "Tors" and Tors != null:
		var second_item = Tors
		Equipment_minus_stat(Tors)
		Tors = null
		return second_item.item_id
	if item_var == "Hands" and Hands != null:
		var second_item = Hands
		Equipment_minus_stat(Hands)
		Hands = null
		return second_item.item_id
	if item_var == "Gloves" and Gloves != null:
		var second_item = Gloves
		Equipment_minus_stat(Gloves)
		Gloves = null
		return second_item.item_id
	if item_var == "Trousers" and Trousers != null:
		var second_item = Trousers
		Equipment_minus_stat(Trousers)
		Trousers = null
		return second_item.item_id
	if item_var == "Boots" and Boots != null:
		var second_item = Boots
		Equipment_minus_stat(Boots)
		Boots = null
		return second_item.item_id
	if item_var == "Weapon1" and Weapon1 != null:
		var second_item = Weapon1
		Equipment_minus_stat(Weapon1)
		Weapon1 = null
		return second_item.item_id
	if item_var == "Weapon2" and Weapon2 != null:
		var second_item = Weapon2
		Equipment_minus_stat(Weapon2)
		Weapon2 = null
		return second_item.item_id
	if item_var == "Bracelet1" and Bracelet1 != null:
		var second_item = Bracelet1
		Equipment_minus_stat(Bracelet1)
		Bracelet1 = null
		return second_item.item_id
	if item_var == "Bracelet2" and Bracelet2 != null:
		var second_item = Bracelet2
		Equipment_minus_stat(Bracelet2)
		Bracelet2 = null
		return second_item.item_id
	if item_var == "Ring1" and Ring1 != null:
		var second_item = Ring1
		Equipment_minus_stat(Ring1)
		Ring1 = null
		return second_item.item_id
	if item_var == "Ring2" and Ring2 != null:
		var second_item = Ring2
		Equipment_minus_stat(Ring2)
		Ring2 = null
		return second_item.item_id
	if item_var == "Ring3" and Ring3 != null:
		var second_item = Ring3
		Equipment_minus_stat(Ring3)
		Ring3 = null
		return second_item.item_id
	return null

For now, I’m only testing it on rings, but I’ll apply it to the rest later.

P.s. I can’t attach a video because I’m a new user.
UPD. I uploaded the video to YouTube —> https://youtu.be/iAQh2b4xgRQ?si=iwA5_GoKyil5mPse
UPD2. If I drink all the potions, the error disappears —> https://www.youtube.com/watch?v=zdNr3-XCwUQ

I think it’s great you are trying to figure this out yourself! However, you have a lot of code. I think it’s harder for questions with a lot of code to be answered because it’s hard to sort through all the code and see what the issue is.

I do think you can cut down on how much code you have, since you have a lot of repeated code. You can make the code shorter by making them into functions instead.

For example, you can turn this code

func antiUse_Equipment(item_var : String):
	if item_var == "Head" and Head != null:
		var second_item = Head
		Equipment_minus_stat(Head)
		Head = null
		return second_item.item_id
	if item_var == "Tors" and Tors != null:
		var second_item = Tors
		Equipment_minus_stat(Tors)
		Tors = null
		return second_item.item_id

Into this code:

var slots_dict := {
    "Head": # Put the rest of the slots here
}
func antiUse_Equipment(item_var: String):
    if item_var in slots_dict:
        var second_item = slots_dict[item_var]
        Equipment_minus_stat(second_item)
        slots_dict.erase(item_var)
        return second_item.item_id

If you need more clarification, let me know!

3 Likes

This looks quite effective. I’ll rewrite the code and try to understand the error again, maybe it will become clearer. Thanks for the advice! If you have any ideas about the error, please let me know.

1 Like

UPD. I uploaded the video to YouTube —> https://youtu.be/iAQh2b4xgRQ?si=iwA5_GoKyil5mPse

UPD2. If I drink all the potions, the error disappears —> https://www.youtube.com/watch?v=zdNr3-XCwUQ

The problem is inside the for-loop of the inv_add_item() function:

If the inventory has two items (RingOfHell x1, HealPoison x20) while unequipping one RingOfHell, on the first iteration the for-loop will increase the amount of RingOfHell to x2, and on the second iteration the elif-condition will be true, so a new RingOfHell x1 will be added to the inventory.
This will always happen if the item is already in the inventory, but not at the last position. (Also, if there are multiple instance of an item in the inventory, all will increase by 1.)

I think it should be enough to break from the for-loop at the end of the if-part to fix this:

	for i in $Back/ItemsList/Container.get_children():
		for_count += 1
		if i.editor_description == item_shab.editor_description and i.text.right(3) != "199":
			var count_items = i.text.right(2)
			if count_items[1] == "9":
				i.text[i.text.length()-1] = ""
				i.text[i.text.length()-1] = ""
				i.text = i.text + str(int(count_items)+1)
			else:
				if count_items[0] == "0":
					i.text[i.text.length()-1] = ""
					i.text = i.text + str(int(count_items)+1)
				else:
					i.text[i.text.length()-1] = ""
					i.text[i.text.length()-1] = ""
					i.text = i.text + str(int(count_items)+1)
+++			break
		elif for_count == $Back/ItemsList/Container.get_children().size():
			item_shab.connect("pressed", use, 123)
			$Back/ItemsList/Container.add_child(item_shab)
1 Like

Thank you very much!