Incrementing and Decrementing Values Like a Pro – How to Use The Min/Max Functions to Save Time


Save time and effort when updating your game variables, like player scores and health – by eliminating the need to check their minimum and maximum allowed values in another separate event. Do it all in just one simple maths function!


With any game that has the player trying to score points to advance in the game, or lose health or lives in trying to beat a level – you’re always going to deal with incrementing and clamping total scores, or keeping track of player health and lives and checking they don’t end up with negative values!

Within GB Studio, we use basic maths function events to keep track of these incrementing scores; and if a player loses a life or health value, keep track of how much is left before the player runs down to zero. To track a high score value and level of health, we would store these values in two separate variables.

Scoring extra points to add to the player score – we simply increment a given value onto the player’s existing score variable. Losing health points if a player runs into an enemy, or falls onto an obstacle in a scene – we reduce their health variable, usually we decrement it by 1.

An example of a HUD in a Game Boy game. It shows a Health Bar, Air Level, and Score.
Scuba Dave, Stuck in a Cave – showing the player’s health bar, remaining air, and their score.

Limiting Variables to set Minimum and Maximum Values

Included after these two game events, we tend to keep track of a maximum value (for scores) and minimum value (for health). We may have designed the game to track high scores up to a maximum of, for example – 9,999, for basic maths reasons, limitations in code, or for design restrictions when displaying current and/or saved high score values in a heads-up-display (HUD) or menu scenes.

An example of a large score display from Tetris for Game Boy.
Example of Tetris if the player score was not limited to five digits, e.g. 99,999.

And when the player reaches a health value of zero, they may lose a life or end the game, and can be sent back to the start of a level, or to the game over scene. If we don’t limit their health to a set base number, they may end up with a negative value which could cause bugs in scripts checking for the end of play, or graphical errors in health bars or numbers of lives.

An example of a variable overflowing into a negative from Super Mario Land.
Super Mario Land, with a Mario having -01 lives remaining.

Common Way To Update a Value and Check It’s Limits

Traditionally we set each event with two mathematical operations. In the case of increasing score, we would first increment the player score by a given points value, using [Variable Set To Value] using the value as an Expression $playerScore = $playerScore + $points.

An expression event in GB Studio.

Then we would add another event immediately after this [If Variable Compares With Value] to check the new value of variable $playerScore and if it is above a predefined value (e.g. 9999), we limit the variable to that value.

If $playerScore >= 9999
[Variable Set To Value] $playerScore = 9999

An "if expression" event in GB Studio.


No matter how many points the player now gets, when the additional points are added to their score, the second event checks their score value and clamps (or limits) it to the specified value (e.g. 9,999).

Similarly when losing health points, we would first decrement a player’s health variable by a given value (e.g. 1) [Variable Decrement By 1].

The decrement event from GB Studio.

Then we would add another variable compare event immediately after this [If Variable Compares With Value] to check the new value of variable $playerHealth and if it is below or equal to zero, we default the variable to zero.

If $playerHealth <= 0
[Variable Set To Value] $playerHealth = 0

An If Variable Value event being used to create a cap for our variable value.


This way the player will never end up with a health value in the negative range, or show that they have a negative number of lives available to play.

There is however a better way to update these variables and limit their values!

Using A Maths Expression Function in Just One Event

That’s two events per in-game update to adjust the player score or health. We can reduce these two events down to just one single mathematical expression event – using the min and max functions.

Their mathematical use can be seen in the table below:

FunctionResults / Use
min(a,b)Returns the lower value between a or b
max(a,b)Returns the higher value between a or b

To update the player’s score when adding new points, we replace the two events with just one event [Evaluate Math Expression] and use the min function. We set the variable to $playerScore, and in the Expression set min($playerScore+$points,9999), where 9,999 is the maximum value we want the score to be, and $playerScore+$points is the new score value. If $playerScore + $points is greater than 9,999, the lower value (i.e. 9,999) will be returned.

An example of the min function being used in an evaluate expression event.


Similarly for reducing player health to no less than zero, we replace the two events with just one event using the max function. We set the variable to $playerHealth, and in the Expression set max($playerHealth-1,0), where 0 is the lowest value we want the health to be, and $playerHealth - 1 is the new player health. If $playerHealth - 1 is less than or equal to 0, the higher value (i.e. 0) will be returned.

An example of the max function being used in an evaluate expression event.


We have now replaced two events per in-game update with just one maths function expression – tidying up our events flow, and reducing the possibility of introducing typos, mistakes or errors when assigning and checking variables in multiple different events and equations.

The use of min and max can be applied across all aspects of your game, from updating body armour values and gun ammo magazine counts, to keeping track of looping through menu pages and pause screen options lists. It’s a lot neater and quicker to write the update and check in one expression than to populate two separate events.

An example of tracking and limiting menu item value between a min and max value.
Example of tracking and limiting menu item value between a min and max value.

An example project that showcases these two approaches can be found at https://github.com/chunkysteveo/GBS-Central-Example-Min-Max where you can test both methods to see how each functions. Feel free to download this project and run it locally to see for yourself!

A screenshot from the example project, where you can test out the functions from today's article.
Liked it? Take a second to support GB Studio Central on Patreon!
Become a patron at Patreon!