Fix negative score

Get help using Construct 2

Post » Sun Dec 06, 2015 1:56 pm

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:

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.
Play Bikosaur open alpha: http://www.bikosaur.com/alpha
B
12
S
2
Posts: 30
Reputation: 829

Post » Sun Dec 06, 2015 3:42 pm

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.
New to Construct ? Where to start

Image Image

Image Image

Please attach a capx to any help request or bug report !
Moderator
B
280
S
104
G
86
Posts: 7,313
Reputation: 84,356

Post » Sun Dec 06, 2015 3:53 pm

This is a pretty easy fix:
Code: Select all
When score < 0 set score to 0.


Or you could use Kyatric's solution. That's probably a better choice in the long run.
Image
B
13
S
2
G
1
Posts: 60
Reputation: 1,554

Post » Fri Dec 18, 2015 12:37 pm

Kyatric wrote: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!

goodfeet wrote:This is a pretty easy fix:
Code: Select all
When score < 0 set score to 0.


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. :D
Play Bikosaur open alpha: http://www.bikosaur.com/alpha
B
12
S
2
Posts: 30
Reputation: 829


Return to How do I....?

Who is online

Users browsing this forum: mr32D, SoldjahBoy and 7 guests