Page 1 of 1

Real time feature help?

Posted: Fri Sep 04, 2015 7:28 pm
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!!

Re: Real time feature help?

Posted: Fri Sep 04, 2015 8:10 pm
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.

Re: Real time feature help?

Posted: Fri Sep 04, 2015 8:17 pm
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.

Re: Real time feature help?

Posted: Fri Sep 04, 2015 8:41 pm
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]"

Re: Real time feature help?

Posted: Fri Sep 04, 2015 8:45 pm
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.

Re: Real time feature help?

Posted: Fri Sep 04, 2015 9:47 pm
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'"

Re: Real time feature help?

Posted: Fri Sep 04, 2015 9:53 pm
by Zetsubou
Oh, that would be:

Code: Select all

str(now.hour) + ':' + str(now.minute)

Re: Real time feature help?

Posted: Fri Sep 04, 2015 11:08 pm
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?

Re: Real time feature help?

Posted: Fri Sep 04, 2015 11:12 pm
by Zetsubou
You could zero-pad the numbers like so:

Code: Select all

format(now.hour, "02d") + ':' + format(now.minute, "02d")

Re: Real time feature help?

Posted: Sat Sep 05, 2015 12:11 am
by inkbrush
Awesome! That's all, thank you!!

Re: Real time feature help?

Posted: Sat Sep 05, 2015 12:14 am
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!"