Fix negative score

0 favourites
  • 4 posts
From the Asset Store
Create the right kind of drama with Film Scoring Tools sound library!
  • Hello all,

    I'm currently developing my first game with Construct 2, and I'm facing an issue. Well, not really an issue, but I understand I probably made a mistake while building my highscore feature.

    So, in my game, there is a Global variable: ScorePoints = 0.

    On my HUD, this is simple text that shows current player score.

    Player can earn points by collecting items, and killing ennemies.

    Player can loose points by being hurt, and dying.

    Each time these events happen, it add or remove points to my ScorePoints global variables.

    Everything was fine until I realized there was something wrong:

    [quote:3gfvu26v]At the beginning of the game, player score is 0. So, if he is being hurt, or die, at this time, he will lose points and its score is going to be a negative value.

    This might looks stupid, but I absolutely forgot this case... Now, I am wondering how should I build my player score to avoid this. Is this possible to avoid negative variable? I'm probably missing something.

  • In the event(s) that decrease(s) the value of ScorePoints, instead of using an action "Substract from global variable", prefer to use "set global variable value" and use the clamp() expression.

    clamp(ScorePoints - 1, 0, 999)

    Clamp is a system expression that limits the returning value between the min and max values provided.

    In the example it limits with minimum value 0 (so never negative) and max value "999". Adapt max according to the possible scores that can be achieved in your game.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • This is a pretty easy fix:

    When score < 0 set score to 0.[/code:3ig0vmm3]
    
    Or you could use Kyatric's solution. That's probably a better choice in the long run.
  • In the event(s) that decrease(s) the value of ScorePoints, instead of using an action "Substract from global variable", prefer to use "set global variable value" and use the clamp() expression.

    clamp(ScorePoints - 1, 0, 999)

    Clamp is a system expression that limits the returning value between the min and max values provided.

    In the example it limits with minimum value 0 (so never negative) and max value "999". Adapt max according to the possible scores that can be achieved in your game.

    Ok, I gave it a try and this is perfectly what I was looking for. Very easy to use, and relevant to what I need.

    Note that I used a constant Global Variable for the max. highscore.

    Thank you very much!

    This is a pretty easy fix:

    When score < 0 set score to 0.[/code:1v8cfctn]
    
    Or you could use Kyatric's solution. That's probably a better choice in the long run.
    

    Thanks, Goodfeet for your suggestion anyway.

    P.S.: S'ry for this late answer, but I work on my game aside my daily work... It is a side project, so I do it in my spare time. That's why... But very happy you answered fast, guys.

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