# Best way to code an inventory-typed system?

**URL:** <https://forums.unrealengine.com/t/best-way-to-code-an-inventory-typed-system/70057>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [July 27, 2016, 7:50pm UTC](https://forums.unrealengine.com/t/best-way-to-code-an-inventory-typed-system/70057 "2016-07-27T19:50:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_abd980be](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_abd980be](https://forums.unrealengine.com/u/anonymous_user_abd980be)\
**Post date:** [July 27, 2016, 7:50pm UTC](https://forums.unrealengine.com/t/best-way-to-code-an-inventory-typed-system/70057/1 "2016-07-27T19:50:19Z")

</div>

So I learned how to use TMaps to create an inventory, but it was a much simpler type of inventory.

Basically it was like this:

TMap\<FString, int32\> Inventory;  
TMap\<FString, UTexture2D\> Icon;

And I combined these TMaps to create a grid like system like so: FString name, int32 quantity, UTexture icon;

However, now I need to store more complex items in my inventory. I need to store variables such as name, cost, texture, mesh, and possibly other stuff. I basically need to put a tmap in a tmap in another tmap, and I don’t think that’s possible.

So, what is the best way to code an inventory system, like let’s say starcraft 2?

---

<div class="post-metadata">

**Author:** ![Young\_Wolf](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Young\_Wolf](https://forums.unrealengine.com/u/Young_Wolf)\
**Post date:** [July 27, 2016, 10:19pm UTC](https://forums.unrealengine.com/t/best-way-to-code-an-inventory-typed-system/70057/2 "2016-07-27T22:19:52Z")

</div>

Instead of making several TMaps why not make a struct or class (Actor) of Items ?  
Class Actor named Item which will hold Mesh, Texture, Cost, Name, Durability and so on.

Then have a TMap\<FString, Item\> Inventory

It’s way I approach in mine, tho, I use TArray

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_abd980be](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_abd980be](https://forums.unrealengine.com/u/anonymous_user_abd980be)\
**Post date:** [July 27, 2016, 10:34pm UTC](https://forums.unrealengine.com/t/best-way-to-code-an-inventory-typed-system/70057/3 "2016-07-27T22:34:36Z")

</div>

Instead of making several TMaps why not make a struct or class (Actor) of Items ?  
Class Actor named Item which will hold Mesh, Texture, Cost, Name, Durability and so on.

Then have a TMap\<FString, Item\> Inventory

It’s way I approach in mine, tho, I use TArray  
[/QUOTE]

Well to be honest, I don’t know why. The book I’m using to learn UE4 says Arrays can be slower than TMaps, and I don’t want to make a habit of using methods that are inefficient.

---

<div class="post-metadata">

**Author:** ![BrUnO\_XaVIeR](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/bruno_xavier/32/1110230_2.png) [@BrUnO\_XaVIeR](https://forums.unrealengine.com/u/BrUnO_XaVIeR)\
**Post date:** [July 27, 2016, 10:38pm UTC](https://forums.unrealengine.com/t/best-way-to-code-an-inventory-typed-system/70057/4 "2016-07-27T22:38:36Z")

</div>

Learn about UDataAsset… You won’t regret it.

---

<div class="post-metadata">

**Author:** ![Young\_Wolf](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Young\_Wolf](https://forums.unrealengine.com/u/Young_Wolf)\
**Post date:** [July 28, 2016, 6:40pm UTC](https://forums.unrealengine.com/t/best-way-to-code-an-inventory-typed-system/70057/5 "2016-07-28T18:40:40Z")

</div>

Dictionaries are generally faster than arrays because you are mapping values to a key and searching in it is always a constant if my memory does not fail me. But dictionaries are not always the best solution. For example looping through it is harder. Another bad thing is, you can get collisions if not implemented properly.

Anyway both are cool to use.

---

<div class="post-metadata">

**Author:** ![brianfrankenstein](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@brianfrankenstein](https://forums.unrealengine.com/u/brianfrankenstein)\
**Post date:** [July 28, 2016, 7:07pm UTC](https://forums.unrealengine.com/t/best-way-to-code-an-inventory-typed-system/70057/6 "2016-07-28T19:07:00Z")

</div>

TMaps might be faster, but practically speaking unless your TArray is gigantic or you are accessing many many times per tick, its generally insignificant. You can also keep it sorted like a BinaryHeap and use binary search.

I generally go with TArrays because they can be UPROPERTYs, which can save you from some nasty memory bugs and make debugging in the editor really easy.

The general rule in C++ is not to optimize prematurely if it can cost you future headaches, let the profiler guide you if and when you get CPU bound.
