Counting Array Contents

0 favourites
  • 6 posts
From the Asset Store
An educational game for counting pictures. An easy to use template for developers to build larger games
  • 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?

  • 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.

  • 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.

  • 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.

  • 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.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)