[Solved] Coding time based events based on clock

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
Senpai
Newbie
Posts: 18
Joined: Thu Jul 21, 2016 4:54 pm
Contact:

[Solved] Coding time based events based on clock

#1 Post by Senpai » Mon Aug 01, 2016 7:43 pm

Hey, I'm trying to code some time based events (i.e. character A will be at that place from 7:00 AM to 4:00 PM). This is the clock I am using:

Code: Select all

    $ minutes = time_in_minutes % 60
    $ hours = (time_in_minutes / 60) % 24 
    $ day = (time_in_minutes / 60 / 24) % 28
    $ month = (time_in_minutes / 60/ 24 / 28) % 12
    text ("%d:%02d" % (hours, minutes)) pos (715, 24) color "#ffffff" size 30
    text ("Day %d" % (day)) pos (15, 24) color "#ffffff" size 30
So, when I tried to use hours as a definition for my conditional, it didn't work, it looked like this:

Code: Select all

label classroomA:

    $ time_in_minutes += 1
    if hours >= 7 and hours <= 16:
        "From 7 AM to 4 PM"
        call screen classroomA
    else:
        call screen classroomA
But when I tried with this, it works:

Code: Select all

label classroomA:

    $ time_in_minutes += 1
    if time_in_minutes >= 1860 and time_in_minutes <= 2401:
        "From 7 AM to 4 PM"
        call screen classroomA
    else:
        call screen classroomA
The problem is that time_in_minutes is always increasing, I need to be able to use a stable definition like "hours" that only goes up to 24.
Last edited by Senpai on Tue Aug 02, 2016 10:27 pm, edited 2 times in total.

User avatar
gas
Miko-Class Veteran
Posts: 838
Joined: Mon Jan 26, 2009 7:21 pm
Contact:

Re: Coding time based events based on clock

#2 Post by gas » Mon Aug 01, 2016 9:45 pm

Of course it doesn't work.
Variable definitions are not retroactives.

So, if you increase time_in_minutes +1, only this variable increase, not any other one.
You had to call a function or do instructions that update all other values (like hours and days), THEN do those checks.
If you want to debate on a reply I gave to your posts, please QUOTE ME or i'll not be notified about. << now red so probably you'll see it.

10 ? "RENPY"
20 GOTO 10

RUN

Senpai
Newbie
Posts: 18
Joined: Thu Jul 21, 2016 4:54 pm
Contact:

Re: Coding time based events based on clock

#3 Post by Senpai » Mon Aug 01, 2016 10:00 pm

gas wrote:Of course it doesn't work.
Variable definitions are not retroactives.

So, if you increase time_in_minutes +1, only this variable increase, not any other one.
You had to call a function or do instructions that update all other values (like hours and days), THEN do those checks.
Odd, I thought that since

Code: Select all

hours = (time_in_minutes / 60) % 24
that it would be fine. Is there something I need to change in the clock's code in order to be able to use hours? Or is there something I'm missing?

User avatar
Iylae
Regular
Posts: 73
Joined: Sat Jan 09, 2016 6:57 am
Location: Cornwall, UK
Contact:

Re: Coding time based events based on clock

#4 Post by Iylae » Tue Aug 02, 2016 6:01 am

Senpai wrote:

Code: Select all

label classroomA:

    $ time_in_minutes += 1
    if hours >= 7 and hours <= 16:
        "From 7 AM to 4 PM"
        call screen classroomA
    else:
        call screen classroomA
The reason it's not working, as gas alluded to, is when you're altering the time_in_minutes, you're not re-calculating the other variables (hours , day, month).

A simple way of doing this is simply copying the calculation lines from your earlier code, but you'll need to do this every time you change the time_in_minutes.
Senpai wrote:

Code: Select all

label classroomA:

    $ time_in_minutes += 1
    $ minutes = time_in_minutes % 60
    $ hours = (time_in_minutes / 60) % 24 
    $ day = (time_in_minutes / 60 / 24) % 28
    $ month = (time_in_minutes / 60/ 24 / 28) % 12
    if hours >= 7 and hours <= 16:
        "From 7 AM to 4 PM"
        call screen classroomA
    else:
        call screen classroomA
P.S. You should probably convert this to a function to save you peace of mind... but that's for you to decide.
Image
  If we are what we repeatedly do, then good coding is not an act, but a habit

Senpai
Newbie
Posts: 18
Joined: Thu Jul 21, 2016 4:54 pm
Contact:

Re: Coding time based events based on clock

#5 Post by Senpai » Tue Aug 02, 2016 11:18 am

Iylae wrote:
Senpai wrote:

Code: Select all

label classroomA:

    $ time_in_minutes += 1
    if hours >= 7 and hours <= 16:
        "From 7 AM to 4 PM"
        call screen classroomA
    else:
        call screen classroomA
The reason it's not working, as gas alluded to, is when you're altering the time_in_minutes, you're not re-calculating the other variables (hours , day, month).

A simple way of doing this is simply copying the calculation lines from your earlier code, but you'll need to do this every time you change the time_in_minutes.
Senpai wrote:

Code: Select all

label classroomA:

    $ time_in_minutes += 1
    $ minutes = time_in_minutes % 60
    $ hours = (time_in_minutes / 60) % 24 
    $ day = (time_in_minutes / 60 / 24) % 28
    $ month = (time_in_minutes / 60/ 24 / 28) % 12
    if hours >= 7 and hours <= 16:
        "From 7 AM to 4 PM"
        call screen classroomA
    else:
        call screen classroomA
P.S. You should probably convert this to a function to save you peace of mind... but that's for you to decide.
Oh, so that is what he meant by updating it, it's working now. Thanks a lot.

And sorry, I'm still very new to coding, so I don't even know what converting something to a function means =/

User avatar
Iylae
Regular
Posts: 73
Joined: Sat Jan 09, 2016 6:57 am
Location: Cornwall, UK
Contact:

Re: Coding time based events based on clock

#6 Post by Iylae » Tue Aug 02, 2016 6:19 pm

I assumed as much, which is why I didn't explain. I'll not go into an explanation here because it's relatively useless without more basic programming knowledge.

If you keep finding yourself stuck on similar topics, then it may be good to take a break from your game and learn some basic python instead. (Python is the language you use when your line starts with a $ or an indent starts with python:

There's plenty of websites out there that can teach you how to code using Python, I recently refreshed myself through http://learnpythonthehardway.org/book/ but you don't have to worry about setting up a python environment on your computer - you already have one in Ren'Py.
Image
  If we are what we repeatedly do, then good coding is not an act, but a habit

Senpai
Newbie
Posts: 18
Joined: Thu Jul 21, 2016 4:54 pm
Contact:

Re: Coding time based events based on clock

#7 Post by Senpai » Tue Aug 02, 2016 10:27 pm

Iylae wrote:I assumed as much, which is why I didn't explain. I'll not go into an explanation here because it's relatively useless without more basic programming knowledge.

If you keep finding yourself stuck on similar topics, then it may be good to take a break from your game and learn some basic python instead. (Python is the language you use when your line starts with a $ or an indent starts with python:

There's plenty of websites out there that can teach you how to code using Python, I recently refreshed myself through http://learnpythonthehardway.org/book/ but you don't have to worry about setting up a python environment on your computer - you already have one in Ren'Py.
I see, for now my game is coming along just fine, there are only a few more roadblocks that I'm trying to solve before I can start getting into the meat of it. Hopefully I will not have to dive into Python in order to do that. Thanks for the help, understanding that a variable has to be called before it can be used will help me greatly in the future.

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot]