Method _get_drag_data not called when loaded as singleton/autoload

Godot Version

4.0

Question

I followed a youtube tutorial and was able to implement the inventory but when i load the scene of the inventory as a singleton, the inventory shows up but when i try to select an item, it does not pick it up to drag and drop. Placed a print statement in the ready function and i see them but _get_drag_data seems to never be called along with _can_drop_data and _drop_data. do i need to connect them via script? im not really sure how i would create the connect since i understand _get_drag_data is looking for clicks and drags plus one argument. i dont know how _can_drop_data and _drop_data are being called either.

InventoryItem.gd

extends TextureRect
class_name InventoryItem

@export var data: ItemData	

func init(d: ItemData) -> void:
	data = d

func _ready():
	print('hi2')
	expand_mode = TextureRect.EXPAND_IGNORE_SIZE
	stretch_mode = TextureRect.STRETCH_KEEP_ASPECT_CENTERED
	if data:
		texture = data.texture

func _get_drag_data(at_position: Vector2):
	print("here")
	set_drag_preview(make_drag_preview(at_position))
	return self
	
func make_drag_preview(at_position: Vector2):
	print("here2")
	var t := TextureRect.new()
	t.texture = texture
	t.expand_mode = TextureRect.EXPAND_IGNORE_SIZE
	t.stretch_mode = TextureRect.STRETCH_KEEP_ASPECT_CENTERED
	t.custom_minimum_size = size
	t.modulate.a = 0.5
	t.position = Vector2(-at_position)
	t.z_index = 10
	
	var c := Control.new()
	c.add_child(t)
	
	return c

InventorySlot.gd

extends PanelContainer
class_name InventorySlot

@export var type: ItemData.Type

func init(t: ItemData.Type, cms: Vector2) -> void:
	type = t
	custom_minimum_size = cms

func _can_drop_data(_at_position: Vector2, data: Variant):
	print('help')
	if data is InventoryItem:
		if type == ItemData.Type.MAIN:
			if get_child_count() == 0:
				return true
			else:
				if type == data.get_parent().type:
					return true
			return get_child(0).data.type == data.data.type
		else:
			return data.data.type == type
	return false

func _drop_data(_at_position: Vector2, data: Variant):
	print("help10")
	if get_child_count() > 0:
		var item := get_child(0)
		if item == data:
			return
		item.reparent(data.get_parent())
		if data.get_parent().name == "WeaponSlot":
			get_node("../../../../Player").change_weapon(item)
		else:
			get_node("../../../../../Player").change_weapon(data)
	data.reparent(self)

Are your inventory items responding to input at all? Maybe the mouse input signal ends up in another node, or your inventory item has mouse interaction disabled.

i solved it by instantiating the inventory scene into the root via code on the initial scene.
here is the line
get_tree().get_root().add_child.call_deferred(load(“res://Scenes/UI/inventory.tscn”).instantiate())

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.