Problem with a variable, please help!

0 favourites
  • 12 posts
From the Asset Store
Game with complete Source-Code (Construct 3 / .c3p) + HTML5 Exported.
  • Hi there!

    First of all, I have really tried to figure this out on my own without any luck so therefor I ask you guys. In my latest game, I have a variable that starts at 200 and then subtract 1 each second. If you finish the level in 150 seconds then the variable is 50 (200 - 150) and then your score is 50. So the faster you finish the level the higher score you'll get.

    Example:

    First level finished in 150 seconds = 50 points.

    Second level finish in 100 seconds = 100 points + 50 points from the previous level.

    I can figure out how to achieve this with a new variable for each level but that seems like overkill, I'm sure there's an easier way that I haven't figured out yet.

    So, how can I achieve this without having 50 different variables (50 levels)?

    Thanks in advance,

    Tommy / tomologames.com

  • You would only need 2 global variables, a level score and a total score.

    When you finish level 1, level score = 50, total score = 50

    finish level 2, level score = 100, total score = total score + level score, so 100 + 50 = 150

    finish level 3, level score = 150, total score = total score + level score, so 150 + 150 = 300

    You just add the level score to total at the end of each level.

  • First of all, thank you for your reply. I do understand and know everything you said but the problem I have is if you play the same level again to get a better score.

    Level 1 - score 100 = total score is then 100

    Play level 1 again and score 90 = total score is then 190

    How do I check to see if the score you'll get the second time you play is higher than the first time you played with only these 2 variables?

    Thanks again

  • If you are designing it so it's a high score per level then yes it's a variable per level as you would need to show the high score for each level, right?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • You can use an Array, that holds the current (or best) level score. Adding all of the values in the array gives the total score.

  • First of all I would like to thank you, it's very kind of you to take your time to help me out. I have never used "Array" though but after reading your comment I have tried to figure out how to use it. Ultimately, all I want to show after you have finished a level is:

    Score = X

    Total Score = X

    Is that possible to achieve using "Array" or will it show the individual scores of all levels? It would mean the world to me if you could explain further how I can achieve this. I know it's annoying when people ask first without even trying for themselves to begin with but I have tried it on my own without any luck.

  • See if this tutorial helps:

    scirra.com/tutorials/307/arrays-for-beginners

    Essentially you index into the array per level. You say you have 50 levels, so you need a one dimensional array of size 50. You use Array.SetAt(Level-1, score) to save the level score, and use Array.At(Level-1) to get that level's score. Looping through all values and adding them up gives the total score.

  • So now I've tried to figure this out but if I do what you told me to do the level score is saved every time, even if it's lower than the first time.

    I mean, if I play level 1 and score 150 and play the same level again and score 100, then the score 100 is saved. Or maybe it's just me doing something wrong?

    Thanks again

    Edit: I did it like this and it seems to work.

    System: LayoutName = "Level1"

    Array Value At 0 < TimeVar - Array Set Value at 0 to TimeVar

    TimeVar is the variable for the score that starts at 200 on each level.

    Edit again: I couldn't figure out how to add all the values up with the "For each X elements" so I just created a global variable called TotalScore and did this:

    System: Set TotalScore to Array.At(0)+Array.At(1)+Array.At(2) and so on. It may not be the best solution but it worked :)

  • Edit again: I couldn't figure out how to add all the values up with the "For each X elements" so I just created a global variable called TotalScore and did this:

    System: Set TotalScore to Array.At(0)+Array.At(1)+Array.At(2) and so on. It may not be the best solution but it worked :)

    You can use (Array For Each X Element):>>> Action Add To "TotalScore" >>>>> Array.CurValue This will Loop and add the current Values for all X on the Array

    An alternative you could use the "For" Loop:

  • Thank you so much for your help and also for including print screens, it's always very helpful :)

    I get a very weird result though and I have no idea why. If I play Level 1 and score 150 and then play the same level again and score 150, the total score says 300. I save everything with LocalStorage and here's the weird part. If I close the game and open it up again the total score no longer says 300, it's 150. Why do you think it shows the "correct" total score when I close the game but not as long as I'm still "in" the game?

    Thanks again Boss

  • Thank you so much for your help and also for including print screens, it's always very helpful :)

    I get a very weird result though and I have no idea why. If I play Level 1 and score 150 and then play the same level again and score 150, the total score says 300. I save everything with LocalStorage and here's the weird part. If I close the game and open it up again the total score no longer says 300, it's 150. Why do you think it shows the "correct" total score when I close the game but not as long as I'm still "in" the game?

    Thanks again Boss

    No Problem, Glad it helped

    For the score are you using a Global variable? because the Global they don't reset as longs you are on the Game even if you change Levels, so if you added to TotalScore 150 and played again and you ad another 150 will = 300

    If you need a new Fresh Total Score every Time you Loop the Array then before the loop set (TotalScore = 0)

    or

    an alternative make the "TotalScore" as Local Variable and at the end of the Loop set txt to TotalScore, the Local Variables are good for this example they reset each Thick you don't need to set (TotalScore = 0) at the start of the Loop it resets automatically

  • I'm such an idiot, I knew that about global variables but I wasn't thinking straight haha. I have now added "Set TotalScore to 0" before "For each X element" and now everything works like a charm :) Your solution is awesome, a thousand thanks to you again :)

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