Real time feature 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
User avatar
inkbrush
Regular
Posts: 60
Joined: Tue Jul 08, 2014 3:28 am
Contact:

Real time feature help?

#1 Post by inkbrush »

Hi Lemmasoftians! (You may have noticed me already in this thread but I have another issue so I thought I'd make a different thread because of the different nature)


I'm trying to add a semi-real time feature into my game. I want to have it where the player gets more points/money to spend every day. So, obviously you need the game to be keeping track of what the time is so if the player opens the game a new day, they get their stuff.

Code: Select all

init python:
    import datetime
    today = datetime.date.today()

label start:
    "Today is [today]"
I put this code into my game to see how it worked and it does and that's awesome--But I can't figure out how to get the time to appear, instead of the date. I tried getting information on Python and searching through google and the forums for an answer but I could not find one, unfortunately. So, what would the code be for putting in the time instead of the date?


Also, while I'm on the topic . . . How would I implant the system of adding points at the player's midnight? (12:00am) And how do I make sure that they don't get more points if they re-open the game?


Thank you!!

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: Real time feature help?

#2 Post by trooper6 »

You might want to check out my clock tutorial in my signature. One of the clock's modes is a real time mode and you can see how I get time from that.
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

User avatar
Zetsubou
Miko-Class Veteran
Posts: 522
Joined: Wed Mar 05, 2014 1:00 am
Completed: See my signature
Github: koroshiya
itch: zetsuboushita
Contact:

Re: Real time feature help?

#3 Post by Zetsubou »

Rather than datetime.date, you could use datetime.datetime.
https://docs.python.org/2/library/datet ... e.datetime

Code: Select all

datetime.date.today() #2015-09-05
datetime.datetime.today() #2015-09-05 10:03:40.449852
You'd likely want to format it though.

Code: Select all

label start:
    $ now = datetime.datetime.today()
    $ time = now.hour + ':' + now.minute
    "The time is [time]" #The time is 10:03
In regards to adding points, I'd do a check in init.

Code: Select all

init python:
    import datetime
    today = datetime.date.today()

    if persistent.yesterday and persistent.yesterday.timetuple().tm_yday != today.timetuple().tm_yday:
        pass #Give the player some point/money

    persistent.yesterday = today
The above stores the last date/time you started the game, then compares the day of year.
Minor issue is that we're going by day of year, so if exactly one year passed (ignoring leap days) without the player opening the game, they wouldn't get anything.
Finished games
-My games: Sickness, Wander No More, Max Massacre, Humanity Must Perish, Tomboys Need Love Too, Sable's Grimoire, My Heart Grows Fonder, Man And Elf, A Dragon's Treasure, An Adventurer's Gallantry
-Commissions: No One But You, Written In The Sky, Diamond Rose, To Libertad, Catch Canvas, Love Ribbon, Happy Campers, Wolf Tails

Working on:
Sable's Grimoire 2

https://zetsubou.games

User avatar
inkbrush
Regular
Posts: 60
Joined: Tue Jul 08, 2014 3:28 am
Contact:

Re: Real time feature help?

#4 Post by inkbrush »

Alright I inserted the code into my script but it keeps giving me an error with the "$ time".

Here's what I have put in:

Code: Select all

init python:
    import datetime
    time = datetime.datetime.today()


# The game starts here.
label start:

    $ now = datetime.datetime.today()
    $ time = now.hour + ':' + now.minute
    
    e "The time is [time]"

User avatar
Zetsubou
Miko-Class Veteran
Posts: 522
Joined: Wed Mar 05, 2014 1:00 am
Completed: See my signature
Github: koroshiya
itch: zetsuboushita
Contact:

Re: Real time feature help?

#5 Post by Zetsubou »

"time" is likely a reserved word. Try something slightly different.
"cTime = datetime.datetime.today()"
Also, you'll want to use a different variable for the one in init python than in your start label.
Finished games
-My games: Sickness, Wander No More, Max Massacre, Humanity Must Perish, Tomboys Need Love Too, Sable's Grimoire, My Heart Grows Fonder, Man And Elf, A Dragon's Treasure, An Adventurer's Gallantry
-Commissions: No One But You, Written In The Sky, Diamond Rose, To Libertad, Catch Canvas, Love Ribbon, Happy Campers, Wolf Tails

Working on:
Sable's Grimoire 2

https://zetsubou.games

User avatar
inkbrush
Regular
Posts: 60
Joined: Tue Jul 08, 2014 3:28 am
Contact:

Re: Real time feature help?

#6 Post by inkbrush »

Okay, I tried that but it's still not working. The error is telling me:

"TypeError: unsupported operand type(s) for +: 'int' and 'str'"

User avatar
Zetsubou
Miko-Class Veteran
Posts: 522
Joined: Wed Mar 05, 2014 1:00 am
Completed: See my signature
Github: koroshiya
itch: zetsuboushita
Contact:

Re: Real time feature help?

#7 Post by Zetsubou »

Oh, that would be:

Code: Select all

str(now.hour) + ':' + str(now.minute)
Finished games
-My games: Sickness, Wander No More, Max Massacre, Humanity Must Perish, Tomboys Need Love Too, Sable's Grimoire, My Heart Grows Fonder, Man And Elf, A Dragon's Treasure, An Adventurer's Gallantry
-Commissions: No One But You, Written In The Sky, Diamond Rose, To Libertad, Catch Canvas, Love Ribbon, Happy Campers, Wolf Tails

Working on:
Sable's Grimoire 2

https://zetsubou.games

User avatar
inkbrush
Regular
Posts: 60
Joined: Tue Jul 08, 2014 3:28 am
Contact:

Re: Real time feature help?

#8 Post by inkbrush »

Awesome! It works now--Thank you!!

Though, when I tested it, it put "20:5" instead of "20:05". How would I fix this?

User avatar
Zetsubou
Miko-Class Veteran
Posts: 522
Joined: Wed Mar 05, 2014 1:00 am
Completed: See my signature
Github: koroshiya
itch: zetsuboushita
Contact:

Re: Real time feature help?

#9 Post by Zetsubou »

You could zero-pad the numbers like so:

Code: Select all

format(now.hour, "02d") + ':' + format(now.minute, "02d")
Finished games
-My games: Sickness, Wander No More, Max Massacre, Humanity Must Perish, Tomboys Need Love Too, Sable's Grimoire, My Heart Grows Fonder, Man And Elf, A Dragon's Treasure, An Adventurer's Gallantry
-Commissions: No One But You, Written In The Sky, Diamond Rose, To Libertad, Catch Canvas, Love Ribbon, Happy Campers, Wolf Tails

Working on:
Sable's Grimoire 2

https://zetsubou.games

User avatar
inkbrush
Regular
Posts: 60
Joined: Tue Jul 08, 2014 3:28 am
Contact:

Re: Real time feature help?

#10 Post by inkbrush »

Awesome! That's all, thank you!!

User avatar
PyTom
Ren'Py Creator
Posts: 16088
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Real time feature help?

#11 Post by PyTom »

Just so people know, the easy way to format things is to assign the datetime to a variable, and use Ren'Py/Python's string formatting, which supports fields.

Code: Select all

$ now = datetime.datetime.now()
"At the tone, the time will be [now.hour:02d]:[now.minute:02d]. Beep!"
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

Post Reply

Who is online

Users browsing this forum: Semrush [Bot]