I have created an inventory, but it is very bad

Godot Version

4.1.3

Question

I’ve made an inventory and it even works, but there are four problems that I can’t solve.

  1. When I drag an object from one cell to another, and touch another object, it disappears (although the dragged object passed by).
  2. If I start dragging an item behind the inventory, it will still appear, and I can even put it down.
  3. If you throw an item out of the inventory cell and stop dragging, it disappears, and does not return.
  4. I do not know how to make support for other items that I will add in the future.

It’s very difficult for me, I’ve been thinking about it for a little more than half a month, tell me how to fix it! The tree of the inventory panel: Panel —> Sprite2D, Button

The script:

extends Panel

var items_list = [
	preload("res://Inv/Items/sword.tres") #Way
]

@onready var pos = $Sprite2D.position
var scr = Sprite2D.new()

func _ready():
	pass

func _process(delta):

	if Input.is_action_just_pressed('mouse_left'):
		scr.texture = $Sprite2D.texture

	elif Input.is_action_pressed('mouse_left'):
		scr.visible = true
		scr.top_level = true
		var asd = get_global_mouse_position()
		scr.position = asd
		#scr.modulate = 'ffffff90'
		scr.modulate = 'ffffff64'
		get_parent().get_parent().add_child(scr)
	else:
		$Sprite2D.visible = true
		scr.visible = false
		$Sprite2D.position = pos
		$Sprite2D.modulate = 'ffffff'
		$Sprite2D.top_level = false

func _on_button_mouse_entered():
	if Input.is_action_pressed('mouse_left') and scr.visible:
		$Sprite2D.visible = false
		$Sprite2D.texture = items_list[0].texture
		$Button.tooltip_text = items_list[0].name + '\n' + items_list[0].description

func _on_button_mouse_exited():
	if Input.is_action_pressed('mouse_left'):
		$Sprite2D.visible = true
		$Sprite2D.texture = null
		$Button.tooltip_text = ''

func _on_button_focus_entered():
	if Input.is_action_pressed('mouse_left'):
		$Sprite2D.visible = false

Please help me!

you should change a lot of the is is_action_pressed to is_action_just_pressed

1 Like

I don’t know much about your variable and how you set your item so this might not work

extends Panel

var item_is_on_hand = false
var items_list = [
	preload("res://Inv/Items/sword.tres") #Way
]

@onready var pos = $Sprite2D.position
var scr = Sprite2D.new()

func _ready():
	pass

func _process(delta):

	if Input.is_action_just_pressed('mouse_left') & item_is_on_hand == false:
		scr.texture = $Sprite2D.texture
		scr.visible = true
		scr.top_level = true
		var asd = get_global_mouse_position()
		scr.position = asd
		#scr.modulate = 'ffffff90'
		scr.modulate = 'ffffff64'
		get_parent().get_parent().add_child(scr)
		item_is_on_hand = true
	if Input.is_action_just_released('mouse_left') && item_is_on_hand == true:
		$Sprite2D.visible = true
		scr.visible = false
		$Sprite2D.position = asd
		$Sprite2D.modulate = 'ffffff'
		$Sprite2D.top_level = false
		item_is_on_hand = false

func _on_button_mouse_entered():
	if Input.is_action_pressed('mouse_left') and scr.visible:
		$Sprite2D.visible = false
		$Sprite2D.texture = items_list[0].texture
		$Button.tooltip_text = items_list[0].name + '\n' + items_list[0].description

func _on_button_mouse_exited():
	if Input.is_action_pressed('mouse_left'):
		$Sprite2D.visible = true
		$Sprite2D.texture = null
		$Button.tooltip_text = ''

func _on_button_focus_entered():
	if Input.is_action_pressed('mouse_left'):
		$Sprite2D.visible = false
1 Like

