Clock + Calendar counter

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
Loopaz
Newbie
Posts: 13
Joined: Thu Apr 12, 2018 4:03 am
Completed: ..asking still the same questions!
Projects: Your choice VN
Location: Poland
Contact:

Clock + Calendar counter

#1 Post by Loopaz »

Hello
I started writing a VN and i need more help because i don't know how to bite this.
How to make clock which will move further with time only when player do something like

Code: Select all

label player_wake:
	$ minutes +5
	"Time to wake up"
and calendar implemented to this counting clock every time move foward when is 24h gone like

Code: Select all

if hours = 24:
	$ day +1
wish to have a lot of time like whole year ;P
could you best people show me how to start with it?
I tried to lern some but there are only caledars using real date not a game time progress
"I know that I know nothing" :shock:
"Who ask not stray" :roll:
Bad english talker :|
I don't expect miracle. I expect profesionals :3
or do something right or do not do it at all

kivik
Miko-Class Veteran
Posts: 786
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

Re: Clock + Calendar counter

#2 Post by kivik »

We probably need a bit more context to know how best to do this - e.g. how are you displaying time? How do you store the time? Do you have a 24hour cycling? Or does the day end at a certain time such as midnight?

The fact that you have 24 makes me think you're going for 24 hour cycle. The fact that you have minutes and hours as variables makes me think you've already got code to increment the hour when minutes hits 60.

If you haven't got that part and you're just trying to ask broadly, I've mocked up a simple clock object below. I did some quick testing and it seems to work - you can add minutes, hours and days to it. It basically uses the principle of floor division and remainders to figure out how to increment hours and days. Please read the comments in the code for more information.

If you want to have things like month, year, weekday etc, you'll need to add to the class. I actually mocked up some code for weekdays, months and year in another thread here about day night cycles.

I think it's a good idea to use a time object to keep time in general, as you can make it output anything you need it to.

Code: Select all

init python:
    class clock(object):
        def __init__(self, day = 1, hour = 8, minute = 0):
            self._day = day
            self._hour = hour
            self._minute = minute

        def addtime(self, minutes, hours = 0, days = 0): # use this to advance time
            self._minute += minutes # add minutes
            self._hour += hours # add hours
            self._day += days

            # add hours by floor division minute over 60
            self._hour += self._minute // 60

            # add days by floor division hour over 24 - make sure to do this after hour's been added
            self._day += self._hour // 24

            # now we can clean up minutes and hours by using mod %
            self._minute = self._minute % 60
            self._hour = self._hour % 24

        # use property to return formatted hour and minutes
        @property
        def day(self): # day as int
            return self._day

        @property
        def hour(self): # hour as int
            return self._hour

        @property
        def hh(self): # hours as HH. I'm not familiar enough with python to know if there's a shorter way to do this
            hour = "0" + str(self._hour)
            return hour[-2:]

        @property
        def minute(self): # minute as int
            return self._minute

        @property
        def mm(self): # minutes as mm.
            minute = "0" + str(self._minute)
            return minute[-2:]

default clock = clock()

label start:
    while True:
        "Day: [clock.day] Time: [clock.hh]:[clock.mm]"
        $ clock.addtime(15) # add 15 minutes
    return

User avatar
Loopaz
Newbie
Posts: 13
Joined: Thu Apr 12, 2018 4:03 am
Completed: ..asking still the same questions!
Projects: Your choice VN
Location: Poland
Contact:

Re: Clock + Calendar counter

#3 Post by Loopaz »

Wow!
First of all
It is black magic for me
But
I go after logic and

Code: Select all

