Showing Money in game

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
HoAbGa
Newbie
Posts: 12
Joined: Mon Oct 30, 2017 4:38 am
Contact:

Showing Money in game

#1 Post by HoAbGa »

Hi all,

I am currently designing a game in RenPy.
There will be a money system in the game.
At game start, the player has 300 (maybe dollars). I just set a point-value for this at the game start.

Code: Select all

# The game starts here.
label start:
    $ money_points = 300
During the game, the player can earn and spend money.
In this example, the player receives 30 Money Points

Code: Select all

 $ money_points += 30
Now the question:

How can I make the current score visible in the game?
The player should see on the screen how much money he currently has. I would like to represent this with an additional picture, a dollar bill or similar.
It does not have to be a button. It just has to be seen, preferably in the upper right corner of the screen.

How can I make the value visible?

Thanks for your help!

User avatar
Steamgirl
Veteran
Posts: 322
Joined: Sat Jul 28, 2012 4:39 am
Completed: My Cup of Coffee, Queen at Arms (co-wrote a battle scene)
Projects: Stranded Hearts, Emma: A Lady's Maid
Deviantart: steamgirlgame
Contact:

Re: Showing Money in game

#2 Post by Steamgirl »

Here's some simple code for you:

Put this in your screens.rpy (although you can put it almost anywhere)

Code: Select all

#you can rename money_display to something else like wallet or whatever your want, just make sure it matches your "show" later
screen money_display:
   
    frame:
        xalign .5 #change this value between 0 and 1 if you want to move it to a different part of the screen horizontally
        yalign .02 #change this value if you want to move it to a different part of the screen vertically
        
        has vbox #this just makes all the elements inside the frame line up vertically, so you could add other things like hearts or XP or whatever
        text "[money_points]" #this will show your money points variable, you can add extra parameters such as font type, size, alignment etc
        
Then in your main game, you do...

Code: Select all

# The game starts here.
label start:
    $ money_points = 300
    show screen money_display #this shows the money
    "Some text"
    hide screen money_display #this hides the money
Hope this helps! Let me know if you have any questions or if you get any errors (I didn't get the chance to test it)! :)

You can find more info on custom screens here as well if you want to add buttons or whatever: https://www.renpy.org/doc/html/screens.html

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3791
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: Showing Money in game

#3 Post by Imperf3kt »

I'd highly suggest removing $ money_points = 300 and instead add

Code: Select all

default money_points = 300
Before the start label
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

HoAbGa
Newbie
Posts: 12
Joined: Mon Oct 30, 2017 4:38 am
Contact:

Re: Showing Money in game

#4 Post by HoAbGa »

Imperf3kt wrote: Thu Nov 09, 2017 4:11 pm I'd highly suggest removing $ money_points = 300 and instead add

Code: Select all

default money_points = 300
Before the start label
Thank you :)
but....
Why?
What is the difference?

HoAbGa
Newbie
Posts: 12
Joined: Mon Oct 30, 2017 4:38 am
Contact:

Re: Showing Money in game

#5 Post by HoAbGa »

Steamgirl wrote: Thu Nov 09, 2017 4:01 pm

Code: Select all

#you can rename money_display to something else like wallet or whatever your want, just make sure it matches your "show" later
screen money_display:
   
    frame:
        xalign .5 #change this value between 0 and 1 if you want to move it to a different part of the screen horizontally
        yalign .02 #change this value if you want to move it to a different part of the screen vertically
        
        has vbox #this just makes all the elements inside the frame line up vertically, so you could add other things like hearts or XP or whatever
        text "[money_points]" #this will show your money points variable, you can add extra parameters such as font type, size, alignment etc

Code: Select all

# The game starts here.
label start:
    $ money_points = 300
    show screen money_display #this shows the money
    "Some text"
    hide screen money_display #this hides the money
This wokrs!!!! THANK YOU!!!!
But how can I add a image (icon) for money to that value in the screen?

User avatar
Steamgirl
Veteran
Posts: 322
Joined: Sat Jul 28, 2012 4:39 am
Completed: My Cup of Coffee, Queen at Arms (co-wrote a battle scene)
Projects: Stranded Hearts, Emma: A Lady's Maid
Deviantart: steamgirlgame
Contact:

Re: Showing Money in game

#6 Post by Steamgirl »

The default thing just means the value gets set before the game runs.

If you were working on the game and had a save game, if you then added some new variable under the start label that you check against later, you'd get errors if you loaded the save. It wouldn't exist in your save game. But if you set it as default, the save would check against the default value. I don't know if I explained that very well... It's handy to know about it but the way you did it works just fine. ;)

Anyway, for the icon...

Code: Select all

screen money_display:
   
   #a frame creates a background for you so that your text isn't just floating around on screen but you can change it to a vbox or hbox if you don't want the frame
    frame:
        xalign .5 #change this value between 0 and 1 if you want to move it to a different part of the screen horizontally
        yalign .02 #change this value if you want to move it to a different part of the screen vertically
        has vbox
        
        #elements inside an hbox will appear horizontally compared to each other
        #you can use vbox: if you want them vertically, and you can stick hboxes inside vboxes and vice versa
        
        hbox:
            add "images/money_icon.png" #put your image location here
            text "[money_points]" #this will show your money points variable, you can add extra parameters such as font type, size, alignment etc
This is the important bit of code which you will need to change with the correct location.

Code: Select all

add "images/money_icon.png" #put your image location here
Last edited by Steamgirl on Thu Nov 09, 2017 6:16 pm, edited 1 time in total.

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3791
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: Showing Money in game

#7 Post by Imperf3kt »

HoAbGa wrote: Thu Nov 09, 2017 4:21 pm
Imperf3kt wrote: Thu Nov 09, 2017 4:11 pm I'd highly suggest removing $ money_points = 300 and instead add

Code: Select all

default money_points = 300
Before the start label
Thank you :)
but....
Why?
What is the difference?
For this game, no difference.

I merely suggested it as good habit to get into since there may come a time where you need a default value.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

HoAbGa
Newbie
Posts: 12
Joined: Mon Oct 30, 2017 4:38 am
Contact:

Re: Showing Money in game

#8 Post by HoAbGa »

Ok got it :)

And it works pretty well!
Thank you both!

User avatar
Taleweaver
Writing Maniac
Posts: 3428
Joined: Tue Nov 11, 2003 8:51 am
Completed: Metropolitan Blues, The Loyal Kinsman, Daemonophilia, The Dreaming, The Thirteenth Year, Adrift, Bionic Heart 2, Secrets of the Wolf, The Photographer
Projects: The Pilgrim's Path, Elspeth's Garden, Secret Adventure Game!
Organization: Tall Tales Productions
Location: Germany
Contact:

Re: Showing Money in game

#9 Post by Taleweaver »

Ren'Py question. Moved here.
Scriptwriter and producer of Metropolitan Blues
Creator of The Loyal Kinsman
Scriptwriter and director of Daemonophilia
Scriptwriter and director of The Dreaming
Scriptwriter of Zenith Chronicles
Scriptwriter and director of The Thirteenth Year
Scriptwriter and director of Romance is Dead
Scriptwriter and producer of Adrift
More about me in my blog
"Adrift - Like Ever17, but without the Deus Ex Machina" - HigurashiKira

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot]