Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Use for each loop to count amount of times a string appears in an array?

I am currently trying to make an "store" full of items which give the player different stats. such as health etc. I have done this by making the blueprints pull their stats based off if an item is in their "Inventory" array. The problem is if they get the same item twice. I would like to be able to count the amount of times and item is in their inventory so I can give them the stats required. Such as if you get for example - 2 10hp items with the same name it counts them separatley as right now I have it with a true false branch with the Booleanalt text being a "contains" within an array.

Product Version: UE 4.21
blueprint.png (296.9 kB)
more ▼

asked Nov 29 '18 at 03:29 PM in Using UE4

avatar image

1 1 1

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

Just create a counter variable, set it to 0 in the beginning, go through the Inventory with a ForEachLoop, and if Item == "Skean", then increase this variable by 1. After completing, you'll have the number of times the item occur in your array.

more ▼

answered Nov 29 '18 at 04:18 PM

avatar image

1.1k 4 5 7

avatar image zafner Nov 29 '18 at 09:17 PM

I did that but now it caused a new problem. The goal was to count the total of items then multiply it by the damage so I can add that number to the total damage. It should add 20 damage every time I buy the item but what ends up happening is it goes from 20 - 60- 100 because every time I trigger it; it loops and counts the amount currently in the inventory + what it has already added. alt text

blueprint.png (931.1 kB)
avatar image KristofMorva Dec 01 '18 at 03:44 PM

But your ForEachLoop makes no sense now. If it contains Skean, then you loop through the whole array and increase Skean in every iteration, so that you add the length of the array to Skean even if the array only contains it one time. I'd suggest first going through some basic tutorials which teach you the usage of variables and loops.

Anyways, make sure to set your variable to 0 before looping, so that the new increments won't be added to your previous value.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question