Need help with time system

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
Swinwappy
Newbie
Posts: 14
Joined: Mon Jun 01, 2020 3:52 am
Contact:

Need help with time system

#1 Post by Swinwappy »

Hi,

So I found this code online and wanted to edit it slightly (I'm quite new to Python):

Code: Select all

init python:
    class day_time(object):
        def __init__(self):
            self.day = 1 # set this to whatever starting day is
            self.weekdays = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"] # arrange these so first weekday goes first
            self.time_of_day = ["8AM","9AM","10AM","11AM","12PM","1PM","2PM","3PM","4PM","5PM","6PM","7PM","8PM","9PM","10PM","11PM","12AM","1AM","2AM","3AM"] # add or remove to increase time of day slots
            self.end_of_day = self.time_of_day[-1] # automatically picks last slot as end of day

        @property
        def weekday(self):
            return self.weekdays[(self.day-1)%7]

        @property
        def time(self):
            return self.time_of_day[0]

        def advance(self, increment = 0, days = 0):
            if not (increment + days): # no input
                increment = 1

            if days: # add to increment by length of time_of_day
                increment += days * len(self.time_of_day)

            while increment > 0: # loop through increments to shift timeslot and days forward
                if self.time_of_day[0] == self.end_of_day:
                    self.day += 1
                self.time_of_day.append(self.time_of_day.pop(0))
                increment -= 1 # reduce incrememnt to escape loop after enough runs




default clock = day_time()
Essentially, what this does is implement the 7 days of the week as well as a time system (8am - 3am). You can pass time with:

Code: Select all

clock.advance()
You can pass a few hours with

Code: Select all

clock.advance(3)
And when time passes after 3am, the day of the week increases by one.

So currently if my player does something at 3am that advances the time by 3 hours, the day increases but instead of starting at 8am, they of course start at 10am.

I want to be able to add on to this a system so that if the player does something that advances time past 3am, they get a message that says "It's late so you go to sleep", and then increases the day, but sets the time to 8AM.

Long shot, but could anyone help me with this, nothing I've tried works.
cheers!

User avatar
zmook
Veteran
Posts: 421
Joined: Wed Aug 26, 2020 6:44 pm
Contact:

Re: Need help with time system

#2 Post by zmook »

Untested, but you might find it easier to work with something like this:

Code: Select all

init python:
    class day_time(object, start_time=0):
        def __init__(self):
            self.t = start_time   # count a constantly-increasing number of hours since the game began
            self.weekdays = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"] # arrange these so first weekday goes first
            self.times = ["8AM","9AM","10AM","11AM","12PM","1PM","2PM","3PM","4PM","5PM","6PM","7PM","8PM","9PM","10PM","11PM","12AM","1AM","2AM","3AM"] # add or remove to increase time of day slots
            self.end_of_day = self.time_of_day[-1] # automatically picks last slot as end of day

        @property
        def day(self):
            return int(self.t/len(self.time_of_day)) + 1  
            
        @property
        def weekday(self):
            return self.weekdays[(self.day-1)%7]

        @property
        def i_time(self):
            return self.t % len(self.times)

        @property
        def time_of_day(self):
            return self.times[self.i_time]
        

        def advance(self, increment = 0, days = 0):
            if not (increment or days): # no input
                increment = 1

            need_sleep = False
            start_day = self.day
            
            self.t += days*len(self.times) + increment
            
            if (self.day > start_day):
                # if we've passed a night, reset the clock to 8AM
                self.t = (self.day - 1) * len(self.times)
                renpy.notify("It's late so you go to sleep.")
                need_sleep = True
                
            # this is optional, but you might want to trigger a game action when sleep 
            # happens, such as calling a scene?
            return need_sleep


default clock = day_time()
Keeping time as a constantly-increasing integer, and then computing hours and days and whatever from it as necessary, is how your computer operating system does it.
colin r
➔ if you're an artist and need a bit of help coding your game, feel free to send me a PM

Swinwappy
Newbie
Posts: 14
Joined: Mon Jun 01, 2020 3:52 am
Contact:

Re: Need help with time system

#3 Post by Swinwappy »

zmook wrote: Sun Jun 05, 2022 12:12 pm Untested, but you might find it easier to work with something like this:

...

Keeping time as a constantly-increasing integer, and then computing hours and days and whatever from it as necessary, is how your computer operating system does it.
Cheers for this, I'm getting an error though.

Code: Select all

File "game/script.rpy", line 10: invalid syntax
    class day_time(object, start_time=0):

User avatar
zmook
Veteran
Posts: 421
Joined: Wed Aug 26, 2020 6:44 pm
Contact:

Re: Need help with time system

#4 Post by zmook »

D'oh. It should be:

Code: Select all

init python:
    class day_time:
        def __init__(self, start_time=0):
colin r
➔ if you're an artist and need a bit of help coding your game, feel free to send me a PM

Swinwappy
Newbie
Posts: 14
Joined: Mon Jun 01, 2020 3:52 am
Contact:

Re: Need help with time system

#5 Post by Swinwappy »

zmook wrote: Thu Jun 09, 2022 3:58 pm D'oh. It should be:

Code: Select all

init python:
    class day_time:
        def __init__(self, start_time=0):
I'm getting this now O.o

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "renpy/common/00start.rpy", line 189, in script
    python:
  File "renpy/common/00start.rpy", line 190, in <module>
    renpy.execute_default_statement(True)
  File "game/script.rpy", line 54, in set_default
    default clock = day_time()
  File "game/script.rpy", line 54, in <module>
    default clock = day_time()
  File "game/script.rpy", line 12, in __init__
    self.t = start_time   # count a constantly-increasing number of hours since the game began
NameError: global name 'self' is not defined

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "renpy/common/00start.rpy", line 189, in script
    python:
  File "renpy/ast.py", line 928, in execute
    renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
  File "renpy/python.py", line 2245, in py_exec_bytecode
    exec(bytecode, globals, locals)
  File "renpy/common/00start.rpy", line 190, in <module>
    renpy.execute_default_statement(True)
  File "renpy/exports.py", line 3753, in execute_default_statement
    i.set_default(start)
  File "game/script.rpy", line 54, in set_default
    default clock = day_time()
  File "renpy/python.py", line 2269, in py_eval_bytecode
    return eval(bytecode, globals, locals)
  File "game/script.rpy", line 54, in <module>
    default clock = day_time()
  File "game/script.rpy", line 12, in __init__
    self.t = start_time   # count a constantly-increasing number of hours since the game began
NameError: global name 'self' is not defined

User avatar
zmook
Veteran
Posts: 421
Joined: Wed Aug 26, 2020 6:44 pm
Contact:

Re: Need help with time system

#6 Post by zmook »

It seems like you must have a copy error somewhere? Could you post a copy of *your* version, starting from the 'init python' line?
colin r
➔ if you're an artist and need a bit of help coding your game, feel free to send me a PM

Swinwappy
Newbie
Posts: 14
Joined: Mon Jun 01, 2020 3:52 am
Contact:

Re: Need help with time system

#7 Post by Swinwappy »

zmook wrote: Thu Jun 09, 2022 4:24 pm It seems like you must have a copy error somewhere? Could you post a copy of *your* version, starting from the 'init python' line?
You're right. I edited the new lines you gave me wrong! Thanks for your help with this zmook!

Swinwappy
Newbie
Posts: 14
Joined: Mon Jun 01, 2020 3:52 am
Contact:

Re: Need help with time system

#8 Post by Swinwappy »

zmook wrote: Thu Jun 09, 2022 4:24 pm It seems like you must have a copy error somewhere? Could you post a copy of *your* version, starting from the 'init python' line?
Time's advancing rather weirdly now. I'm not too sure exactly what's causing this (time & day are at the top left):
https://www.youtube.com/watch?v=a8vMhi4 ... e=youtu.be

User avatar
zmook
Veteran
Posts: 421
Joined: Wed Aug 26, 2020 6:44 pm
Contact:

Re: Need help with time system

#9 Post by zmook »

Ah, lines 10-11 should be:

Code: Select all

        def day(self):
            return int(self.t/len(self.times)) + 1  
colin r
➔ if you're an artist and need a bit of help coding your game, feel free to send me a PM

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], LuckyT, Ocelot, Semrush [Bot]