Timer on the background (Timer, energy)

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
prestristan
Regular
Posts: 25
Joined: Sat Apr 11, 2020 1:28 pm
Contact:

Timer on the background (Timer, energy)

#1 Post by prestristan »

Im trying to build a game with a timer on the background.
A timer will countdown on the background and after it goes to 0, you will have a point that you can spend in the game.

It's kinda like (most) Visual novel games on Android nowadays.
But I don't want the "Energy" system where you need to spend "Energy" to open a chapters yada yada yada..
I'm thinking of a code that will increase the "points" for a character in real time. Even when the app is closed or just running on the background.

I did some coding, combing different forum posts and codes I found in LSF and on the WWW.
And here's what I got:

Code: Select all

# The script of the game goes in this file.

# Declare characters used by this game. The color argument colorizes the
# name of the character.


define e = Character("Eileen")
default now = 0
default timer = 0
default give_point_timer = 10  # Seconds
default point = 0
#default today = datetime.datetime.now().strftime("%S")

init python:
    import time

screen cash():
    timer 1.0 action Jump("waiting_loop")
    vbox:
        text "Eileen's Points: [point]"
        text "+1 in [wait:.0f] seconds"



label waiting_loop:
    hide screen cash
    show screen cash
    $ now = time.time()
    $ wait = timer + give_point_timer - now

    #"Wait for [wait:.0f] seconds"

    if wait < 1:
        $point = point + 1
        $wait =wait + 9
        $ timer = time.time()
    #jump waiting_loop



label start:
    show screen cash
    scene bg house
    "JUST RANDOM"
    $ timer = time.time()
    screen black
    "Yoh"
    jump waiting_loop


    return
I know I'm doing something wrong here but I don't know it think it should be coded inside the "Screen" because the timer should be happening in the background.
But Thank you all so much in advanced you guys.

User avatar
Per K Grok
Miko-Class Veteran
Posts: 882
Joined: Fri May 18, 2018 1:02 am
Completed: the Ghost Pilot, Sea of Lost Ships, Bubbles and the Pterodactyls, Defenders of Adacan Part 1-3, the Phantom Flyer
itch: per-k-grok
Location: Sverige
Contact:

Re: Timer on the background (Timer, energy)

#2 Post by Per K Grok »

prestristan wrote: Sat Apr 11, 2020 1:57 pm Im trying to build a game with a timer on the background.
A timer will countdown on the background and after it goes to 0, you will have a point that you can spend in the game.

It's kinda like (most) Visual novel games on Android nowadays.
But I don't want the "Energy" system where you need to spend "Energy" to open a chapters yada yada yada..
I'm thinking of a code that will increase the "points" for a character in real time. Even when the app is closed or just running on the background.

I did some coding, combing different forum posts and codes I found in LSF and on the WWW.
And here's what I got:

Code: Select all

# The script of the game goes in this file.

# Declare characters used by this game. The color argument colorizes the
# name of the character.


define e = Character("Eileen")
default now = 0
default timer = 0
default give_point_timer = 10  # Seconds
default point = 0
#default today = datetime.datetime.now().strftime("%S")

init python:
    import time

screen cash():
    timer 1.0 action Jump("waiting_loop")
    vbox:
        text "Eileen's Points: [point]"
        text "+1 in [wait:.0f] seconds"



label waiting_loop:
    hide screen cash
    show screen cash
    $ now = time.time()
    $ wait = timer + give_point_timer - now

    #"Wait for [wait:.0f] seconds"

    if wait < 1:
        $point = point + 1
        $wait =wait + 9
        $ timer = time.time()
    #jump waiting_loop



label start:
    show screen cash
    scene bg house
    "JUST RANDOM"
    $ timer = time.time()
    screen black
    "Yoh"
    jump waiting_loop


    return
I know I'm doing something wrong here but I don't know it think it should be coded inside the "Screen" because the timer should be happening in the background.
But Thank you all so much in advanced you guys.
try this

Code: Select all


default timeleft=10
default point=0

