Strftime and conditionals for a calendar [Solved]

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
Clareity
Newbie
Posts: 2
Joined: Wed Aug 28, 2019 10:47 am
Contact:

Strftime and conditionals for a calendar [Solved]

#1 Post by Clareity »

I got the calendar code from the renpy cookbook and played around with it a bit:

Code: Select all

init python:

    import datetime

    class GameTime:

        def __init__(self, dt="01 Jan 2018"):
            self._dt = datetime.datetime.strptime( dt, "%d %b %Y" )

        def alter(self, **kwargs):
            self._dt += datetime.timedelta( **kwargs )

        def __repr__(self):
            return _strftime("%A, %d %b %Y %H:%M", self._dt.timetuple())

        @property
        def tod(self): #tod = time of day
            return [ k[-1] for k in (
                (0,1,2,3,4,5,6, "Night Time"),
                (7,8,9,10,11, "Morning"),
                (12, "Noon"),
                (13,14,15,16,17, "Afternoon"),
                (18,19,20, "Evening"),
                (21,22,23, "Late Evening") ) if self._dt.hour in k ][0]


So I added a section into my game to check if it's "night time"

Code: Select all

if gt.tod == "Night Time":
        "watch out for ghosts!"
I want the game to also print a line if it's the weekend. How do I define a property to check if it's the weekend or not?

I tried this:

Code: Select all

@property
        def daytype(self):
            return _strftime("%A", self._dt.timetuple())
Followed up by

Code: Select all

if gt.daytype == "Saturday" or "Sunday":
        "It's the weekend!"
But it just ends up printing "It's the weekend!" regardless of the day. Any help?
Last edited by Clareity on Thu Aug 29, 2019 7:46 am, edited 1 time in total.

User avatar
isobellesophia
Miko-Class Veteran
Posts: 979
Joined: Mon Jan 07, 2019 2:55 am
Completed: None
Projects: Maddox and Friends! (AI Teacher friend), Friendly Universities! (Soon)
Organization: Friendly Teachers series
Deviantart: SophBelle
itch: Child Creation
Location: Philippines, Mindanao
Contact:

Re: Strftime and conditionals for a calendar

#2 Post by isobellesophia »

Clareity wrote: Wed Aug 28, 2019 11:29 am I got the calendar code from the renpy cookbook and played around with it a bit:

init python:

import datetime

class GameTime:

def __init__(self, dt="01 Jan 2018"):
self._dt = datetime.datetime.strptime( dt, "%d %b %Y" )

def alter(self, **kwargs):
self._dt += datetime.timedelta( **kwargs )

def __repr__(self):
return _strftime("%A, %d %b %Y %H:%M", self._dt.timetuple())

@property
def tod(self): #tod = time of day
return [ k[-1] for k in (
(0,1,2,3,4,5,6, "Night Time"),
(7,8,9,10,11, "Morning"),
(12, "Noon"),
(13,14,15,16,17, "Afternoon"),
(18,19,20, "Evening"),
(21,22,23, "Late Evening") ) if self._dt.hour in k ][0]



So I added a section into my game to check if it's "night time"

if gt.tod == "Night Time":
"watch out for ghosts!"


I want the game to also print a line if it's the weekend. How do I define a property to check if it's the weekend or not?

I tried this:

@property
def daytype(self):
return _strftime("%A", self._dt.timetuple())


Followed up by

if gt.daytype == "Saturday" or "Sunday":
"It's the weekend!"


But it just ends up printing "It's the weekend!" regardless of the day. Any help?

You can use elif.

https://www.renpy.org/doc/html/conditio ... -statement

Like..

Code: Select all

if gt.daytype == "Saturday":
        "It's the weekend!"
        
        elif gt.daytype == "Sunday":
        "Its the weekend!"
        
        elif gt.tod == "Night Time":
        "watch out for ghosts!" 
Try if it works, since i am not best or good at coding.. so..
I am a friendly user, please respect and have a good day.


Image

Image


User avatar
isobellesophia
Miko-Class Veteran
Posts: 979
Joined: Mon Jan 07, 2019 2:55 am
Completed: None
Projects: Maddox and Friends! (AI Teacher friend), Friendly Universities! (Soon)
Organization: Friendly Teachers series
Deviantart: SophBelle
itch: Child Creation
Location: Philippines, Mindanao
Contact:

Re: Strftime and conditionals for a calendar

#3 Post by isobellesophia »

I dont think if it is a games date or the real time function?If real time, then i had a example.
I am a friendly user, please respect and have a good day.


Image

Image


Clareity
Newbie
Posts: 2
Joined: Wed Aug 28, 2019 10:47 am
Contact:

Re: Strftime and conditionals for a calendar

#4 Post by Clareity »

Code: Select all

init python:

    import datetime

    class GameTime:

        def __init__(self, dt="01 Jan 2018"):
            self._dt = datetime.datetime.strptime( dt, "%d %b %Y" )

        def alter(self, **kwargs):
            self._dt += datetime.timedelta( **kwargs )

        def __repr__(self):
            return _strftime("%A, %d %b %Y %H:%M", self._dt.timetuple())

        @property
        def tod(self): #tod = time of day
            return [ k[-1] for k in (
                (0,1,2,3,4,5,6, "Night Time"),
                (7,8,9,10,11, "Morning"),
                (12, "Noon"),
                (13,14,15,16,17, "Afternoon"),
                (18,19,20, "Evening"),
                (21,22,23, "Late Evening") ) if self._dt.hour in k ][0]

        @property
        def daytype(self):
            return _strftime("%A", self._dt.timetuple())

default gt = GameTime("3 Jun 2019")   

label start:

    show screen calendar

    "Next up is the menu where we can mess around with the date and time!"

label timechange:

    if gt.tod == "Night Time":
        "watch out for ghosts!"

    if gt.daytype == "Saturday" or "Sunday":
        "It's the weekend!"

    "It is [gt] ( [gt.tod] )"
These are the relevant sections of my code, and basically the problem is the

Code: Select all

   @property
        def daytype(self):
            return _strftime("%A", self._dt.timetuple())
I'm not sure why when i set gt.daytype == "Saturday" or "Sunday":

It doesn't seem to care what the "%A" print is, it just automatically sees that Saturday or Sunday is part of the list and prints the text?

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: Strftime and conditionals for a calendar

#5 Post by Remix »

Likely best to use "%w" for testing the weekday (0 is Sunday, 1 is Monday etc, 6 is Saturday) as it is locale independent.

Code: Select all

        @property
        def daytype(self):
            return "Weekday" if 0 < _strftime("%w", self._dt.timetuple()) < 6 else "Weekend"
Should work
Frameworks & Scriptlets:

Post Reply

Who is online

Users browsing this forum: Google [Bot]