Hours progresssing days help

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
sluggybunny
Newbie
Posts: 5
Joined: Sat Jul 15, 2017 6:46 pm
Contact:

Hours progresssing days help

#1 Post by sluggybunny »

Hello. I'm having a little issue with trying to create a day and time system for my game. I have tried a lot of code, lots of googling, and I'm practicing and reading alot of python resources... but this is stumping me so hard @_@ I essentially want a clock system with hours and minutes, and when the hours hit 24 (or midnight) it adds to a day counter, so it will switch to the next day (Adds +1 to day). I use a tuple for the days of the week, and then the variable is like, currentday = weekdays[day].

I know I can do the if == and else statements anytime the time changes but that seems so clunky, I'd prefer something automatic so I can have a more sandbox-y experience for my game.

I'm not really sure where to begin for this. I tried doing a class but it won't add or do anything with the day variable. Any help would be appreciated.

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2384
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: Hours progresssing days help

#2 Post by Ocelot »

An example class, which stores a timepoint with minute precision and converts it into days/hours/minutes/string representation of current weekday on demand:

Code: Select all

init python:
    class Clock:
        HOURS = 60
        DAYS = 24 * 60
        DAYS_PER_WEEK = 7

        weekdays = [ "monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday" ]

        def __init__(self, minutes = 0, hours = 0, days = 0):
            self.time = 0
            self.add_time(minutes, hours, days)

        def add_time(self, minutes = 0, hours = 0, days = 0):
                self.time += minutes
                self.time += hours * Clock.HOURS
                self.time += days * Clock.DAYS

        @property
        def minutes(self):
            return self.time % Clock.HOURS

        @property
        def hours(self):
            return (self.time % Clock.DAYS) / Clock.HOURS

        @property
        def day(self):
            return self.time / Clock.DAYS

        @property
        def weekday(self):
            return self.day % Clock.DAYS_PER_WEEK

        @property
        def weekday_str(self):
            return Clock.weekdays[self.weekday]


default clock = Clock(days=5, hours=8) # Skip 5 days and start at 8:00

label start:
    scene bg room
    $ clock.add_time(minutes = 30, hours = 4) # Skip 4 hours and 30 minutes
    "Current day: [clock.day], [clock.weekday_str], Time: [clock.hours]:[clock.minutes]"
    return
< < insert Rick Cook quote here > >

sluggybunny
Newbie
Posts: 5
Joined: Sat Jul 15, 2017 6:46 pm
Contact:

Re: Hours progresssing days help

#3 Post by sluggybunny »

Thank you for your reply! This works exactly (And better then what I had) how I needed it to :) And I will attempt to use this example to expand on it too, since I would like the days to progress into months, and months to have their own range of days (I managed to get the months in... XD it's a journey for sure) Thank you ^_^

uncoded
Regular
Posts: 27
Joined: Fri Apr 09, 2021 10:29 am
Contact:

Re: Hours progresssing days help

#4 Post by uncoded »

Are you using gregorian calendar, or a custom (fantasy) calendar ?
Even if you use a fantasy calendar, as long as your calendar works like ours (365.25 day, 30/31 days per month, yadda yadda), you can just increase your "day of year" counter, and use use Python's datetime module to make the week/months conversion for you.
Something like that :

Code: Select all

python:
    import datetime
    today = datetime.datetime(2021, 4, 23)
    days_since_1ad = today.toordinal()
    # what date will we be in 2 weeks (14 days) ?
    in_two_weeks_since_1ad = days_since_1ad + 14
    result = datetime.date.fromordinal(in_two_weeks_since_1ad)
Then you retrieve your year, month number, and day of the month from result. Then maybe use a list with (fantasy) months or days of week names.

In this example, result is a date that allows your to use hours, minutes and seconds, but I kept those out for the sake of readability. It works essentyally the same way.
You can have a look at the documentation of datetime if you're interested.

If your calendar has a different number of days in year and/or a different months "rythm", it'd work differently. As a start you could use Ocelot's excellent snippet and tweak the constants.
🐾

sluggybunny
Newbie
Posts: 5
Joined: Sat Jul 15, 2017 6:46 pm
Contact:

Re: Hours progresssing days help [solved]

#5 Post by sluggybunny »

The datetime module seems really useful and exactly what I'd like, but I can't figure out how to create a function to advance time with it? @__@ I've tried several different things... I'd just like to create a function so that i can progress time like $advance(30) so it will progress time by 30 seconds. But whatever I try to do, it doesn't seem to agree with ren'py.

edit: nevermind, i figured it out lol. I should nap before I attempt coding.

Post Reply

Who is online

Users browsing this forum: No registered users