screen cash():
    if timeleft>1:
        timer 1.0 action SetVariable("timeleft", timeleft-1) repeat True
    elif timeleft==1:
        timer 1.0 action [SetVariable("timeleft", 0), SetVariable("point", point+1) ]
    elif timeleft==0:
        timer 1.0 action SetVariable("timeleft", 10)
    vbox:
        text "Eileen's Points: [point]"
        if timeleft>0:
            text "+1 in [timeleft] seconds"
            
            

prestristan
Regular
Posts: 25
Joined: Sat Apr 11, 2020 1:28 pm
Contact:

Re: Timer on the background (Timer, energy)

#3 Post by prestristan »

Per K Grok wrote: Sat Apr 11, 2020 4:23 pm try this

Code: Select all


default timeleft=10
default point=0

screen cash():
    if timeleft>1:
        timer 1.0 action SetVariable("timeleft", timeleft-1) repeat True
    elif timeleft==1:
        timer 1.0 action [SetVariable("timeleft", 0), SetVariable("point", point+1) ]
    elif timeleft==0:
        timer 1.0 action SetVariable("timeleft", 10)
    vbox:
        text "Eileen's Points: [point]"
        if timeleft>0:
            text "+1 in [timeleft] seconds"
            
            
Yeah! this gives me some logic on how the timer should work and I thank you for that.
The only problem is that this won't be counting down when the app / program is closed.

I'll try playing with "SetVariable" plus the time.time() and see what I can come up with. :D

User avatar
Per K Grok
Miko-Class Veteran
Posts: 882
Joined: Fri May 18, 2018 1:02 am
Completed: the Ghost Pilot, Sea of Lost Ships, Bubbles and the Pterodactyls, Defenders of Adacan Part 1-3, the Phantom Flyer
itch: per-k-grok
Location: Sverige
Contact:

Re: Timer on the background (Timer, energy)

#4 Post by Per K Grok »

prestristan wrote: Sat Apr 11, 2020 10:07 pm
Per K Grok wrote: Sat Apr 11, 2020 4:23 pm try this

Code: Select all


default timeleft=10
default point=0

screen cash():
    if timeleft>1:
        timer 1.0 action SetVariable("timeleft", timeleft-1) repeat True
    elif timeleft==1:
        timer 1.0 action [SetVariable("timeleft", 0), SetVariable("point", point+1) ]
    elif timeleft==0:
        timer 1.0 action SetVariable("timeleft", 10)
    vbox:
        text "Eileen's Points: [point]"
        if timeleft>0:
            text "+1 in [timeleft] seconds"
            
            
Yeah! this gives me some logic on how the timer should work and I thank you for that.
The only problem is that this won't be counting down when the app / program is closed.

I'll try playing with "SetVariable" plus the time.time() and see what I can come up with. :D
Counting time while the program is closed would be a separate function. What you would need to do is to in the exit procedure include, get datetime, and save that information in a variable. When starting a new session - get datetime -, compare it to the saved variable of datetime at earlier exit, transform the difference into points and add that to the point variable.

User avatar
beastcarving
Regular
Posts: 139
Joined: Mon May 13, 2019 5:03 pm
Completed: Pulse Cage https://beastcarving.itch.io/pulse-cage-the-full-series
Projects: Your Brother's Religion
Organization: BeastCarving Studio
IRC Nick: BeastCarving
Tumblr: beastcarving
Deviantart: beastcarving
Github: beastcarving
itch: beastcarving
Contact:

Re: Timer on the background (Timer, energy)

#5 Post by beastcarving »

Counting time while the program is closed would be a separate function. What you would need to do is to in the exit procedure include, get datetime, and save that information in a variable. When starting a new session - get datetime -, compare it to the saved variable of datetime at earlier exit, transform the difference into points and add that to the point variable.
This is what I was looking for, but I'm such a rookie, I have no clue how to put this together. Could you help?
Image Pulse Cage (full game)Image Your Brother's Religion (Demo)
PLAY HERE: https://beastcarving.itch.io/
Love my work: https://www.patreon.com/beastcarving

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Timer on the background (Timer, energy)

