Counting Array Contents

Get help using Construct 2

Post » Thu Jul 19, 2012 4:40 am

I have an array with the contents of 0,2,3,0,5,0. I would like to be able to count how many times a number exists in the array. So in this example, I'd like to be able to store in a variable the number 3 when testing how many times 0 exists within the array.

I'm searched the forums looking for an answer but am coming up empty or confused. I attempted the following without success:


MyArray > For each X element
    MyArray > Value at MyArray.CurX = 0   
       action: System > Set myVar to MyArray.PickedCount

I would think the first two lines would be okay for testing each array object to see if it's zero but I can't figure out the action to capture the count. Also, I didn't want to use an iterating variable to add to my counting variable because the For Each statement will run 40+ times a second..

Can someone help?

B
15
S
6
Posts: 17
Reputation: 1,927

Post » Thu Jul 19, 2012 4:56 am

What's wrong with using an iterating variable? For each X element, if MyArrayCur.X = 0, variable + 1. You can come up with alternatives, say dumping the array content to a text string and checking for how many 0's pop up, but that shouldn't be any different than updating a variable. It shouldn't make a heavy difference performance-wise.
B
19
S
5
G
1
Posts: 104
Reputation: 2,920

Post » Thu Jul 19, 2012 5:49 am

Also MyArray.PickedCount will only return 1 as you only have one instance of the array object.

The iterating variable is indeed a good way to go.
New to Construct ? Where to start

Image Image
Image Image

Please attach a capx to any help request or bug report !
Moderator
B
247
S
85
G
40
Posts: 6,999
Reputation: 57,793

Post » Thu Jul 19, 2012 4:23 pm

If you are worried about performance due to for call being run multiple times, you could have a separate dictionary object, a key exists for each integer in the array, the key's value is set to the number of times aforementioned key exists within the array. Then, whenever you make changes to the array such as adding or deleting an integer, update the corresponding key's value in the dictionary. Then, whenever you want to find the number of times any given integer exists within the array, you can just do a vey quick dictionary check instead of a 'for' call.

If you are doing lots and lots of lookups, this would probably be faster, however if you are constantly making lots of changes to the array, the performance gain may be negligible.cacotigon2012-07-19 16:25:00
B
30
S
6
G
8
Posts: 232
Reputation: 6,119

Post » Thu Jul 19, 2012 7:06 pm

I wasn't concerned with performance - I just can't get the iterating variable to work. Instead of giving me a count, it iterates forever. It keepings increasing the value in the variable indefiniately.

The array is holding text data which comes from the state of 6 sprites that are touched by the player. Initially, all array values are 0 but when the player touches the sprite, the number of the sprite replaces the 0. You start out with a display showing 0,0,0,0,0,0 but after you've touched them all it shows 1,2,3,4,5,6.

It's proving difficult to descrive I guess and the code is buried in a decent sized project right now, so I'll try to post a smaller example later if I can reproduce my problem in a capx.
B
15
S
6
Posts: 17
Reputation: 1,927

Post » Thu Jul 19, 2012 9:45 pm

You need to reset the variable to 0 before you count.
This is how I'd do it:

Global number zero_count=0

System: Every tick
---- System: Set zero_count to 0

Array: For each X element
Array: Current value = 0
---- System: Add 1 to zero_count
B
79
S
24
G
54
Posts: 4,743
Reputation: 40,749


Return to How do I....?

Who is online

Users browsing this forum: anty21ro, Magistross, mallorcaredes, PavleNikolic and 23 guests