How do I sum value of arreys

0 favourites
  • 13 posts
  • 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

  • For each X element

    add x to a variable (local or global)

    outside loop

    set text.text to variable or something

  • actually i want to count amount of stars in above linked tutorial

    i m new in construct 2 and not getting this point

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

  • here is the link

    i want to count total stars here

    https://www.dropbox.com/s/n9qq0r1s2rmlb ... .capx?dl=0

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

    Sytem add 1 to NumberStarLevels(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

  • seems complex

    can't understand

    i have to read this about many times to proper understand

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

  • thanks

    i'll try to grab it

    thanks again

  • 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

  • Here you go

    Did this quick.

    Used function, with local variables

    Stores Total stars in global variable totalstars... so just display it where you want. or look at it in debugger to see it in action.

  • thanks brother

    thank you very much it really gud feeling getting problem solved

    i was about from two weeks in this point

    thank you once again

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • No problem. Sorry I didn't give it too you straight off the bat

    Good you did a little practice.

    Reason it didn't work is cause you add 1 to totalvalue when you needed to add total of each current value and not 1

    I see I typed that too, so my bad, but I was doing it out of head - lol.

    Anyways, functions are great. Now learn how to pass info via param and that single function could possibly find total stars, stars for specific level, etc etc etc

    Functions are most powerful part of C2.

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