init python:
    class clock(object):
        def __init__(self, century = 19, year = 1999, month = 1, day = 1, hour = 8, minute = 0):
            self._century = century
            self._year = year
            self._month = month
            self._day = day
            self._hour = hour
            self._minute = minute

        def addtime(self, minutes, hours = 0, days = 0, months = 0, years = 0, centuries = 0): 
            self._minute += minutes # add minutes
            self._hour += hours # add hours
            self._day += days
            self._month += months
            self._year += years
            self._century += centuries

            # add hours by floor division minute over 60
            self._hour += self._minute // 60

            # add days by floor division hour over 24 - make sure to do this after hour's been added
            self._day += self._hour // 24
            
            self._month += self._day // 31 # Q Ok how to ? 
            
            self._year += self._month // 12 # Q 372 days
            
            self._century += self._year // 100

            # now we can clean up minutes and hours by using mod %
            self._minute = self._minute % 60
            self._hour = self._hour % 24
            self._day = self._day % 31  
            self._month = self._month % 12
            self._year = self._year % 372 # Q how to 365? and 366?
            

        # use property to return formatted hour and minutes
        
        
        @property
        def century(self): # day as int
            return self._century
        
        @property
        def year(self): # day as int
            return self._year
        
        @property
        def yy(self): # minutes as mm.
            year = "0" + str(self._year)
            return year[-2:]
        
        @property
        def month(self): # day as int
            return self._month
        
        @property
        def mn(self): # minutes as mm.
            month = "0" + str(self._month)
            return month[-2:]
            
        @property
        def day(self): # day as int
            return self._day
        
        @property
        def dd(self): # minutes as mm.
            day = "0" + str(self._day)
            return day[-2:]
            
        @property
        def hour(self): # hour as int
            return self._hour

        @property
        def hh(self): # hours as HH. I'm not familiar enough with python to know if there's a shorter way to do this
            hour = "0" + str(self._hour)
            return hour[-2:] # Q what is doing this 2?

        @property
        def minute(self): # minute as int
            return self._minute

        @property
        def mm(self): # minutes as mm.
            minute = "0" + str(self._minute)
            return minute[-2:]

default clock = clock()

label start:
    while True:
        "Day: [clock.day] Time: [clock.hh]:[clock.mm]"
        $ clock.addtime(15) # Q   how to able add for ex months? Count? 60x24x31~~44640
        "Date: [clock.dd]/[clock.mn]/[clock.yy]  Time: [clock.hh]:[clock.mm]"
        & clock.addtime(44640) # Here Error why?
        "Blow mind"
    return
1. Why i have error in 98?
2. How to make different days on different months
3. How to make 366 days every 4 years
4. How to name days and months?
5. For what is -2 in returns?
6. And the best. How to implement this to Event Handler of Remix

My questions looks like hard work
I understand that i don't understand
"I know that I know nothing" :shock:
"Who ask not stray" :roll:
Bad english talker :|
I don't expect miracle. I expect profesionals :3
or do something right or do not do it at all

kivik
Miko-Class Veteran
Posts: 786
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

Re: Clock + Calendar counter

#4 Post by kivik »

1. Why i have error in 98?

If it's line 98 above, you've used & instead of $ so I'm guessing that threw some weird error?

2. How to make different days on different months
3. How to make 366 days every 4 years
4. How to name days and months?

Ok, looks like you're actually doing a full on calendar - you may actually want to use python's datetime object for this. I can recreate a datetime object probably, but that doesn't really make sense if you're basically recreating a full on datetime system. Python's own datetime library should have everything you need and they'll be more efficient I'm sure :)

Have a look at this: https://docs.python.org/2/library/datetime.html

It's a lot to take it (since there's so much more you can do with datetime), but that page should have everything you need to handle real date and time including calculations, leap years, days in months, date time formats etc.

Also I just re-read and saw that you didn't want real time - but the python datetime allows you to create your own date and time on creation, and you can just add time / days / months / years to the datetime object to move it forward.

5. For what is -2 in returns?

I will answer this just to explain what's happening. My clock object stores minutes and hours as integers so I can math with them :) Unfortunately if I were to display them, they won't have leading zeros: 8:1 instead of 08:01 for example. So I first convert it to string and add a leading zero:

Code: Select all

