swoyer2 I have been working with drag-n-drop recently, let's see if I can help
swoyer2 If I have been using a panel as my inv_ui_slot that contains the item (Sprite2D) and want to communicate with my inventory every time I drag a slot to another slot it can run my swap function.
please slow down
swoyer2 Would I be able to use a built in dragging function like _get_drag_data etc. Or am I coming at this from a bad approach. Either way how would I go about implementing this.
I don't understand what the question is.
func update(slot: InvSlot):
if !slot.item:
# Hide the visual if there's no item
item_visual.visible = false
amount_text.visible = false
else:
# Show the visual and update the texture if there's an item
item_visual.visible = true
item_visual.texture = slot.item.texture
amount_text.visible = true
amount_text.text = str(slot.amount)
you don't need to hide every node, when you hide a node it hides all it's children. if you have 2 different nodes that have to be hidden, it would be best to have a parent node and the others as children.
about your "question", if I understood something, you can pass a reference to self in a dictionary, from there you can obtain the parent of the node that's being dragged or reparent it. you can also pass callables (functions) through the dictionary and call them on the other side.
for swapping parents you need a temp variable to store one of the parents.
func _can_drop_data(_at_position, data):
return typeof(data) == TYPE_DICTIONARY and has_slot(data.get("slot_type", 0))
func has_slot(data) -> bool:
if get_parent().has_method("is_swappable"):
if get_parent().is_swappable():
return data == get_parent().slot_type
else:
return true
else:
return false
func _drop_data(_at_position, data):
var oth : Control = data.get("remove")
var par : Control = self.get_parent()
if get_parent().is_swappable():#I test this in can_drop_data
self.reparent(oth.get_parent(), false)
oth.reparent(par, false)
and this is on the item that can be dragged. In my game you can drag the item and drop it onto another item, and if the parent can hold more than one item it is added, otherwise it is replaced.
edit: You got me thinking so I worked on implementing the dragged-node-becoming-invisible while being dragged.
I used the node notification system for making it visible when dragging ends:
func _notification(what):
if what == NOTIFICATION_DRAG_END:
visible = true
and you set it invisible during _get_drag_data:
func _get_drag_data(at_position):
set_drag_preview(drag_slot)
visible = false