[Closed] [SOLVED] Count specific items within an array?

I’m trying to figure out how to get the total amount of potions are in my inventory array, but I’m not sure how!
Is there an array node that i can use to do this?

No, you have to do a foreach with a count, example: foreach(on the array)->branch->cast to potion->success->count = count + 1(or count++).

Hmm… I’m sorry, I’m not sure I understand. Do you have a blueprint example?

You can get the length of the array (very bottom of the pic) if the inventory has just potions; and this is roughly what DDemon meant:

A failed cast means the item in the array was not a potion. There are better, yet more involved methods of filtering but perhaps the above is enough here.


See if you can get this to work:

Whether it works or not depends on what your potions are, assuming here they inherit from Actor.

Hmm didn’t know there is a filter for arrays neat

Works only with Actors so it’s somewhat limited.

Dang, this would’ve been perfect! Unfortunately it isnt an actor array):

What are you potions then? Structs, components, widgets?

They’re structs!

That makes it quite easy then, no casting required; loop through the structs and check what’s what. The item struct must have an identificator of sorts, surely. An enumerator perhaps? When the loop runs into a potion, increase the counter, as in the example above.

How do you normally tell one item from another?

Yep, I have an enumerator to tell what is what. This is what comes to mind when I do what you said, but im sure im doing it incorrectly…

That did it!! Thanks so much, it works perfectly!

For anyone that might be curious of the final result:

Looks fine, just remember to Set the Potions on Hand to 0 before the loop. Otherwise the next time you count, it will add to the last result.

A couple of purely optional things you could do:

  • no need to Set the variable after the ++ node again, it sets the value by reference already (note the diamond shaped icon)
  • instead of Sequence, you can use the Completed pin of the For Each Loop node and create the widget there
  • rather than setting the text variable after creating the widget, you can flag that var as Exposed on Spawn & Instance Editable (inside the widget) - the var will then show up as an extra pin on the Create Widget node (you may need to right click and refresh the node after doing the above) - this can be actually useful if you want to prevent a potential widget jiggle as it tries to readjust its desired size after it has been added to the screen and already visible.

None of the above alter functionality but you end up with 30% less wires, a personal pet peeve :wink:

Good luck with the rest!

I am posting this answer because this was the first hit on Google.
This is for ANY array, not specific to actors or whatnot:

The example shows an enum and basically, a foreach loop that checks if an object exists on a resulting map.
If it does not, it is added with a count of 1. If it does exist, the value is updated.

I know most folks don’t use maps that often but this is a great example where maps shine.

2 Likes

I’m working on a texas holdem algorithm for checking the results, I had just started using maps and finding this has made them make so much sense AND this method is giving me the results i wanted. thankyou good sir!