How do I count the number of times a result shows in an arra

Hello! I’m pretty new to GDevelop, but not new to coding. Just having a little trouble wrapping my mind around the condition/action thingy.

I’ve got a simple array of numbers, and a few text items. I want each text instance (which has a variable to distinguish themselves) to show the number of times a certain number shows up in the array.

For example, if my array is:
ColorCode = {1,1,3,4,1,2,4,1}

Then I want the text instances to represent:
TextItem (where textID=1) = 4
TextItem (where textID=2) = 1
TextItem (where textID=3) = 1
TextItem (where textID=4) = 2

I was able to use the child count function and almost figured it out, but the number kept increasing because of the game loop. I couldn’t figure out how to make the text value constantly update without the counting remaining constant.

If I was “coding” this, it would look something like:

For x = 1 to ColorCount.count do
If ColorCount[ x ] = 1 then
text1 = text1 + 1
Elseif ColorCount[ x ] = 2 then
text2 = text2 + 1
……. Etc

Thanks in advance for helping out a newbie!

Use a structure to keep track of the instance tally:

image


Then loop through each array element, and use element value as a key in the tally structure (if it doesn’t exist, GDevelop will add it automatically):




Now Tally has 4 keys, each one with the number of times that key occurred in the array:

image

To get the values out of the structure and into each of the text boxes:

1 Like