'Method' to calculate 'time' whenever 'time' changes

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
Brandom
Newbie
Posts: 11
Joined: Thu Mar 16, 2017 6:24 pm
Contact:

'Method' to calculate 'time' whenever 'time' changes

#1 Post by Brandom »

Hello all,

First of all. Thank you very much for providing a platform where people such as myself can come and learn from others. I have learned a lot already and am sure will use this forum for many more things.

Anyway, on to the problem. I am working on a visual novel where time will have to be displayed. It's at very early stages and just working on the layout, interface, etc. before continueing. I have managed to get a simple clock showing and am able to alter the time.

This will be initialized at the start:

Code: Select all

    $ minutes = 420
    $ clock_hours = minutes / 60
    $ clock_minutes = minutes - (clock_hours * 60)
    $ day = 1
And the time will change after a certain event, for now just a single mouse click after someone says something, as a test:

Code: Select all

    $ minutes += 15
    if (minutes >= 1440):
        $ day += 1
        $ minutes = 420
    $ clock_hours = minutes / 60
    $ clock_minutes = minutes - (clock_hours * 60)
    "The time has jumped 15 minutes."

    $ minutes += 1005
    if (minutes >= 1440):
        $ day += 1
        $ minutes = 420
    $ clock_hours = minutes / 60
    $ clock_minutes = minutes - (clock_hours * 60)
    "It is now the next day."
However, the way I have it now is that when minutes are added, I recalculate the time by adding the if (......* 60) piece of code every time I change the time.
From a few other programming languages (I have a tiny bit of experience here and there, but lacking in many, for example Python), I know there are 'methods' that can be defined in certain languages, which can be 'run' everytime they're 'called'.
I was hoping that, instead of having to add the extra lines in the second piece of code, I could just add a single line (the method), which would recalculate the time every time it's changed or so that it runs every time a mouseClick is performed or maybe even continuously? Maybe a label that can be jumped to, run and then 'jumped' back to where we left off? If that's even possible.

I am sure there is a pretty easy and 'tidy' solution for this, but I am stumped as the few things I have tried did not seem to work.

Thanks in advance for your time!

philat
Eileen-Class Veteran
Posts: 1909
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: 'Method' to calculate 'time' whenever 'time' changes

#2 Post by philat »

Super basic example. You can get into some fancier stuff with trooper6's quite awesome clock tutorial here: viewtopic.php?f=51&t=21978

divmod is a useful shortcut when doing time. I put in the renpy.say statements for debugging's sake -- if you don't need the notifications, feel free to take them out.

Code: Select all

default minutes = 420
default clock_hours = 7
default clock_minutes = 0
default day = 1

init python:
    def set_time(min):
        global minutes
        global clock_hours
        global clock_minutes
        global day
        minutes += min
        clock_hours, clock_minutes = divmod(minutes, 60)
        temp_day, clock_hours = divmod(clock_hours, 24)
        renpy.say(None, "%s minutes have passed." % min)
        if temp_day + 1 > day:
            day += 1
            renpy.say(None, "It is now the next day.")

label start:
    "[minutes] [clock_hours] [clock_minutes] [day]"
    $ set_time(65)
    "[minutes] [clock_hours] [clock_minutes] [day]"
    $ set_time(1455)
    "[minutes] [clock_hours] [clock_minutes] [day]"

Brandom
Newbie
Posts: 11
Joined: Thu Mar 16, 2017 6:24 pm
Contact:

Re: 'Method' to calculate 'time' whenever 'time' changes

#3 Post by Brandom »

Thank you for your reply!
I did have a read through that thread, but figured I didn't need something as complicated as that - plus, I probably would not need an analog clock either, just a digital version.

But, thanks! The code works, obviously :). I added a small check so that you'll "wake up" at 7.00am by default, regardless of the 'set_time()'.

That seems to work pretty well now! Awesome!

I have a small follow up question, which I am sure I would be able to figure out. But if I were to add a check to prevent the player from 'sleeping throughout the day', I could have something similar to:

Code: Select all

    menu:
        "Take a 30 minute nap..."
            if (minutes < 1380):
                $ set_time(30)
            else:
                "It is too late for a nap, might as well go to sleep..."
                $ set_time(100) #100 would put the value over 1440
        "Sleep untill morning..."
            if (minutes < 1380)
                jump tooearlytosleep
            else:
                $ set_time(100)

label home
    "Good morning!"
    # And the story continues here...

label tooearlytosleep
    "It is too early to sleep, find something to do for a while."
    jump home
Correct?

JMHenriques
Newbie
Posts: 1
Joined: Fri Jan 22, 2016 1:48 pm
Contact:

Re: 'Method' to calculate 'time' whenever 'time' changes

#4 Post by JMHenriques »

Hello all,

New to programming and renpy and I still have several doubts here...


How can I center the:
renpy.say(None, "It is now the next day.")

I can change the size and stuf like that but the centered is outside of the actual test and the below code dont work:
centered renpy.say(None, "It is now the next day.")

Tried other versions but nothing worked. I tried to just use the text (without renpy.says) but it seems that that doesnt work either...

Post Reply

Who is online

Users browsing this forum: No registered users