#6 Post by Remix »

As your first post alluded to, you need to totally move away from any in-built Ren'Py time system (which generally work on seconds while in game, not menu, context).

This kind of thing can only really work by referencing an external time value.

The general concept is:
You know the time value of now (likely a large float indicating seconds since epoch)
Let's say it is 125474200 now
When you schedule an event (such as energy increase or building upgrade complete) you store a number to represent the exact moment that happens.
Let's say that is now plus 3600 (an hour) 125474200 + 3600 = 125477800
You come back to the game (or are still playing and some periodical check happens)
You know the time value of now and can check to see if any scheduled event is historic (has passed)

So basically, you can read an integer representing seconds, can set a variable to be an integer and can test to see if one integer is below or above another.

Simplicity:

Code: Select all

init python:
    import time

default next_cash = time.time() + 60 # 60 seconds time

label start:

    if next_cash <= time.time():

        "You waited a full minute..."

    else:

        return

    "wait longer"

    jump start
time.time() gives us a seconds count, setting a saved variable to a number that is that value plus something gives a time in the future, testing current seconds versus that gives us success or not
Frameworks & Scriptlets:

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Timer on the background (Timer, energy)

#7 Post by Remix »

Note:

If you have some periodic or loop system that checks if things are done, you really should also check if the value of time.time() is lower (see note) than the last found value of it.
As time.time() uses the system clock, people can adjust it to race through games.

Note:
To race, people would set their clock in the future (the direction it generally goes).
You can kind of test an idle time (last known vs now) and set a max on that.
You can also see the cheats off by relying on either they reset their clock afterwards (spotted) or their system clock resets via the internet (spotted)
Frameworks & Scriptlets:

User avatar
beastcarving
Regular
Posts: 139
Joined: Mon May 13, 2019 5:03 pm
Completed: Pulse Cage https://beastcarving.itch.io/pulse-cage-the-full-series
Projects: Your Brother's Religion
Organization: BeastCarving Studio
IRC Nick: BeastCarving
Tumblr: beastcarving
Deviantart: beastcarving
Github: beastcarving
itch: beastcarving
Contact:

Re: Timer on the background (Timer, energy)

#8 Post by beastcarving »

Here's where I am now:

Code: Select all

init python:
    import time
default timeleft=10
default now = 0
default just_wait = 0
default TIME = 60

screen wait():
    if persistent.pages < 2:

        if timeleft>1 :
            timer 1.0 action SetVariable("timeleft", timeleft-1) repeat True
            $ just_wait = time.time()
        elif timeleft==1:
            timer 1.0 action [SetVariable("timeleft", 0), SetVariable("persistent.pages", persistent.pages+1) ]
        elif timeleft==0:
            if timeleft <= time.time():
                timer 1.0 action SetVariable("timeleft", 60)
        vbox:
            if timeleft>0:
                $ now = time.time()
                $ wait = just_wait + TIME - now
                text "{font=qokijo.ttf}+[timeleft]" ypos 12 xpos 400

    elif persistent.pages < 3:
        vbox:
            text "{font=qokijo.ttf}full" ypos 12 xpos 400


    if layout.QUIT:
        $ persistent.last_exit = time.time()
Every time I go to any menu the timer pauses, and only resumes if I'm watching it. The same thing happens when I close the game. Is there anything I've done wrong that's causing this?
Image Pulse Cage (full game)Image Your Brother's Religion (Demo)
PLAY HERE: https://beastcarving.itch.io/
Love my work: https://www.patreon.com/beastcarving

User avatar
hell_oh_world
Miko-Class Veteran
Posts: 777
Joined: Fri Jul 12, 2019 5:21 am
Contact:

Re: Timer on the background (Timer, energy)

#9 Post by hell_oh_world »

that's the default behavior of a timer... once a screen hides, its timers will be in paused state, there's nothing you can do about it. i think your best bet is to place the timer in a screen that is shown all the time. and i think its doable if you place the screen in another layer, *i think*

Post Reply

Who is online

Users browsing this forum: Majestic-12 [Bot]