Drag and Drop Question

RahvinRahvin Posts: 1Member

Hello,

I'm trying to get a simple drag and drop mechanism to work, but without success.
I have a grid of elements and I'd like to be able to drag one element on another.

This is what I have so far:

When the game starts, I create a grid of elements dynamically (this is my start scene):

Each CardContainer should be able to be draged and droped an each other CardContainer. This is the code and how it looks like, when the game starts:

Any idea what I am doing wrong?
Many thanks in advance :).

Comments

  • cowhand214cowhand214 Posts: 9Member
    edited June 24

    I don't have any special insight as I'm just learning my way around Godot as well but I just remembered there's a small demo project on the Godot demos repository that has the described functionality. It looks like it's a couple years old but hopefully still useful.

    https://github.com/godotengine/godot-demo-projects/tree/2.1/gui

    EDIT: Nevermind, it doesn't look very useful. The only thing it really shows is the get_drag_data etc. which you're already doing. Sorry.

  • whooshfrostedwhooshfrosted Posts: 28Member
    edited June 29

    I got this working recently. Its quite simple once you understand how it works. I'll post some code I'm using and hopefully you can figure it out from that.

            *********************************************************
            Script for the items you are dragging around:
            *********************************************************
            extends Control
    
            # For parent node with script, to detect drags and drops:
            # Ignore Mouse = false, Stop Mouse = true
            # For child nodes to ignore drags and drops:
            # Ignore Mouse = true, Stop Mouse = false
    
            func get_drag_data(pos):
                var inventory_panel_item = self.duplicate()
                set_drag_preview(inventory_panel_item)
                return self
    
            func can_drop_data(pos, data):
                if self == data:
                    prints("Cannot drop: is over self")
                    return false
                else:
                    # Check if the item is the correct type for this item.
                    # Swords can't be dropped on shields etc
                    prints("Can drop: is over another item")
                    return true
    
            func drop_data(pos, data):
                prints("Dropped", data.item.identified_name, "on", self.item.identified_name)
    
  • whooshfrostedwhooshfrosted Posts: 28Member
    edited June 29
        **************************************************************
        Script for the slots you are dropping the items on
        **************************************************************
        extends Control
    
    
        func can_drop_data(pos, data):
            prints("is over slot:", label_text)
            # Check if the item is the correct type for this slot.
            # Swords can't be dropped in the shield slot
            return true
    
    
    
        func drop_data(pos, data):
            prints("Dropped", data.item.identified_name, "on slot", self.label_text)
    
Sign In or Register to comment.