4
$\begingroup$

I am trying to make a realistic day/night/season simulator for a BGE project. the lighting and sky will be animated based on the time of day, and the ground will have 365 frames - animated between green grass, leaves, and snow, as the seasons progress. I know how to make everything run perfectly, except for one part.

I have an object, called Time of Day Control Empty. (the name could easily be changed if it is too long, or the spaces are a problem)

enter image description here

I need a script to, upon running, find how many days it is into the year, and assign that value to the property "Day"

Then, I want the script to constantly update the property "Minutes" with the value of how many minutes into the day it currently is based on the current time.

The property "Day" should only range between 0-365, and the property "Minutes" should range between 0.00-1440.99 - It would be nice if the seconds were represented in .00 - .99 based on how far it was into each minute, at the end of the float property "Minutes," but that isn't really necessary.

Supposing that the current time was 2:27:27 PM on December 25th, the properties should look just like this:

enter image description here

I'm sure this is possible, but I have no idea where to start. How can I go about making this script?

$\endgroup$
  • $\begingroup$ oooo can't wait to see some good answers. $\endgroup$ – Scalia Dec 4 '15 at 19:55
  • $\begingroup$ BTW your example time seems off.. If seconds are to go from .0 to .99, 2:27:27PM should be 867.45. Do you want seconds to be scaled? Or left as .0 to .59? $\endgroup$ – gandalf3 Dec 4 '15 at 21:21
  • $\begingroup$ scaled. my example is incorrect.... (your answer answered what I wanted even though I asked the wrong thing!) $\endgroup$ – X-27 Dec 4 '15 at 21:46
2
$\begingroup$

You can get the current time in python with the time module (who would've thunk, right?)

time.localtime() gives you a struct containing everything you need:

import time
import bge

def clamp(n, nmin, nmax):
    if n > nmax:
        return nmax
    elif n < nmin:
        return nmin
    else:
        return n

cont = bge.logic.getCurrentController()
own = cont.owner

# this returns a struct containing the year, month, day etc. 
now = time.localtime() 

# combine the hour and minute to get the total number of minutes, plus seconds (clamped to avoid tangling with leap seconds) scaled to .00 to .99
own['minute'] = now.tm_min + now.tm_hour*60 + clamp(now.tm_sec, 0, 59)*.01667
# we don't even need to do anything to get the year day. Note that this can return 356 on leap days, so be sure you either clamp it here or handle such a situation properly later.
own['day'] = now.tm_yday 

Just hook it up to an always sensor. I set the skip on the always sensor so it only gets executed once a second at 60 tics per second, to avoid using extra precious cpu time:

enter image description here

$\endgroup$
  • $\begingroup$ This is exactly what I wanted! already have it working in my blend. Thanks! (Uved and Accepted) $\endgroup$ – X-27 Dec 4 '15 at 21:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.