Like many people, I’ve been building an inventory system. Like many people (for what I’ve seen), I have an issue trying to sort my inventory by ascending (or descending name) name.
So far, this is what I got, which used to work fine:
But as soon as I have duplicated names, it falls appart.
For instance, if I have two “sword” with different durability, the first sword will be added to the map under the key “sword”.
Second “sword” coming with a different durability, it will also be added to the map under the key “sword”, which is already present in the map, so it will erase the first “sword” occurrence (and its durability). And then, instead of having two “sword”, I end up by having just one occurrence in the map.
But I don’t see what else I could do to sort an inventory. Any idea?
When you build the array of names try using an append node with 3 pins, plug name from struct in pin 1, type “.” in the middle input, and plug index from the loop in 3. Add this string to a string array called “NameAndIndex” or some such. Should be able to use an array sort node on this to make it alphabetical or whatevs. Then when you loop thru array in widget to render text and make use buttons, use the split string node,with “.” as the delimiter, then plug left output into set text and convert right output to int and feed that into your item index to use function.
To always have eqipped listed first, do it twice with a second array called EquippedNameAndIndex or something, and widget loop thru equipped array first, then the other array when first loop completes.
Or do it in c++ since there’s a native struct sorting function you can use there,
Hey, thanks for replying. Your solution should work indeed.
I also got this feedback:
But at the end of the day, I took a little break and thought about this. And I believe my architecture was wrong.
I could use the Uobject inside my struct to get the name, but this was actually meant to be the displayed name, inside the inventory, and not a way of “tagging” an item in the array of structures.
I added an GUID inside the structure and now, every item/stack has its own GUID. If the item stack has to split because one item is now different (condition for instance), then this newly created item/icon/struct has a new GUID. I had to change a bunch of stuff upon equiping, droping, unequipping actions, but I think it’s better.
And now I can use the map I used in my screenshot to know which GUID == which structure.
And if I want to display the name of the item in the inventory, I can still get the struct → get the Uobject → get the item name and here we are.
I think that’s more or less how Bethesda does it now in fallout4/skyrim only they call guid a formID there because creation engine is kinda extra. I thought about something like that, but just figured saving the array index into string as basically a pseudo-map might be easier. Using guid is also great because it helps prevent duplicates when moving stuff around real fast.