How do I Sum the Values of an Array Column?

1 favourites
  • 3 posts
From the Asset Store
Supports 1D, 2D, 3D arrays. Import and export arrays in JSON format
  • Hey guys;

    Do you know how can I do this? I have an array with one column changing it's values often; I must store the sum of the values in this column and store them in a variable.

    To explain further, I have a layout with some sprites for the player to select, each sprite has a price. When one sprite is selected I upload it's price to an Array. So I do: For each Sprite, if is selected, Array: Set value at (4, loopindex, 0) to Sprite.price. So I fill one column of the Array with the values of the selected sprites, and when a sprite is not selected I do Array: Set value at (4, loopindex, 0) to 0, letting the 4th column of the array filled only with the values of the selected sprites.

    What I need to do is show in the screen the sum of this array column, and this is being tricky. Here's a screenshot of my code:

  • If you need to display the sum of all the Prices on the Moment you adding the Prices into an Array you can just create a Local Variable = Total

    Then while you adding the prices to the Array in that loop add another action add to Var "Total" the sprite.Price

    Then at the end of the loop set Txt to Total this will show the Sum of all the Prices that you added into the Array

    =====================

    But if you need to retrieve the Sum from the Array Directly while not adding Prices you can use the "For" Loop & a Local Variable: Total

    For "Y"

    From :0

    To Array.Height

    Action: add to Var "Total" >>>>> Array.At(4, loopindex("Y"))

    Set Txt to "Total"

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • tarek2 thank you so much! Here's what I did and its working wonders :)

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