Handle input for multiple dynamically generated GUI (SOLVED)

I’m dynamically generating box nodes to display items in my inventory. I want the player to be able to drag each of the boxes. How would I go about doing this?

I’m trying to do something like this:
inventory

Below is how i generate my boxes

for i, v in pairs(table) do
    local node = gui.new_box_node(vmath.vector3(x, y, 0), vmath.vector3(140, 140, 0))

    x = x + 150
    if x >= 600 then
        x = 0
        y = y - 160
    end
end

Here’s my suggestion:

  • Store all of the box nodes in a list once you’ve created them
  • Detect mouse clicks in your on_input() function
    • Loop over the list of box nodes
    • Use gui.pick_node() to see which box node was clicked (if any!)
    • Store the clicked node in a variable
  • Move the clicked node
    • Move the clicked node when the mouse moves
    • Stop when the player releases the mouse button

I created an example showing the above solution:

You can also use the Druid library to build this kind of thing:

1 Like

Thank you! I’ll try to do just that.