I’m trying to make a “Minecraft-style” inventory system, but I can’t figure out how to get a reference to the widget that’s being dragged. I also need to get a reference to the widget that the dragged widget is dropped on, so that it can be picked up. How can I accomplish this?
The magic word is:
DragDropOperation
It is a Blueprint Class, that can hold any type of variable and you should spawn one when DragDetected is fired.
Set each variable in the DDO to be ExposedOnSpawn and InstanceEditable.
you can (for example) store a reference to your start widget in that DDO.
and OnDrop, get the “Operation”, cast it to your custom one… and take that reference to do stuff with it… like (f.e.) return the Ref to the Drop Widget back to the Source…
in a DataTable driven Inventory f.e. it is recommendet to set the OtemName inside the DDO, and the ItemQuantity… so the Drag and Drop knows what item and how many of that should be dragged. And… a referenc to the Source Inventory Slot.
And on drop, the target slots receives the itemname and quantity, and the source slot gets reduced by this quantity via the ref of the DDO…
I currently have my inventory set up as a uniform grid panel with 40 images as children. Is there a way to detect which image is being clicked on within the array of children?
yes… don’t use images at all…
a unigrid is okay. but don’t populate it with children.
better:
Beside your InventoryWidget, create a second Widget InventorySlot for just the Grid Slots.
in the slot widget’s graph, add the OnMouseDown, OnDragDetected, OnDrop and OnDragCancelled functions.
do all your DnD Stuff there, not in the inventory itself. use the InventoryWidget only for populating the grid, based on the inventory content array.
But… i really recommend watching Tutorials about Inventories… Like… from RyanLaley…
He has two Inventory Tutorials… one for 4.7 and one for 5…
Watch both… learn from them (not copy & pasting 1:2 to your project).
in a game, inventories and inventory slots are the basement for nearly everything that needs to handle items… Equipment Slots, Shop Slots… Crafting Slots… everything is working the same… so… make sure to learn those fundamentals of Array manipulation, Widget Object oriented design and the Widget operations like Drag&Drop ![]()
I watched a bunch of tutorials, and was able to build out most of the functionality for my inventory system. It is currently capable of dragging images, cancelling a drag, and changing the texture of another image to the texture of the dragged image. However, what I’m still struggling with is getting the dragged image to change its texture to the texture of the image being dropped on. For instance, if I want to move a sword image to the slot a shield image is in, the shield image will turn into a sword, but the sword won’t turn into a shield. How could I get these two images to appear as though they have ‘swapped’?
by overwriting the contwnt of the origin with the contwnt of the target Slot.
That’s why you want to put the item name, dragged quantity, origin inventory and origin slot Index inro the DragDropOperation.
Ok, I think I understand now. I am running into a sort of specific problem, though. I’m populating the images of my inventory with PNGs stored as a local file using the ‘download image’ node to convert them into dyanmic texture 2Ds. I’ve found a node called ‘Get Brush Resource as Texture 2D’, but I haven’t found any node for getting a texture 2d dynamic. Do you know if there is a way to get the texture 2d dynamic from an image?
I’m trying to ‘get’ the brush from one image so that I can ‘set’ it on another. ‘Set brush from texture dynamic’ works for setting it, but what I was asking about is how to ‘get’ it.
ok.
first… we make sure…
Your Inventory is a UMG Widget… f.e. “UI_Inventory”
And the Slots of that Inventory is a separate Widget f.e. “UI_InventorySlot”?
If no… do so…
cause currently it reads as if you just added a bunch of images into a single wudget and try to move their data around… what is really hard… and not recommendet…
if you have the Slots as separate widgets:
Add a Variable of Type Texture2DDynamic.
Have such a variable in your DragAndDropOperation, too… but exposed.
when loading the image into memory, you stire the Texture2DDynamic output into that variable and simply pass it along with your DnDOperation…
just to make it clear…
You NEVER have an actual Texture as Variable somehwere… neither in a Variable, nor in a brush. All you have is a reference to your Texture in Memory.
f.e. If you load a Texture2D, it gets copied into memory and a Reference is returned, that you can pass along… and everyone having that reference, can load the texture from there.
Same principle with Tex2DDynamic… just, they are not already stored as memkry friendly uasset, but loaded from your drive, formated and then memcopied to the memory as if it is a Texture2D… and all you get returned is a reference to its Stack.
that’s the reason you want an Instanced AssetManager for such Objects, that get read In from Disc/Drive instead of uassets. to hold their references on a central place to grab from that.
Ok that makes a lot of sense. Thanks for explaining everything in detail. I guess the only questions I have left is how I would get the inventory to save its layout after its been changed? And how could I prevent ‘blank’ images from being dragged?