How to add +1 to a variable every second

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
User avatar
renardjap
Regular
Posts: 75
Joined: Sun Aug 05, 2018 1:08 pm
Location: France ! Cocorico
Contact:

How to add +1 to a variable every second

#1 Post by renardjap » Wed Mar 04, 2020 2:11 pm

Hello guys !

I want to increase a resources variable every second. For exemple, when the player do an activity like mining iron, the iron variable increase every second +1
And I want to display the increasement on a screen. I know I need to do some loops... but I don't know how...

I search some tuto or advises on the forum, I found something with ui.timer but it's out of date (the post was written in 2008). I tried something in python

Code: Select all

python:
        iron = 0
        while True:
            iron +1
            time.sleep(1.0)
But my computer crashed...

I don't understand how work time and how can I use the function ? Can you help me ?

User avatar
IrinaLazareva
Veteran
Posts: 399
Joined: Wed Jun 08, 2016 1:49 pm
Projects: Legacy
Organization: SunShI
Location: St.Petersburg, Russia
Contact:

Re: How to add +1 to a variable every second

#2 Post by IrinaLazareva » Wed Mar 04, 2020 2:48 pm

Code: Select all

default iron = 0
screen resources():
    text "Iron: [iron]" at topleft
    timer 1.0 repeat True action SetVariable('iron', iron+1)
 
label start:
    '?'
    show screen resources
    '!'
The algorithm can be improved as you wish

https://renpy.org/doc/html/screens.html#timer
https://renpy.org/doc/html/screen_actio ... etVariable
https://renpy.org/doc/html/screens.html ... statements

4example:

Code: Select all

default gold = 0
default silver = 0
default vita = 1000

screen resources():
    vbox at topleft:
        text "Silver: [silver]"
        text "Gold: [gold]"
        text "Vita: [vita]"
    if vita>0:
        timer 1.0 repeat True action SetVariable('gold', gold+1)
        timer 3.7 repeat True action SetVariable('silver', silver+1)
        timer 10.0 repeat True action SetVariable('vita', vita-1)

User avatar
Alex
Lemma-Class Veteran
Posts: 2981
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: How to add +1 to a variable every second

#3 Post by Alex » Wed Mar 04, 2020 3:00 pm

Just a minor adding...

Code: Select all

default iron = 0

screen counter_scr():
    on "show" action Function(renpy.retain_after_load) # <---

    timer 1.0 action SetVariable("iron", iron + 1) repeat True
    text "iron: [iron]" align (0.05, 0.05)

label start:
    
    show screen counter_scr
    "?!"
https://www.renpy.org/doc/html/save_loa ... after-load

User avatar
renardjap
Regular
Posts: 75
Joined: Sun Aug 05, 2018 1:08 pm
Location: France ! Cocorico
Contact:

Re: How to add +1 to a variable every second

#4 Post by renardjap » Wed Mar 04, 2020 5:08 pm

It was finally so easy... when we know the solution... :D Thank you very much !

I have one more little problem: when I use the History and go back, the variable come back to zero... Is it possible to keep the score even we go back in the history ?

rayminator
Miko-Class Veteran
Posts: 754
Joined: Fri Feb 09, 2018 12:05 am
Location: Canada
Contact:

Re: How to add +1 to a variable every second

#5 Post by rayminator » Wed Mar 04, 2020 9:02 pm

you can try using these they might help

load and save

Code: Select all

renpy.retain_after_load()
Fixing Rollback
https://www.renpy.org/doc/html/save_loa ... g-rollback

Styling Fixed Rollback
https://www.renpy.org/doc/html/save_loa ... d-rollback

Post Reply

Who is online

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