Deduct score without going below 0

Hi, my player collects coins but upon impact with an enemy their coins get deducted by 1. My problem is the coin variable goes below zero. For example, the player has 0 coins, on impact with the enemy it becomes -1. How can I prevent and fix this? Thanks :slight_smile:

Hi, there are definitely many ways to do that. One option is to compare the value of your variable and if it is equal or below 0 just set it to 0.

It looks a little bit ugly and there is probably a better way to do it, but it works.

2 Likes

You could tweak what @Drona recommended and just have “The variable Coins is > 0” be a condition along with “Player is in collision with Enemy” for the action of “Subtract 1 from variable Coins”. That way, if the Player impacts with an Enemy it will only deduct a coin of the variable is greater than zero.

Conditions:
Player is in collision with Enemy
The variable Coins is > 0

Action:
Subtract 1 from variable Coins

1 Like

All of these are viable options, although my personal recommendation would just be to modify your event that changes the coin total.

Instead of doing “subtract 1” when the player is hit, do “set to clamp(Variable(variablenamehere)-1,0,9999999)”

This will mean that it will set the new value to current value-1, with a MINIMUM of 0 (it’ll never go past 0), or a maximum of 9999999 (or whatever number you’d like here).

No new events needed.

6 Likes