I’m making a drag-and-drop hotbar using this tutorial. When an item is selected on the hotbar, it shows a panel, and when you click it again, it hides it. But when I click another item in the hotbar while something is already selected, it shows the panel over the other panel. I would like to hide the other panel when another item is selected. How can I achieve this?
Thanks in advance. I feel I need to say that I’m not asking anyone to make the whole system for me.
So I figured out how to do it; it probably isn’t the best way, but it’s fine for now.
I decided to create a global variable to hold the path for the visible node, but then I got an error, so I changed it to a panel (but you can change it to whatever node you’re using; in my case, it’s a panel). I also created two new scripts with the visibility_changed() function:
And in the item_slot.gd, I created a new if statement where I check if the autoloaded variable isn’t null, and then I set the variable to be equal to the panel that’s showing. Then I hide it and show the item in the hotbar that was clicked:
#Checking if the global variable isn't null
if autoload._node != null:
#Getting the global variable
var _node = autoload._node
#Setting it's visibility to false
_node.visible = false
#Setting the other node's visibility to true
node.visible = true
#Printing the global variable for debugging
print(str(autoload._node))
If you know a way to improve this, please tell me
If anyone wants access to the full project, I included it in the original post.
You may prefer to use a static variable instead, it’s like a global but for the script/class. Maybe you can also assign it when visible, rather than using a separate script and relying on signals elsewhere
static var active_panel: Panel = null
# aside: you can use `get_node_or_null` to avoid errors logged
var node = get_parent().get_parent().get_parent().get_node_or_null(item.path)
if event.is_action_pressed("l_click") and node != null:
if active_panel:
active_panel.visible = false
node.visible = true
active_panel = node