Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Night and Day

Kai Venkman
Will script for food...
Join date: 21 Feb 2006
Posts: 43
03-09-2006 09:52
Greetings,

I'm trying to add specific night and day behavior to an object; a good example would be like a light turning on at dusk and off at daybreak. What I'm wondering is what the most efficient method is. At first blush I'm thinking of a timer that fires every minute and checks the time.

Is there a better way?

TIA
Ziggy Puff
Registered User
Join date: 15 Jul 2005
Posts: 1,143
03-09-2006 10:19
Well, if a timer fires and checks the time and sees that it is 1 PM, then a minute later the time is probably going to be 1:01.

How about a timer that fires, checks the time, calculates how much longer to go until the time you need, and then sets a timer for that long? So if you check the time and it's 1 PM and you want to do something at 6:30 PM, set a timer for 5 1/2 hours.

If you're worried about accuracy (which I'm not sure why you would be, because it shouldn't matter if dusk happens a few minutes late) You could wake up 5 minutes before the required time, and then check it every minute or something.
Kai Venkman
Will script for food...
Join date: 21 Feb 2006
Posts: 43
03-09-2006 10:33
I knew someone would make me do math... *pouts*
Argent Stonecutter
Emergency Mustelid
Join date: 20 Sep 2005
Posts: 20,263
03-09-2006 10:46
Here's a script I recently posted for doing a similar thing... except with a 15 minute interval rather than a half-day interval. The modifications shouldn't be too onerous.
CODE

integer RING_INTERVAL = 15; // minutes

time_sync() // Figure out how many seconds to next chime, and set the timer event for it
{
integer seconds_used = ((integer)llGetWallclock()) % (RING_INTERVAL * 60);
llSetTimerEvent((RING_INTERVAL * 60) - seconds_used);
}

default {
state_entry() {
time_sync(); // first time through
}
on_rez(integer p)
{
time_sync(); // Who knows how long we were in inventory
}
timer() {
// Ring.
time_sync(); // And the event might have been delayed...
}
}
BamBam Sachertorte
floral engineer
Join date: 12 Jul 2005
Posts: 228
03-09-2006 10:58
You also want to use the llGetSunDirection() function to see where the sun is. It returns a vector that is the direction of the sun. The z-coordinate is positive during the day and negative at night.