1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

Numeral separators *SOLVED*

Discussion in 'Programming' started by Zefyrinus, Mar 14, 2017.

  1. Zefyrinus

    Zefyrinus Member

    Joined:
    Jul 11, 2016
    Posts:
    20
    Note: I'm using GM 6.1. I know it's outdated, but because I started this game project using 6.1, I'm also going to finish it with it.

    I think this topic has been posted about before, but I can't find anything relevant. Maybe I just don't know what keywords to search for. :confused:

    Anyway, in my game the player has money which can maximally be $ 999 999 999 999. This figure is drawn on screen. But it's confusing if you see for example 100000000000 on screen, so it would be better if it was drawn as 100 000 000 000, with a space between each group of three digits. I have no idea how to go about programming this. Surely someone must've already come up with a solution for this?

    Also, for it to look pretty in context of my game, the number needs to be right-aligned, and preceded by a $ in smaller size than the number. So the $ probably needs to be drawn as a separate string, but given that the number string is right-aligned and of variable length, how do I define the horizontal placement of the $ string so that it's always slightly to the left of the left edge of the number string?
     
  2. NightFrost

    NightFrost Member

    Joined:
    Jun 24, 2016
    Posts:
    2,012
    Turn the number into a string (does 6.1 have string() function?). Have a loop that goes from string length to zero. Inside the loop, draw the letter at loop variable position, and after every three steps also draw a space. Do this draw from right to left by using a variable for the draw x position and decrement after every draw. After the loop, this variable should more or less point where you want to draw the $ symbol. You'll have to manually adjust the numbers based on your font size to make it look good.
     
    Zefyrinus likes this.
  3. JFitch

    JFitch Member

    Joined:
    Sep 28, 2016
    Posts:
    428
    Code:
    new_money=""
    for (var i=money;i>=1;i=i div 1000)
    {
    new_money=" "+string(i mod 1000)+new_money
    }
    I tested this and it works. "Money" is the original money variable, and "new_money" is the string that you'd be drawing. Put this in your STEP event.
     
    Zefyrinus likes this.
  4. Zefyrinus

    Zefyrinus Member

    Joined:
    Jul 11, 2016
    Posts:
    20
    Thanks for the replies, but it's not working. :/ First I tried JFitch's solution. I wrote the exact same code, but with funds_string and funds instead of new_money and money, and put it in the step event. It would not compile. It said something about an unexpected character at position 12, which would be the "=" after "var i". I then tried removing "var" and it compiled fine, but the text is not drawn. In the draw even I have a font and a color set, and then I use the DD to draw funds_string at 300, 300. The object drawing the text has the lowest depth (-2) so it can't be that the text is obscured by other objects.

    Since I don't really understand that code, I thought of trying to do it myseld from scratch, following NightFrost's instructions. But I don't understand how you add characters into a string. I don't really have any experience working with strings, and I can't understand GM 6.1's help file where it explains all the string functions. :confused:
     
  5. NightFrost

    NightFrost Member

    Joined:
    Jun 24, 2016
    Posts:
    2,012
    JFitch's code splits the string but doesn't draw it, add a draw_text (or whatever 6.1 uses to draw strings) into the draw event to display it at a position you want.
     
  6. Perseus

    Perseus Not Ragarnak Forum Staff Moderator

    Joined:
    Dec 29, 2016
    Posts:
    953
    You can create a script to directly convert the real value being held in the money variable to a string with a space as the thousands separtor in the Draw event. You can also insert a "$" sign right at the beginning of the generated string to make things easier.

    Code:
    ///string_thousands(integer)
    var s, i;
    s = string(argument0);
    for (i = string_length(s) - 2; i > 1; i -= 3) {
        s = string_insert(" ", s, i);
    }
    s = string_insert("$", s, 1);
    return s;
    
    Draw:
    Code:
    draw_text(x, y, string_thousands(global.money));
    
    Hope that helps. :)
     
    Zefyrinus likes this.
  7. Zefyrinus

    Zefyrinus Member

    Joined:
    Jul 11, 2016
    Posts:
    20
    @Ragarnak: Thanks! :) After a bit of tweaking I got it to work.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice