Ren'Py [SOLVED] Getting a money inventory gui looking clean.

Heisenberg867

Newbie
Jul 18, 2018
90
149
Hi all,

I've got a dirt simple money gui up and running, but I want to make two changes: (i) I want the frame I use to have a little padding on the right side so that there's a space between the last zero and the edge of the frame, and (ii) I want the figure to display commas, so that $1000000 will display as $1,000,000.

Fashion Business does it really well, using code that looks like this: $ money_text = "$ " + '{:10,.2f}'.format(money), but there's a surprising amount going on there, and I can't figure out how to import what it's doing into my existing code.

Anybody know the answers? Thanks in advance! My current code below, and how it looks on the screen attached as a screenshot.

***

style frame_gui1:
background Frame("images/gui/Frame1.png", 25, 25, 25, 25)

screen moneyhud:

frame:
style "frame_gui1"

has hbox xalign 0.0 yalign 0.0
add "images/money_dollar.png"
text "{font=images/gui/Franklin Gothic Demi Regular.ttf}{color=#e80000}$[money]{/color}{/font}" yoffset 15
 

Kinderalpha

Pleb
Donor
Dec 2, 2019
198
265
I don't know the entire scope of your code or how you're determining your money variable so I can't help you there. The money formatting with decimals is just apply a string format method to it. The gap could possibly be fixed by adding a space after the money variable in your text entry.

Code:
text "{font=images/gui/Franklin Gothic Demi Regular.ttf}{color=#e80000}$[money] {/color}{/font}" yoffset 15
Not sure if it will interpret the space or if your container will act accordingly but worth a shot. There's probably a better option.

You can format by using this method on a string.

Code:
'${:,.2f}'.format(moneyVariable)
Where moneyVariable is the variable for money. Read for an explanation on what's going on there. It's just reading your input integer to format().
 
Last edited:

Heisenberg867

Newbie
Jul 18, 2018
90
149
You can format by using this method on a string.

Code:
'${:,.2f}'.format(moneyVariable)
Where moneyVariable is the variable for money. Read for an explanation on what's going on there. It's just reading your input integer to format().
Thanks for this. The spacing advice worked perfectly. I'm still a little confused for how to incorporate your money code into my pre-existing code. Where exactly do I put this:

'${:,.2f}'.format(money)

or for that matter, this:

def as_currency(amount):
if amount >= 0:
return '${:,.2f}'.format(amount)
else:
return '-${:,.2f}'.format(-amount)

The code is currently dirt simple:

Code:
$ money = 0
$ display_money = True

style frame_gui1:
    background Frame("images/gui/Frame1.png", 25, 25, 25, 25)

screen moneyhud:

    frame:
        style "frame_gui1"

        has hbox xalign 0.0 yalign 0.0
        add "images/money_dollar.png"
        text "{font=images/gui/Franklin Gothic Demi Regular.ttf}{color=#e80000}$[money] {/color}{/font}" yoffset 12
 

Kinderalpha

Pleb
Donor
Dec 2, 2019
198
265
I'm not quite familiar with what's going on inside your string using the curly brackets, since I don't use Renpy. However, your snippet kinda works. You could rename that function to getMoneyString() and have it return the formatted string appropriate to it's value using '${:,.2f}'.format(moneyVariable) then calling that function into a variable inside by doing $ money = getMoneyString and then keeping what you have for the screen.

Hope this makes sense because typing this out on a phone is difficult. If I was at a laptop I could probably do more justice here.

edit: I actually don't think you need to do all that because you already have. Just make the money variable call the as_currency function and you're done.

$ money = as_currency(1000)
 
Last edited:

dikau

Member
Dec 16, 2019
316
282
Never tried Ren'py, but try this:

Code:
$ money = 0
$ display_money = True

style frame_gui1:
    background Frame("images/gui/Frame1.png", 25, 25, 25, 25)

screen moneyhud:

    frame:
        style "frame_gui1"

        has hbox xalign 0.0 yalign 0.0
        add "images/money_dollar.png"

        ##
        $ money_text = "$ " + '{:10,.2f}'.format(money) 
        ##

        text "{font=images/gui/Franklin Gothic Demi Regular.ttf}{color=#e80000}[money_text] {/color}{/font}" yoffset 12
 

Heisenberg867

Newbie
Jul 18, 2018
90
149
Never tried Ren'py, but try this:

Code:
$ money = 0
$ display_money = True

style frame_gui1:
    background Frame("images/gui/Frame1.png", 25, 25, 25, 25)