hour = "0" + str(self._hour)
So now all the possible outputs are: 00,01,02,03,04,05,06,07,08,09,010,011,012,013,014,015,016,017,018,019,020,021,022,023. Well we only need the last two digits, so I looked up how to pull the 2 right most characters, and it's string_variable[n:] n is how many characters to bring back. If n is positive, it starts from the left; if n is negative, it starts from the right. so [-2:] brings back 2 characters from the right.

I don't know if there's a python shorthand to condense everything into a single statement so that I can return it without setting a temporary local variable to do it, that's why it requires two lines. I tried return "0{self.hour}"[-2:] which returned "r}" so I guess that doesn't work :(

6. And the best. How to implement this to Event Handler of Remix

No idea what this is? Can you link?

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: Clock + Calendar counter

#5 Post by Remix »

kivik wrote: Fri Apr 13, 2018 7:07 pm 6. And the best. How to implement this to Event Handler of Remix

No idea what this is? Can you link?
*cough* This old code snippet rascal

The simplest answer is... when defining months, define more than 12, basically January 1742.... December 1742... Jan 1743... etc... or hack the internal methods and add a modulo year
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: Clock + Calendar counter

#6 Post by Remix »

kivik wrote: Fri Apr 13, 2018 7:07 pm

Code: Select all

hour = "0" + str(self._hour)
Your hour[-2:] thing should work, though you might like to know that Ren'py square brace [] interpolation supports Python formatting (to a degree), so:

Code: Select all

    "[hour:02d]" # should zero pad hour to force 2 digits if wanted
Frameworks & Scriptlets:

kivik
Miko-Class Veteran
Posts: 786
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

Re: Clock + Calendar counter

#7 Post by kivik »

Remix wrote: Fri Apr 13, 2018 11:31 pm
kivik wrote: Fri Apr 13, 2018 7:07 pm 6. And the best. How to implement this to Event Handler of Remix

No idea what this is? Can you link?
*cough* This old code snippet rascal

The simplest answer is... when defining months, define more than 12, basically January 1742.... December 1742... Jan 1743... etc... or hack the internal methods and add a modulo year
Oh my god, why did I even make my code when there's a full on library already?! Impressive library btw Remix!!

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: Clock + Calendar counter

#8 Post by Remix »

kivik wrote: Sat Apr 14, 2018 4:55 am Oh my god, why did I even make my code when there's a full on library already?! Impressive library btw Remix!!
Thanks. There's just so much that needs improving on it though and not much motivation here.
Feel free to start on the todo: list if you like, lol.
Frameworks & Scriptlets:

kivik
Miko-Class Veteran
Posts: 786
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

Re: Clock + Calendar counter

#9 Post by kivik »

Remix wrote: Sat Apr 14, 2018 5:51 am
kivik wrote: Sat Apr 14, 2018 4:55 am Oh my god, why did I even make my code when there's a full on library already?! Impressive library btw Remix!!
Thanks. There's just so much that needs improving on it though and not much motivation here.
Feel free to start on the todo: list if you like, lol.
Haha, wish I had the time. Mind you, I shouldn't even be browsing the forums, but it's actually helping me learn more about Ren'py!

User avatar
Loopaz
Newbie
Posts: 13
Joined: Thu Apr 12, 2018 4:03 am
Completed: ..asking still the same questions!
Projects: Your choice VN
Location: Poland
Contact:

Re: Clock + Calendar counter

#10 Post by Loopaz »

Ok.
First of all.
I started to learn python
Second
For now it is to hard to understand what is going on and how to use provided data to implement on other code.
Third
That's great that gentleman meet each other :D

I will look for something what is easy to read
Thanks for try
U get my credits :)
Sincerly, Loopaz

:edit
Anyway i just realize what is wrong with year that should be -4 in return for year to correctly(almost) print
"I know that I know nothing" :shock:
"Who ask not stray" :roll:
Bad english talker :|
I don't expect miracle. I expect profesionals :3
or do something right or do not do it at all

Post Reply

Who is online

Users browsing this forum: Majestic-12 [Bot], piinkpuddiin, snotwurm