Thanks for the answer, but the script doesn’t work. I tried to integrate your script into my script using a neural network, and the script even changed, but the functionality remained the same, it even got a little worse… Also, I’ve already tried to do something similar to yours ,item_is_on_hand, , but when I did, the item refused to move. Please tell me in which direction I should move in order for everything to work, and maybe I will succeed.
Thank you in advance for the answer!
P.S. If you don’t understand something in the code, ask.

Why would you use a neural network to integrate the code?

I think your issue is these _on_button_mouse_exited funcs only run when the mouse enters or exits a button, it will only register clicks the exact moment you enter or exit the button. If these are UI Buttons try connecting to the pressed signal instead.

1 Like

I think I understood the idea, and even implemented it, only the object does not move.

Code:

extends Panel

var items_list = [
	preload("res://Inv/Items/sword.tres") #Way
]

@onready var pos = $Sprite2D.position
var scr = Sprite2D.new()
var count = 0
@onready var tf = $tf

# Called when the node enters the scene tree for the first time.
func _ready():
	pass # Replace with function body.

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):

	if Input.is_action_just_pressed('mouse_left'):
		scr.texture = $Sprite2D.texture

	elif Input.is_action_pressed('mouse_left'):
		scr.visible = true
		scr.top_level = true
		var asd = get_global_mouse_position()
		scr.position = asd
		#scr.modulate = 'ffffff90'
		scr.modulate = 'ffffff64'
		get_parent().get_parent().add_child(scr)
	else:
		$Sprite2D.visible = true
		scr.visible = false
		$Sprite2D.position = pos
		$Sprite2D.modulate = 'ffffff'
		$Sprite2D.top_level = false

func _on_button_button_down():
	if Input.is_action_pressed('mouse_left'):
		$Sprite2D.visible = false
		$Sprite2D.texture = items_list[0].texture
		$Button.tooltip_text = items_list[0].name + '\n' + items_list[0].description
	if Input.is_action_just_released('mouse_left'):
		$Sprite2D.visible = true
		$Sprite2D.texture = null
		$Button.tooltip_text = ''

why not just the events?

func _on_button_button_down():
	$Sprite2D.visible = false
	$Sprite2D.texture = items_list[0].texture
	$Button.tooltip_text = items_list[0].name + '\n' + items_list[0].description

func _on_button_button_up():
	$Sprite2D.visible = true
	$Sprite2D.texture = null
	$Button.tooltip_text = ''
1 Like

This option still works only in one panel, and when you try to move an item, the item disappears.

I decided to try this code:

extends Panel

var items_list = [
	preload("res://Inv/Items/sword.tres") #Way
]

@onready var pos = $Sprite2D.position
var scr = Sprite2D.new()
var count = false
@onready var tf = $tf

# Called when the node enters the scene tree for the first time.
func _ready():
	pass # Replace with function body.

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):

	if Input.is_action_just_pressed('mouse_left'):
		scr.texture = $Sprite2D.texture

	elif Input.is_action_pressed('mouse_left'):
		scr.visible = true
		scr.top_level = true
		var asd = get_global_mouse_position()
		scr.position = asd
		#scr.modulate = 'ffffff90'
		scr.modulate = 'ffffff64'
		get_parent().get_parent().add_child(scr)
		count = true
	else:
		$Sprite2D.visible = true
		scr.visible = false
		$Sprite2D.position = pos
		$Sprite2D.modulate = 'ffffff'
		$Sprite2D.top_level = false

func _on_button_button_down():
	if Input.is_action_pressed('mouse_left') and count:
		$Sprite2D.visible = true
		$Sprite2D.texture = null
		$Button.tooltip_text = ''

func _on_button_mouse_entered():
	if Input.is_action_pressed('mouse_left'):
		if count:
			$Sprite2D.visible = false
			$Sprite2D.texture = items_list[0].texture
			$Button.tooltip_text = items_list[0].name + '\n' + items_list[0].description

But the problem is that if an item passed one more cell, then the item appeared in all the cells in which it passed.