# Drag&Drop inventory issue

**URL:** <https://forums.unrealengine.com/t/drag-drop-inventory-issue/2063197>\
**Category:** Blueprint\
**Tags:** unreal-engine, Blueprint, question\
**Created:** [October 11, 2024, 10:23am UTC](https://forums.unrealengine.com/t/drag-drop-inventory-issue/2063197 "2024-10-11T10:23:22Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![ybane](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/ybane/32/933134_2.png) [@ybane](https://forums.unrealengine.com/u/ybane)\
**Post date:** [October 11, 2024, 10:23am UTC](https://forums.unrealengine.com/t/drag-drop-inventory-issue/2063197/1 "2024-10-11T10:23:23Z")

</div>

**Hey all!** I’m trying to make a slot based inventory with drag and drop system, but something went wrong.  
The problem is when I drag and drop an item for the first time - it works fine, but for the second time all other items except first one are gone. You can see it on the video: [https://youtu.be/ihO1cNmMg\_c](https://youtu.be/ihO1cNmMg_c)

What causes the problem is my own inventory refreshing as I think. I’ll try to make it clear.  
The algorithm:  
Player got an array of item structures

**BeginPlay** → Resize array of structs to 20

**Item picked up** → Item added to array of structs (Sets an item by first empty index as we got 20 empty slots)

**Inventory opened** → Loop for each element in array of structs → Add element to ListView(via constructing an object with ItemStructure data)

 ![Opening the inventory](https://d3kjluh73b9h9o.cloudfront.net/original/4X/d/8/2/d82cd56a0376a83e6fe2e6c1edf34f192758f574.png)

**Item Dragged and Dropped** → Swap elements in array of structs → Refresh the inventory(Clear List Items → then the same as when the inventory opened)

 ![Inventroy Refresh](https://d3kjluh73b9h9o.cloudfront.net/original/4X/a/4/3/a434e5227846894c486df73f860b4c00f88bfb43.png)

On the video I linked you can see that when the items are cleared, it adds new items but starting from index 20. Really hoping for help, can’t figure it out on my own

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [October 11, 2024, 10:34am UTC](https://forums.unrealengine.com/t/drag-drop-inventory-issue/2063197/2 "2024-10-11T10:34:09Z")

</div>

> [@ybane](#):
>
> ListView(via constructing an object with ItemStructure data)

Could you show this bit?

---

<div class="post-metadata">

**Author:** ![ybane](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/ybane/32/933134_2.png) [@ybane](https://forums.unrealengine.com/u/ybane)\
**Post date:** [October 11, 2024, 10:39am UTC](https://forums.unrealengine.com/t/drag-drop-inventory-issue/2063197/4 "2024-10-11T10:39:22Z")

</div>

How it constructed in widget

 ![Construction](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/c/6/cc67c82f450c9b9759faf3a109ce67942daae48c.png)  
The Init function  
 ![Function](https://d3kjluh73b9h9o.cloudfront.net/original/4X/3/9/f/39f0a9e9bf7b519cc9e62191f8d06a8622753c2b.png)

---

<div class="post-metadata">

**Author:** ![ybane](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/ybane/32/933134_2.png) [@ybane](https://forums.unrealengine.com/u/ybane)\
**Post date:** [October 11, 2024, 10:40am UTC](https://forums.unrealengine.com/t/drag-drop-inventory-issue/2063197/5 "2024-10-11T10:40:43Z")

</div>

I’ve found a node called Regenerate All Entries, tryna get it work with this node

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [October 11, 2024, 10:46am UTC](https://forums.unrealengine.com/t/drag-drop-inventory-issue/2063197/6 "2024-10-11T10:46:48Z")

</div>

Do tell how it goes.

* * *

Unrelated note:

Instead of `Init`, we can:

![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/9/9/2/992d4302e89aabd651d348930ee10c9ea80e9a1e.png)  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/d/e/3/de3a67732ca20bf0480ef24cc1b2181fa2aee9fa.png)

Saves you a call, which may be important-ish if you have 10000 calls to make. Works with any type of spawn / create / construct / add component.

---

<div class="post-metadata">

**Author:** ![ybane](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/ybane/32/933134_2.png) [@ybane](https://forums.unrealengine.com/u/ybane)\
**Post date:** [October 11, 2024, 10:50am UTC](https://forums.unrealengine.com/t/drag-drop-inventory-issue/2063197/7 "2024-10-11T10:50:13Z")

</div>

Yeah, I know about that function I can remove, will do it later when finish with it. Thanks!

---

<div class="post-metadata">

**Author:** ![ybane](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/ybane/32/933134_2.png) [@ybane](https://forums.unrealengine.com/u/ybane)\
**Post date:** [October 11, 2024, 10:59am UTC](https://forums.unrealengine.com/t/drag-drop-inventory-issue/2063197/8 "2024-10-11T10:59:42Z")

</div>

Seems like it doesn’t work. Now it doesnt swap in my UI at all, but in the inventory(array of structs) it does like as always. What I did:  
Item picked up → Item adds to ListVew  
Item dragged and dropped → Regenerate All Entries → Request Refresh(just in case)

UPD: Also did swap of entries, not just structs

---

<div class="post-metadata">

**Author:** ![ybane](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/ybane/32/933134_2.png) [@ybane](https://forums.unrealengine.com/u/ybane)\
**Post date:** [October 11, 2024, 12:48pm UTC](https://forums.unrealengine.com/t/drag-drop-inventory-issue/2063197/9 "2024-10-11T12:48:55Z")

</div>

I almost solved the problem or found the exact reason of it.  
The solution(almost solution) is:  
Remove From Parent → Delay 0.001 sec → Add to Viewport

The reason of the problem was that when we clear all entries in the list - it remembers the amount of them and starts from 20(in my case).

So I’m just destroying the ListView and constructing it again

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [October 11, 2024, 1:01pm UTC](https://forums.unrealengine.com/t/drag-drop-inventory-issue/2063197/10 "2024-10-11T13:01:05Z")

</div>

> [@ybane](#):
>
> Remove From Parent → Delay 0.001 sec → Add to Viewport

You could instead delay until next frame.

![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/5/7/a/57a62e830ad021ef3cecae37b3bd0ece7f5e1aa6.png)

Note that Adding to Viewport triggers `Construct`. Any script there will be re-run. Every time.

* * *

But is there a reason why you’re clearing the ENTIRE list view? Why not remove only the undesired elements → regenerate. The list view would then regenerate only the visible widgets. Sounds wasteful and undoes all the performance benefits list views bring. If you’re reconstructing all from scratch, why not pop widgets into a simple Grid Panel instead?

Asking as you may have your reasons.

---

<div class="post-metadata">

**Author:** ![ybane](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/ybane/32/933134_2.png) [@ybane](https://forums.unrealengine.com/u/ybane)\
**Post date:** [October 11, 2024, 1:19pm UTC](https://forums.unrealengine.com/t/drag-drop-inventory-issue/2063197/11 "2024-10-11T13:19:23Z")

</div>

This node works great, thanks!

The list constructs when player opens up the inventroy. Perhaps there is a way to replace each item when it’s added to the inventory but I don’t know what’s better. I’d try to make it

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [October 11, 2024, 1:39pm UTC](https://forums.unrealengine.com/t/drag-drop-inventory-issue/2063197/12 "2024-10-11T13:39:23Z")

</div>

> The list constructs when player opens up the inventroy.

It also fires `Construct` whenever you `Add to Viewport`. This will re-layout the parent widget and any children widgets. That’s why it’s not recommended - not for complex widgets, not for widgets that must retain data, and not for widgets that rely on Construct script. Changing `Visibility` is just so much more efficient, and safer.

Hope you see what I mean.

> [@ybane](#):
>
> Perhaps there is a way to replace each item

I, on the other hand, am not sure what you mean. Do you mean to update an existing object with new data so the list view can update instanced widgets? Because that’s the whole point of using a list view. The point is to create the objects once (or add new ones / remove them when needed); not to remove all / respawn all. Utterly defeats the performance benefits unless you can justify it with a lower memory footprint. But that’s grasping at straws.

On the other hand, it may not matter if the inventory is small. However, if the inventory is small, there’s no need for a list view either 🤷

* * *

In short:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/a/4/3/a434e5227846894c486df73f860b4c00f88bfb43.png)

- this should be done only **once** per game launch / save / load
- from this point, add new / remove unwanted objects only, and only when needed
- you’d want to clear the entire list view (which destroys the underlying objects & widgets) when the player actually **lost**  **their**  **entire** inventory

Hope that makes sense; consider it and good luck!

---

<div class="post-metadata">

**Author:** ![ybane](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/ybane/32/933134_2.png) [@ybane](https://forums.unrealengine.com/u/ybane)\
**Post date:** [October 11, 2024, 1:44pm UTC](https://forums.unrealengine.com/t/drag-drop-inventory-issue/2063197/13 "2024-10-11T13:44:45Z")

</div>

I get it now. I’ll take a look what I can fix in it and probably text here back later, thanks so much for you answers!!

---

<div class="post-metadata">

**Author:** ![ybane](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/ybane/32/933134_2.png) [@ybane](https://forums.unrealengine.com/u/ybane)\
**Post date:** [October 15, 2024, 10:49am UTC](https://forums.unrealengine.com/t/drag-drop-inventory-issue/2063197/14 "2024-10-15T10:49:29Z")

</div>

Greetings again! I’ve reworked this system and now items add to the list at the same time as to the inventory, so there is no loop no more. That works better but I have the same issue.

The issue is - the list doesn’t update index of items in itself.

This is how it works:

 ![Swapping Items](https://d3kjluh73b9h9o.cloudfront.net/original/4X/3/0/7/307dae029afbc6f359b158d2ab79bf612522ea57.png)

I think I need to update the list but don’t know how. It works via destructing it and constructing again but I’d like to find a better way to refresh it

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [October 15, 2024, 11:15am UTC](https://forums.unrealengine.com/t/drag-drop-inventory-issue/2063197/15 "2024-10-15T11:15:48Z")

</div>

Do read their tooltips as these two work quite differently:

![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/1/3/3/133141cf9eed8c8746a13fa84310893c0683143b.png)

---

<div class="post-metadata">

**Author:** ![ybane](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/ybane/32/933134_2.png) [@ybane](https://forums.unrealengine.com/u/ybane)\
**Post date:** [October 15, 2024, 12:15pm UTC](https://forums.unrealengine.com/t/drag-drop-inventory-issue/2063197/16 "2024-10-15T12:15:34Z")

</div>

I’ve read the tooltips but still don’t get it. In theory request refresh should update the indexes of items but it doesn’t.  
So in practice Regenerate All Entries makes all items in list empty, and Request Refresh does nothing
