Download

A bit complicated questions about Inventory system by TArray and TMap

I need another help again… I have been googling Basic Inventory System, TMap, TArray

(Thanks to WCode, Rama and alvarofer0020… cannot remember others… sorry)

However… I face another problems. And my time is very short (the deadline is close by few days… few days!)

  1. I have a item class based on Actor, it has many members such as name, currency value and so on.

There are various items, and inheritance is not enough to make various types of items. Thus

I used actor component to contain its uniqueness.

For instance, I have a item called a Spear. Primary feature is an weapon. but it can be used as Ammo by a pipe gun.

I thought about making an another class (object) as “WeaponAndAmmo”, somehow it does not sounds right to me.

Thus I made separate component as Weapon Component, Ammo Component… is it better? if it sounds okay,

How do I access its component from Actor class in C++?

  1. In TArray, How do I find specific the object by searching the demanded value? As I commented above,

I have a class named Item and has FString member as its Name. But I want to find the item by a name in TArray Inventory.

The reason I use TArray for one of inventories… because even though these items (actually weapons) has same name, but each item has durability - thus each of them is unique. They don’t have Quantity value. But I am hesitating to use TMap as Weapon Inventory… but it does not sounds right to myself if I ever think about using weapon type inventory as TMap.

3.In TMap, I used UFUNCTION to access it, if I want to use it from BP. But is it possible to access every objects in TMap

by iteration? and if there is, which method is faster and safe? I might need examples… googling is not enough to get what I want.

  1. TMap to TArray, is it possible even with same object type? I might to create the temporary UI(or UMG) Inventory for displaying which items are available to use by TArray. However one of current inventory is TMap. I need to transfer from the TMap data to to TArray… is it even possible? even with same data type? For instance, I need put available Ammo and certain weapons from the current Inventories to UI Display Inventory (thus the player can choose item to use during battle). But Ammo Inventory is TMap… and UI Display Inventory is going to be TArray… if I start to program the battle system.

I know it sounds complicated. but I really have short time with limited knowledge about Unreal Engine and C++ than I thought.

Please… I need your help… Desperately.

about 4… i kinda solved it… another problem is… accessing the component in the Actor class in C++. Not sure how do I achieve it… is using Cast okay?

I posted about this in your other thread. You want to save iterating maps / arrays where possible.

thank you so much. i will check it out. the one with tick, right?

Ah sorry my mistake, I got you confused with this thread: https://forums.unrealengine.com/showthread.php?108084-Associate-Ammo-with-Weapon&p=516622#post516622

However, using the ENUM-like system I posted there for accessing TArrays you can also access specific data members, so long as each is unique. In your case you probably want to use a TMap (so long as you’re not searching through it each frame it’ll be fine), and if you need Blueprint access, write a function that fetches the data from C++

And yeah you can iterate TMaps, though the ‘Type’ is a TPair, not just a single data member. See here:

thx for reply, i will check it out!

For #2 you can use the IndexOfByPredicate function on the TArray.
This example assumes MyArray is a TArray of UItem, and UItem has an FString called Name:
int idx = MyArray.IndexOfByPredicate(=](const UItem& itm) {return itm.Name == *FString(“Johnny”); });

Once you have the index (idx) you just use MyArray[idx] to get the full UItem class.

wow after I fix TickComponent problem in Actor Component, I will try your answer. thank you so much