# How do I sum value of arreys

Get help using Construct 2

### » Fri Oct 16, 2015 10:09 am

In select level tutorial when we set array x to

max(2+stars,levelsat(currentlevel)

then if i want to sum all X values then how do i do it
B
86
S
48
G
49
Posts: 717
Reputation: 35,986

### » Fri Oct 16, 2015 10:23 am

For each X element
add x to a variable (local or global)

outside loop
set text.text to variable or something
You think you can do these things, but you can't, Nemo!
Just keep learning.
B
65
S
16
G
9
Posts: 1,429
Reputation: 12,753

### » Fri Oct 16, 2015 11:00 am

actually i want to count amount of stars in above linked tutorial
i m new in construct 2 and not getting this point
B
86
S
48
G
49
Posts: 717
Reputation: 35,986

### » Fri Oct 16, 2015 11:23 am

luckyrawatlucky wrote:actually i want to count amount of stars in above linked tutorial
i m new in construct 2 and not getting this point

If it's just a tutorial you are following then you would be best posting the .capx so users can quickly see the problem and offer help.
If your vision so exceeds your ability, then look to something closer.
Moderator
B
144
S
33
G
89
Posts: 5,903
Reputation: 62,554

### » Fri Oct 16, 2015 11:36 am

i want to count total stars here
https://www.dropbox.com/s/n9qq0r1s2rmlb ... .capx?dl=0
B
86
S
48
G
49
Posts: 717
Reputation: 35,986

### » Fri Oct 16, 2015 11:37 am

Amount of stars...

Okay found tutorial.
So he stores data as follows
finish level = 1
win = +1
star1 = +1
star2 = +1
star3 = +1
So if a player finishes level and wins and gets 3 stars x = 5
So you want to for each x element
you want to add a condition curent value > 2 [Don't want to count levels without stars]
System add 1 to TotalStars (variable)
outside loop
System Subtract Totalstars numberstarlevels*2 [because we want to remove the 2 in each]

The above isn't pretty. But I am trying to show you the logic.
Foreach x.
Condition that x > 2 (2 being player has won, but no stars)
We want to add all those up.
Now we want to subtract the finish 1 plus the winning 1 to get total stars

You can see this all clearly using the debugger
You think you can do these things, but you can't, Nemo!
Just keep learning.
B
65
S
16
G
9
Posts: 1,429
Reputation: 12,753

### » Fri Oct 16, 2015 11:50 am

seems complex
can't understand
B
86
S
48
G
49
Posts: 717
Reputation: 35,986

### » Fri Oct 16, 2015 12:00 pm

Foreach creates a loop that goes round and round for each x element it cycles through each line in the array.
Condition allows us to restrict amount of lines/loops to a specific rule
Our rule is x must be greater than 2

Why 2?

Because tutorial says if player attempts level he gets a 1, if he wins the level he gets a 2, if he wins and gets 1 star, he gets 3, wins and 2 stars, he gets 4 and if he wins and gets max stars he gets 5

Its just a number.
We know that 2 of the 5 is not stars so we want to subtract the 2 to get the remainder.

Examples
x = 3 player has 1 star 3-2=1
x = 4 player has 2 stars 4-2=1
x = 5 player has 3 stars 5-2=3

So we loop through each x element that is greater than 2
We add total x number to a total variable
we also add 1 to count number of times it cycles (this is number of x entries that have stars.

No we do the same. Total -2 = stars but I did it a litle different
Total - (numberlevelswithstars*2)
You think you can do these things, but you can't, Nemo!
Just keep learning.
B
65
S
16
G
9
Posts: 1,429
Reputation: 12,753

### » Fri Oct 16, 2015 12:14 pm

thanks
i'll try to grab it
thanks again
B
86
S
48
G
49
Posts: 717
Reputation: 35,986

### » Sat Oct 17, 2015 6:51 am

DUTOIT wrote:Foreach creates a loop that goes round and round for each x element it cycles through each line in the array.
Condition allows us to restrict amount of lines/loops to a specific rule
Our rule is x must be greater than 2

Why 2?

Because tutorial says if player attempts level he gets a 1, if he wins the level he gets a 2, if he wins and gets 1 star, he gets 3, wins and 2 stars, he gets 4 and if he wins and gets max stars he gets 5

Its just a number.
We know that 2 of the 5 is not stars so we want to subtract the 2 to get the remainder.

Examples
x = 3 player has 1 star 3-2=1
x = 4 player has 2 stars 4-2=1
x = 5 player has 3 stars 5-2=3

So we loop through each x element that is greater than 2
We add total x number to a total variable
we also add 1 to count number of times it cycles (this is number of x entries that have stars.

No we do the same. Total -2 = stars but I did it a litle different
Total - (numberlevelswithstars*2)

i have try this
but result is below

https://www.dropbox.com/s/ajsvwt1gxmhcd ... m.jpg?dl=0
B
86
S
48
G
49
Posts: 717
Reputation: 35,986

Next