Page 1 of 1

{Solved} Clock loop isn't working, anyone know why?

Posted: Sun Sep 15, 2013 5:52 pm
by Verstehen
I'm sorry, but an uncaught exception occurred.

While running game code:
File "game/Local1.rpy", line 552, in script (<- This was the script that was calling for the stat to be shown.)
File "game/Stats.rpy", line 795, in python
KeyError: 'clock'

Code: Select all

    if minutes > 1380:
        $ minutes = 0
    elif minutes == 0:
        $ clock = "12:00 AM"
    elif minutes == 60:
        $ clock = "1:00 AM"
    elif minutes == 120:
        $ clock = "2:00 AM"
    elif minutes == 180:
        $ clock = "3:00 AM"
    elif minutes == 240:
        $ clock = "4:00 AM"
    elif minutes == 300:
        $ clock = "5:00 AM"
    elif minutes == 360:
        $ clock = "6:00 AM"
    elif minutes == 420:
        $ clock = "7:00 AM"
    elif minutes == 480:
        $ clock = "8:00 AM"
    elif minutes == 540:
        $ clock = "9:00 AM"
    elif minutes == 600:
        $ clock = "10:00 AM"
    elif minutes == 660:
        $ clock = "11:00 AM"
    elif minutes == 720:
        $ clock = "12:00 PM"
    elif minutes == 780:
        $ clock = "1:00 PM"
    elif minutes == 840:
        $ clock = "2:00 PM"
    elif minutes == 900:
        $ clock = "3:00 PM"
    elif minutes == 960:
        $ clock = "4:00 PM"
    elif minutes == 1020:
        $ clock = "5:00 PM"
    elif minutes == 1080:
        $ clock = "6:00 PM"
    elif minutes == 1140:
        $ clock = "7:00 PM"
    elif minutes == 1200:
        $ clock = "8:00 PM"
    elif minutes == 1260:
        $ clock = "9:00 PM"
    elif minutes == 1320:
        $ clock = "10:00 PM"
    elif minutes == 1380:
        $ clock = "11:00 PM"
And the call;

Code: Select all

    text "[clock]" xalign 0.035 yalign 0.20 size 19 
When clicking on an event this is executed :

Code: Select all

        $ minutes += 60

The script is suppose to loop at

Code: Select all

if minutes > 1380:
        $ minutes = 0
but instead it just crashes after 11:00 PM

Re: Clock loop isn't working, anyone know why?

Posted: Sun Sep 15, 2013 7:09 pm
by PyTom
If minutes is > 1380, then minutes is changed, but clock is not set. You probably want:

Code: Select all

    if minutes > 1380:
        $ minutes = 0
    
    if minutes == 0:
        $ clock = "12:00 AM"
    elif minutes == 60:
        $ clock = "1:00 AM"
    elif minutes == 120:
        ...
Although I'd think code like:

Code: Select all

python:
    minutes = minutes % 1440
    hour = minutes // 60 % 12
    if hour == 0:
        hour = 12

    if minutes < 720:
        clock = "{}:{:02d} AM".format(hour, minutes % 60)
    else:
        clock = "{}:{:02d} PM".format(hour, minutes % 60)
is cleaner.

Re: Clock loop isn't working, anyone know why?

Posted: Sun Sep 15, 2013 7:24 pm
by Verstehen
Thanks a ton. It's working now. Still a bit new with python and it's taking me a bit to get use to this.