Topic was automatically imported from the old Question2Answer platform.
Asked By
Antilo
Hi,
I’m building a inventory system for a rpg and when the monster die an enemy bag-loot is instanced an you get close and open it a Canvas layer (with layer =2) child of the bag-loot is Show() and also de the PC-inventory , PC-inventory is in another canvas layer (with layer=1) child of the main node of game, i need drag and drop from loot to inventory and viceversa. The problem is that when a drag_preview from invetory to bag-loot … the preview node is “painted” in the origin canvaslayer, layer1… so… when i try to drag over loot bag, the preview is show behind the layer 2 … and cant be see it and is weird… any help or idea??? Make both canvaslayers in same layer property does not help… IMAGEN SHOWING THE PROBLEM
Hi,
did you manage to solve the issue? I’m facing the same problem.
I’ve found a solution. Here Reduz (One of godot’s authors) explains that “Control” nodes do not have Z-levels (order of drawing). Instead they are drawn in the order they are in the node tree.
Therefore you need to get the dragged item (ie. the gold on your picture) on the same level as the inventory window and move it under it in the node tree, so it’s drawn after inventory window is drawn.
For example consider following node tree:
Main
Inventory
Anvil
I want to drag items I forged from anvil to inventory. So I create the preview node, probably as TextureRect and set it as preview via set_drag_preview(). The node tree will look like this:
Main
Inventory
Anvil
Blade (TextureRect)
At this moment the blade will be hidden behind the inventory after I drag it there. To make it drawn on top of inventory, I need to remove it as a child of the Anvil node and make it a child of the Main node. So my get_drag_data() will have these functions:
get_drag_data(position):
var preview = TextureRect.new()
preview.set_name("Preview")
preview.set_texture(get_texture())
set_drag_preview(preview)
remove_child(preview)
Main.add_child(preview)
return data # Data variable of the ItemNode
The final node tree will look like this:
Main
Inventory
Anvil
Blade
The blade will get drawn as last, which means it will be drawn over the inventory screen.
I had some doubts, but this worked for me as well, thanks!