• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

[GM8.1] Translating a score into sprites

S

Spongyoshi

Guest
Okay so I tried my hands on the "draw" event because I wanted a score to show up but wasn't a fan of just having it in black arial so I wanted to have custom sprites to display the score instead!
However, what I through was a normal task is more complicated than I thought! I hardly can find help online on that matter but I could find a video to assist me:
(And I could check how functions works in GM as a bonus ^^)
But sadly, it didn't worked as the sprite just looped thought it's sub-images instead of just showing my score!
If you have any better way or can find any error in my code, please let me know as soon as possible, any help is appreciated!
Code:
drawNumb(view_xview + 25, view_yview + 25, string(Mario_SkiAcrobatique.MesPoints.value), chiffres)
Code:
var _sprite, _fontLayout, _x, _y, _string, i, _offsetX, _offsetY;

_x = argument0;
_y = argument1;
_string = argument2;
_sprite = argument3;

_fontLayout = "0123456789/";

if(!variable_global_exists("_charMap"))
{
    for(i = 0; i <= 255; i+=1)
        global._charMap[i] = -1;
    
    for(i = 1; i <= string_length(_fontLayout); i+=1)
        global._charMap[ord(string_char_at(_fontLayout, i))] = i;
}

_offsetX = 0;
_offsetY = 0;
for(i = 1; i <= string_length(_string); i+=1)
{
    var c;
    c = ord(string_char_at(_string, i));
    
    if(global.charMap[c] != -1)
        draw_sprite(_sprite, global._charMap[c]-1, _x + _offsetX, _y + _offsetY);
    _offsetX += sprite_get_width(_sprite);
}
 

TheouAegis

Member
Wait, what all are you trying to draw with us right? Just the numbers? All you need to do if you're trying to just draw numbers is make a Sprite with only the numbers in order starting from 0 up to 9. Hold the score in a temporary variable. Take that variable mod 10, that's the image index. Then set that variable to itself div 10. (div, not /). Do this for however long the score will be.
 
S

Spongyoshi

Guest
Yup, just the numbers!
Your idea sounds great but it also confuses me a bit so tell me if this is what you meant.
1) Put the score in a temporary variable, that I got!
2) Then, I do something like "tempscore = tempscore mod 10"?
3) After, do I have to print this with "image index = tempscore"? This is the first number to the left, right?
4) And afterward, if "original score" is bigger than 9, I redo that procedure but with "tempscore = tempscore div 10" and this will return the following number?
5) And I can check if it's bigger than 99, 999, 9999, etc by re-doing step 4, did I got this correctly?
If so, tell me where I'm wrong! I've learned about modulos in programming school but I can't say I was an expert at it...
 

TheouAegis

Member
The tempscore mod 10 would be in a different variable or just used in the draw_sprite calls.

draw_sprite(spr_scorefont, tempscore mod 10, xcoordinates, ycoordinates)

The div partwould affect tempscore directly.

tempscore = tempscore div 10;

That would be after the draw_sprite() call.

Do you want leading zeroes? So it would appear as 0000815, for example?

So say you set your score limit to 99999999, that's 8 digits. Your loop would run 8 times.

Code:
var i, w, tempscore;
tempscore = score;
w = sprite_get_width(spr_scorefont);
for(i=8; i; i-=1)
{
    draw_sprite(spr_scorefont, tempscore mod 10, view_xview + i * w, view_yview);
    tempscore = tempscore div 10;
}
I'd set a limit like that rather than allowing an arbitrary score length.
 
T

Taddio

Guest
The tempscore mod 10 would be in a different variable or just used in the draw_sprite calls.

draw_sprite(spr_scorefont, tempscore mod 10, xcoordinates, ycoordinates)

The div partwould affect tempscore directly.

tempscore = tempscore div 10;

That would be after the draw_sprite() call.

Do you want leading zeroes? So it would appear as 0000815, for example?

So say you set your score limit to 99999999, that's 8 digits. Your loop would run 8 times.

Code:
var i, w, tempscore;
tempscore = score;
w = sprite_get_width(spr_scorefont);
for(i=8; i; i-=1)
{
    draw_sprite(spr_scorefont, tempscore mod 10, view_xview + i * w, view_yview);
    tempscore = tempscore div 10;
}
I'd set a limit like that rather than allowing an arbitrary score length.
Would that work with a particle system as well, with the same subimage principle? Kind of "writing with particles" if I may say
 

TheouAegis

Member
Possibly. I never use particles, let alone Sprite particles. So I'm not sure how the system specifically works.

Edit: had a quick glance at the manual, it looks like the answer is no.
 
Last edited:
T

Taddio

Guest
Possibly. I never use particles, let alone Sprite particles. So I'm not sure how the system specifically works.

Edit: had a quick glance at the manual, it looks like the answer is no.
Thanks! Honestly, that bugged me for a while, now. I kind of like how you control particles variable (color, size, life span, direction, etc.) and feel comfortable with them, and was thinking of maybe trying using them to, for example, draw the damage done to an enemy over it's head, or something like that, tho I never actually really gave it a try. Guess the lesson for me here is: must not try to reinvent the wheel! :D
 

TheouAegis

Member
Thanks! Honestly, that bugged me for a while, now. I kind of like how you control particles variable (color, size, life span, direction, etc.) and feel comfortable with them, and was thinking of maybe trying using them to, for example, draw the damage done to an enemy over it's head, or something like that, tho I never actually really gave it a try. Guess the lesson for me here is: must not try to reinvent the wheel! :D
what you could do is, although it's not going to be as nice, is to break up the score font over multiple sprites. Each Sprite having only one frame. In that way it could work.
 
S

Spongyoshi

Guest
And... This works very well :D
Thanks a lot @TheouAegis you're always so helpful whenever I post here!!
And to @Taddio I wish you luck with what you're trying to achieve!
 

TheouAegis

Member
If you do want unrestricted score lengths, you can calculate how many digits you need by using

var digits = 1;
if score digits+=floor(log(score));
 
Top