r/gamemaker • • 18d ago

Resolved Point counter display 1K instead of 1000, without changing the real amount of points

I have a point counter and I want to shorten long numbers to 1K, 1T, etc. I've gotten some tips on that, but they involved dividing the real number by for example 1000, and then adding a K to the leftover number.

But I don't want the actual points to be changed, since they are being used to buy upgrades, I only want the numbers displayed to be shortened.

4 Upvotes

11 comments sorted by

9

u/nickelangelo2009 Custom 18d ago

easy, get a second variable to keep track of the display string without changing the value of the actual counter variable

1

u/SkullnSkele 18d ago edited 18d ago

Ah, cool, thank you!

So for example, I have in my counter currently

draw_text(x+40, y-15, "Dad Points: " + string(global.dad_points))

So I would make a second variable, thats for example named global.point_display, and have that one have something like

global.point_display = string( floor( counter / 1000 ) ) + "T";

and display that instead of the string i have now

I'm not sure on how to make it have K for 1000, M for million , etc. My only idea is doing an if else, but I'm not sure how to do that right

3

u/Joshthedruid2 18d ago

If else is really easy and probably the right answer, you just have to stack your statements in the right order.

Ex, if (var>1,000,000), numberAbbreviation = "M", else if (var>1,000)...

Something like that starting at the highest number and working down

2

u/nickelangelo2009 Custom 18d ago

yup, you're on the right track. there's never really a need to be stingy with more variables when you need em

3

u/balmut 18d ago edited 18d ago

Tip, if you want to make it a little more juicy don't set the second variable to the first, instead put something like this in the step:

if fakeValue < realValue{fakeValue++;}

and it'll count up instead!
Extra juicy points if you make the draw_text wiggle about as it counts up!

1

u/AmnesiA_sc 17d ago edited 17d ago
suffixes = ["","k","M","B","T","Q"];

/**
 * getScoreString( val)
 * @context self
 * @pure
 * @param {Real}    _val    Value to stringify
 * @description Take a value and return a shortened version with a suffix
 * @returns {String}
 */
getScoreString = function( _val){
    var q;
    for( var i = array_length( suffixes) - 1; i >= 0; --i){
        q = power( 10, i*3);
        if( _val >= q){
            return string( _val div q) + suffixes[i];
        }
    }
}

This will give you an integer followed by the appropriate suffix. For example, 11,200 (without commas) will return "11k". 54,165,576,456,549 will return "54T".

As an example of its use:

draw_text( x, y, getScoreString( global.dad_points));

Edit: If you need to also use negative values:

/**
 * getScoreString( val)
 * @context self
 * @pure
 * @param {Real}    _val    Value to stringify
 * @description Take a value and return a shortened version with a suffix
 * @returns {String}
 */
getScoreString = function( _val){
    var q,
        neg = _val < 0;
    _val = abs( _val);
    for( var i = array_length( suffixes) - 1; i >= 0; --i){
        q = power( 10, i*3);
        if( _val >= q){
            return $"{neg ? "-" : ""}{string( _val div q)}{suffixes[i]}";
        }
    }
}

1

u/SkullnSkele 18d ago

OH i just figured it out I think, or at least it seems to work, i did

var global.display_counter = "???";

if(global.dad_points >= 1000000000){

display_counter = string(floor(global.dad_points/1000000000)) + "G"

}

else if(global.dad_points >= 1000000){

global.display_counter = string(floor(global.dad_points/1000000)) + "M"

}

else if(global.dad_points >= 1000){

global.display_counter = string(floor(global.dad_points/1000)) + "K"

}

else{

global.display_counter = string(global.dad_points)

}

I just don't know how to make it so, that it shows up to 2 decimal points

1

u/KantiLordOfFire 17d ago

Could do something with

var _scale = floor((string_length(string(global.dad_points)) - 1) / 3 );

Then maybe

display_counter = string_format(global.dad_points / power(1000, _scale), 1, 2);

Then use IDK

var _suffix [" ", "K", "M", "B", "T"];

To finish it off with

draw_text(string(display_counter) + _suffix[_scale]);

IDK, could work maybe.

2

u/Krilantis 18d ago

Keep the score numeric and build a separate label: choose a scale and suffix by testing billions, millions, then thousands, and divide only for the displayed value. `string_format(shown, 1, 2)` gives exactly two decimal places; below 1000 you can use `string(global.dad_points)`. Declare a local `var display_counter` and then assign `global.display_counter = display_counter`, rather than using `var global.display_counter`.

1

u/SkullnSkele 18d ago

thank you!

0

u/Somnati 18d ago

i created a script that crunches numbers above 999 like that...if you add me on discord i can give it to you and show you how to use it