screen moneyhud:

    frame:
        style "frame_gui1"

        has hbox xalign 0.0 yalign 0.0
        add "images/money_dollar.png"

        ##
        $ money_text = "$ " + '{:10,.2f}'.format(money)
        ##

        text "{font=images/gui/Franklin Gothic Demi Regular.ttf}{color=#e80000}[money_text] {/color}{/font}" yoffset 12
Sorry, no joy. Your code doesn't break anything, but when I subsequently give the command "$ money = 1000", the game just spits out "$1000" in the HUD with no thousand separator.

Any other ideas? I admit I'm way too far out of my depth to try anything else on my own.
 

Heisenberg867

Newbie
Jul 18, 2018
90
149
edit: I actually don't think you need to do all that because you already have. Just make the money variable call the as_currency function and you're done.

$ money = as_currency(1000)
I hate to be the kind of guy who can't bridge a gap myself, but there's just so much going on here that's above my figurative pay grade. Once you're back at your comp, would you mind spelling it out? I promise I've spent hours on this, and every explanation I can find has too many assumptions baked in about my knowledge level.
 

dikau

Member
Dec 16, 2019
316
282
Did you also change the variable on last line to [money_text]?
Code:
text "{font=images/gui/Franklin Gothic Demi Regular.ttf}{color=#e80000}[money_text] {/color}{/font}" yoffset 12




if it's still not working, try to move
Code:
$ money_text = "$ " + '{:10,.2f}'.format(money)
just below the
Code:
$ display_money = True

if it's still not working, then you'll have to wait for someone that better at Ren'Py to help :LOL:
 
Last edited:

Heisenberg867

Newbie
Jul 18, 2018
90
149
Did you also change the variable on last line to [money_text]?
IT WORKED! Thank you very, very much. Full "solution" posted below for anyone in the future looking for a simple Ren'Py money gui, in the sort of idiot's language I needed when I started.

Code:
style frame_gui1:
    background Frame("images/gui/Frame1.png", 25, 25, 25, 25)

screen moneyhud:

    frame:
        style "frame_gui1"

        has hbox xalign 0.0 yalign 0.0
        add "images/money_dollar.png"
        $ money_text ='{:10,.2f}'.format(money)
        text "{font=images/gui/Montserrat-Light.ttf}{color=#e80000}$[money_text] {/color}{/font}" yoffset 12
If you are that future person, copy and past the above block into the top of the script file. Note, whoever you are, that:

1. You'll need to find and paste a .ttf (font file) in the appropriate folder to replace the .ttf file "Montserrat-Light" above. I stole Montserrat from Cure My Addiction (The Ordeal).
2. You can change the color of the text by changing the #e80000 hex color code.
3. You'll need to find .png files of your own to replace the money icon (money_dollar.png) and the semi-opaque rectangular background (Frame1.png). The money icon I stole from Fashion Business (where there are others that resemble larger sums) and Frame1.png I stole from Milfy City.

4. Those four "25"s in the frame line are responsible for that pleasing rounding effect on the corners of the HUD.
5. Changing the values of xalign and yalign will move the display to a different part of the screen.
6. yoffset 12 shifted the numerals 12 pixels downward to center them better relative to the crumpled dollar bill.

7. When you want the money HUD to show up in the script, enter the line "$ money = 0" (or whatever you want the player to start with), followed by "show screen moneyhud" on the next line. When you want the HUD to go away, enter the line "hide screen moneyhud".
8. To add money, enter the line "$ money += 1,000" (or whatever sum). "-=" subtracts from the player's total, and "=" revises the player's total to a set amount.
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,957
16,192
Fashion Business does it really well, using code that looks like this: $ money_text = "$ " + '{:10,.2f}'.format(money), but there's a surprising amount going on there, and I can't figure out how to import what it's doing into my existing code.
No, it did it totally wrong. The f parameter is for formatting floating point values, it have nothing to do with what you want.

What you are searching for is the n parameter which format number according to the locale convention. But for this to effectively works, you need to first declare the said convention.

What you are searching for is this :
Code:
init python:
    import locale
    locale.setlocale(locale.LC_ALL, '')

[...]
screen whatever():
    text "{:n}".format( money )

Side notes:
It's not possible to correctly use setlocale on Ren'py. The second argument is supposed to be the code for the locale (in your case en_EN or perhaps en_US) but it must be an ASCII string, which isn't possible with Ren'py which silently change all strings into unicode ones ; and yes it matter even when the content of the string is strictly the same in both case.
But this is in fact not a problem, since it will make the game use the locale of the player, and so the number representation that apply for his country ; which is way better than forcing him to use your own representation.
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,957
16,192
you need an ascii string, where's the problem?
Well, last time I tried to use the b prefix, Ren'py was turning the string in unicode anyway. Good thing if it works now or was solved.

This said, it should also works by using str, I was apparently too focused on setlocale bug :(