# Prevent garbage collection for item in inventory

**URL:** <https://forums.unrealengine.com/t/prevent-garbage-collection-for-item-in-inventory/1379376>\
**Category:** Blueprint\
**Tags:** garbage-collection, unreal-engine, question\
**Created:** [November 10, 2023, 9:01pm UTC](https://forums.unrealengine.com/t/prevent-garbage-collection-for-item-in-inventory/1379376 "2023-11-10T21:01:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![spielefreak](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@spielefreak](https://forums.unrealengine.com/u/spielefreak)\
**Post date:** [November 10, 2023, 9:01pm UTC](https://forums.unrealengine.com/t/prevent-garbage-collection-for-item-in-inventory/1379376/1 "2023-11-10T21:01:11Z")

</div>

Hey everyone, I am pretty new to unreal. I want to pick up my item and add it to the player inventory after that remove the item from the world. But I don’t know how to keep a valid reference so the garbage collection doesn’t try to delete it after I call “Destroy Actor”. An idea was to just hide the item but that seems more like a workaround and not a solution.

Item Actor BP:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/b/f/8/bf80a9e0e791dc2eb9e6bf9365024b976a459f65.png)  
FirstPersonCharacter BP:  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/4/1/1/4112b43d3074dbe6b002a0dcbf01534cee8cde75.png)  
Inventory Widget BP:  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/8/8/8/8882f71e51e0da734eff35d9603ec17d51576e2c.png)

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![3dRaven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/3draven/32/906552_2.png) [@3dRaven](https://forums.unrealengine.com/u/3dRaven)\
**Post date:** [November 10, 2023, 9:16pm UTC](https://forums.unrealengine.com/t/prevent-garbage-collection-for-item-in-inventory/1379376/2 "2023-11-10T21:16:32Z")

</div>

Don’t add the item itself. Add a struct that encompasses the items traits. Then destroy the item.

Each item should have this struct inside of it. It can have the name, effects, sell cost etc.

---

<div class="post-metadata">

**Author:** ![spielefreak](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@spielefreak](https://forums.unrealengine.com/u/spielefreak)\
**Post date:** [November 10, 2023, 10:31pm UTC](https://forums.unrealengine.com/t/prevent-garbage-collection-for-item-in-inventory/1379376/3 "2023-11-10T22:31:00Z")

</div>

Got it! Just learned what structures are. Thanks a lot

---

<div class="post-metadata">

**Author:** ![Auran131](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Auran131](https://forums.unrealengine.com/u/Auran131)\
**Post date:** [November 11, 2023, 10:02am UTC](https://forums.unrealengine.com/t/prevent-garbage-collection-for-item-in-inventory/1379376/4 "2023-11-11T10:02:56Z")

</div>

if the item is in your inventory then the inventory has a reference to it so it wont be garbage collected right?

i prefer objects over structs for inventorys, much more modular, in saying that you still need a struct for saving/loading

---

<div class="post-metadata">

**Author:** ![spielefreak](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@spielefreak](https://forums.unrealengine.com/u/spielefreak)\
**Post date:** [November 11, 2023, 2:30pm UTC](https://forums.unrealengine.com/t/prevent-garbage-collection-for-item-in-inventory/1379376/5 "2023-11-11T14:30:45Z")

</div>

Well, I added the reference to the array and destroyed it after because I didn’t know how to do it otherwise. I got it working with the struct now tho.

Can you explain how I would do it with the reference? And what would be the benefits?

---

<div class="post-metadata">

**Author:** ![Auran131](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Auran131](https://forums.unrealengine.com/u/Auran131)\
**Post date:** [November 12, 2023, 3:33am UTC](https://forums.unrealengine.com/t/prevent-garbage-collection-for-item-in-inventory/1379376/6 "2023-11-12T03:33:59Z")

</div>

I was referring to Uobjects I think you’re using actors. if you wanted to use actors you could just hide/disable collision instead of destroying it but that could be expensive if you have 100s of items.

the advantage of objects is they’re modular, you can have a weapon item and an armor item with difference variables for instance, you can also run code on objects. so say a weapon has ammo i handle reloading on the object itself

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [December 12, 2023, 3:34am UTC](https://forums.unrealengine.com/t/prevent-garbage-collection-for-item-in-inventory/1379376/7 "2023-12-12T03:34:30Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
