Abbreviate Large Numbers in GameMaker

How to abbreviate large numbers in GameMaker

There are lots of games these days where the player’s score becomes a gigantic number. Look at Balatro or our very own upcoming puzzle game Nildigo.

If you’re adding obscenely high numbers to your own game, then you’ll probably want to abbreviate them too. We’re sharing two scripts today which show you how to abbreviate large numbers in GameMaker.

Abbreviate Large Numbers in GameMaker - the money in Nildigo gets abbreviated to a power of 10 as it increases.
The money in Nildigo gets abbreviated to a power of 10 as it increases.

Abbreviate using letters (or words)

First, a script to abbreviate your large numbers using letters. For example, the number 1000000 might be shortened to 1m.

/// @description string_abbrev_real(real,decimals)
/// @param real
/// @param decimals
/// Returns argument0 abbreviated with a k, m etc
/// eg 1344003 might become 1.34m
/// Works up to quadrillions
/// Created by Allison J. James
function string_abbreviate_letter(argument0, argument1) {

	var Rea=floor(real(argument0)); 
	var Dec=floor(real(argument1));
	var Add=""; 
	var Div=1; 
	var Eva="";

	if( Rea>=1000000000000000 )
	{
	    Add="q";
	    Div=power(10,15);
	}
	else if( Rea>=1000000000000 )
	{
	    Add="t";
	    Div=power(10,12);
	}
	else if( Rea>=1000000000 )
	{
	    Add="b";
	    Div=power(10,9);
	}
	else if( Rea>=1000000 )
	{
	    Add="m";
	    Div=power(10,6);
	}
	else if( Rea>=1000 )
	{
	    Add="k";
	    Div=power(10,3);
		
	}

	Rea/=Div;
	Eva=string_format(Rea,string_length(string(Rea div 1)),Dec);

	return string(Eva)+Add; 

}

The script will take numbers up to 1 quadrillion, and then every number higher than that will simply be abbreviated in multiples of quadrillions. However, it wouldn’t be hard to alter the script to accept higher numbers. You could also swap the letters for whole words, for example: “billion” instead of simply “b“.

Here’s a screenshot of the script in action:

As you can see, the script also allows for decimal spaces. But what if you want powers of 10, like our game Nildigo?

Abbreviate using powers of 10

Using powers of 10 probably has less mass appeal for your audience, as a lot of casual players may not like things that remind them of maths class. However, it does allow for a near-infinite shortening of number strings. You don’t need to keep adding letters to the script, because it will always shorten numbers even as they get bigger and bigger.

Use this script to shorten numbers like 1000000 to 1 x 10⁶.

/// @description string_abbreviate_pot(_number,_threshold,_decimals)
/// @param _number
/// @param _threshold
/// @param _decimals
/// Returns the _number with its power of 10 if it's over _threshold
/// _decimals specifies the number of decimal places to show before the power
/// eg 1200000 could become 1.2 x 10^6
/// Created by Daniel Johnston Menezes
function string_abbreviate_pot(_number,_threshold,_decimals) {
	
	var _pwr = log10(_number);
	
	if (_pwr > _threshold) {
	
		var _pwrFloor = floor(_pwr);
	
		return string_format(_number/power(10,_pwrFloor),1,_decimals)+" x 10^"+string(_pwrFloor);
	
	} else {
	
		return string(_number);
		
	}

}

This script can also show different numbers of decimals and will only take effect if the number entered is over the threshold value you supply.

Here’s a screenshot of the script in action:

You’ll probably want to break the script down into chunks and draw the “power” separately on screen so that it looks pretty, like in the screenshot of Nildigo above, rather than having “10^n” displayed.

Feel free to copy & paste either of the scripts above, or download our .yyz example project below:

Wishlist Nildigo on Steam

If our article helped you and you’re interested in playing Nildigo, please download the demo and wishlist the game on Steam!

Daniel

I make funky little games of all different kinds like Gyro Boss, Plunder Dungeons & Spellworm! Also making lots of fonts & free game assets.

Leave a Reply