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

**URL:** <https://forums.unrealengine.com/t/closed-solved-count-specific-items-within-an-array/459041>\
**Category:** Editor Scripting\
**Tags:** inventory, inventory-system, unreal-engine, Array, question, Blueprint\
**Created:** [December 2, 2019, 7:03am UTC](https://forums.unrealengine.com/t/closed-solved-count-specific-items-within-an-array/459041 "2019-12-02T07:03:07Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Milo\_911](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Milo\_911](https://forums.unrealengine.com/u/Milo_911)\
**Post date:** [December 2, 2019, 7:03am UTC](https://forums.unrealengine.com/t/closed-solved-count-specific-items-within-an-array/459041/1 "2019-12-02T07:03:07Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![DDemon](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@DDemon](https://forums.unrealengine.com/u/DDemon)\
**Post date:** [December 2, 2019, 7:17am UTC](https://forums.unrealengine.com/t/closed-solved-count-specific-items-within-an-array/459041/2 "2019-12-02T07:17:32Z")

</div>

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++).

---

<div class="post-metadata">

**Author:** ![Milo\_911](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Milo\_911](https://forums.unrealengine.com/u/Milo_911)\
**Post date:** [December 2, 2019, 7:32am UTC](https://forums.unrealengine.com/t/closed-solved-count-specific-items-within-an-array/459041/3 "2019-12-02T07:32:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [December 2, 2019, 10:44am UTC](https://forums.unrealengine.com/t/closed-solved-count-specific-items-within-an-array/459041/4 "2019-12-02T10:44:26Z")

</div>

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:

 ![293353-annotation-2019-12-02-104057.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/d/f/2/df2e5c91d2912517eb48736aedd6f291b65fe15b.png)

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:

 ![293354-annotation-2019-12-02-104647.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/5/a/7/5a7870f4bd8531cf4e1a6924250fe71d746f10e9.png)

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

---

<div class="post-metadata">

**Author:** ![DDemon](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@DDemon](https://forums.unrealengine.com/u/DDemon)\
**Post date:** [December 2, 2019, 1:15pm UTC](https://forums.unrealengine.com/t/closed-solved-count-specific-items-within-an-array/459041/5 "2019-12-02T13:15:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [December 2, 2019, 1:46pm UTC](https://forums.unrealengine.com/t/closed-solved-count-specific-items-within-an-array/459041/6 "2019-12-02T13:46:57Z")

</div>

Works only with Actors so it’s _somewhat_ limited.

---

<div class="post-metadata">

**Author:** ![Milo\_911](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Milo\_911](https://forums.unrealengine.com/u/Milo_911)\
**Post date:** [December 2, 2019, 4:11pm UTC](https://forums.unrealengine.com/t/closed-solved-count-specific-items-within-an-array/459041/7 "2019-12-02T16:11:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [December 2, 2019, 7:19pm UTC](https://forums.unrealengine.com/t/closed-solved-count-specific-items-within-an-array/459041/8 "2019-12-02T19:19:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Milo\_911](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Milo\_911](https://forums.unrealengine.com/u/Milo_911)\
**Post date:** [December 2, 2019, 7:22pm UTC](https://forums.unrealengine.com/t/closed-solved-count-specific-items-within-an-array/459041/9 "2019-12-02T19:22:40Z")

</div>

They’re structs!

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [December 2, 2019, 8:13pm UTC](https://forums.unrealengine.com/t/closed-solved-count-specific-items-within-an-array/459041/10 "2019-12-02T20:13:42Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Milo\_911](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Milo\_911](https://forums.unrealengine.com/u/Milo_911)\
**Post date:** [December 2, 2019, 8:23pm UTC](https://forums.unrealengine.com/t/closed-solved-count-specific-items-within-an-array/459041/11 "2019-12-02T20:23:16Z")

</div>

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…

 ![293374-capture.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/d/0/8/d080e16a7353233d7caed97bc77129078792322a.png)

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [December 2, 2019, 8:30pm UTC](https://forums.unrealengine.com/t/closed-solved-count-specific-items-within-an-array/459041/12 "2019-12-02T20:30:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Milo\_911](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Milo\_911](https://forums.unrealengine.com/u/Milo_911)\
**Post date:** [December 2, 2019, 8:35pm UTC](https://forums.unrealengine.com/t/closed-solved-count-specific-items-within-an-array/459041/13 "2019-12-02T20:35:43Z")

</div>

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

For anyone that might be curious of the final result:

 ![293375-capture.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/5/b/a/5ba41b3b59d6b111ccc739b9ff1d1e8abf07f2f4.png)

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [December 2, 2019, 8:52pm UTC](https://forums.unrealengine.com/t/closed-solved-count-specific-items-within-an-array/459041/14 "2019-12-02T20:52:12Z")

</div>

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 😉

Good luck with the rest!

---

<div class="post-metadata">

**Author:** ![Galvanicus](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Galvanicus](https://forums.unrealengine.com/u/Galvanicus)\
**Post date:** [August 23, 2021, 4:46pm UTC](https://forums.unrealengine.com/t/closed-solved-count-specific-items-within-an-array/459041/15 "2021-08-23T16:46:18Z")

</div>

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

 ![347053-countexample.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/4/4/644402bed66ad4ceb9fd6b5f2323608fca9b2919.png)

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.

---

<div class="post-metadata">

**Author:** ![BJGames](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/bjgames/32/879537_2.png) [@BJGames](https://forums.unrealengine.com/u/BJGames)\
**Post date:** [March 19, 2024, 2:04pm UTC](https://forums.unrealengine.com/t/closed-solved-count-specific-items-within-an-array/459041/16 "2024-03-19T14:04:50Z")

</div>

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!
