[Home] [Back To Scripts]

Wall Clock Tutorial


I acquired this script long ago in SL at IBM open source region.  Here's the verbose code, including all events in one script:

//  Analog Wall Clock
//  Created by Water Rogers for IBM/Opensource
 
//  Purpose
//  --------------------------------------------------------------
//  This purpose of this script is to show llRotateTexture() in
//  action.  It also uses multiple scripts in different links of
//  the object.
 
//  Requirements
//  --------------------------------------------------------------
//  3 Prims are required for all 3 functions, as well as a texture
//  that looks like a clock hand where the rotation of the object's
//  Roll is 0 degrees.
 
//  Usage
//  --------------------------------------------------------------
//  Put this script into an object, and use the function desired.
//  Hours, minutes, and seconds all affect how and when the texture
//  will rotate.
 
 
//  EVENTS
//  --------------------------------------------------------------
 
display_hours()
{
    //  Get the wallclock, and in order to figure out the hours, divide
    //  the seconds returned from wallclock by 60, then divide that number
    //  by 60.  This will return the hour of the day (in military time).
    //  Since this is the hour hand, we take 360 degrees (a full circle)
    //  and divide it by 12 (the hours on a clock).  This returns 30
    //  degrees - multiply by the current hour, then translate the remainder
    //  to Radians, since that's how rotations are interpretted in SL.
    llRotateTexture(-(((llGetWallclock() / 60) / 60) * 30) * DEG_TO_RAD, ALL_SIDES);
}
 
display_minutes()
{
    //  The same logic applies to minutes, as with hours, except this time we
    //  are only interested by how many minutes have passed rather then hours.
    //  One less mathmatical computation.  Also, 360 divided by 60 segments
    //  equates to 6 degrees.
    llRotateTexture(-((llGetWallclock() / 60) * 6) * DEG_TO_RAD, ALL_SIDES);
}
 
display_seconds()
{
    //  Same as the other two, except the wallclock already returns how many
    //  seconds, so there is no need to do any other math other then to translate
    //  to radians and rotate the texture.
    llRotateTexture(-(llGetWallclock() * 6) * DEG_TO_RAD, ALL_SIDES);
}
 
default
{
    state_entry()
    {
        //  Call the function that this object will represent.  This particular
        //  object happens to be the "seconds" hand of the clock.  The other 2
        //  will call the other 2 respectively.
        display_seconds();
        llSetTimerEvent(1);  //change this to (10) for minutes and (60) for hours for better function in OS.
       
    timer()
    {
        display_seconds();
    }
}


You have to create three cylinder prims, and include each of three scripts in each prim, to make second hand, minute hand and hour hand of clock.
Here are links to the transparent textures I used in each prim to create a clock:

hour_hand.tga
minute_hand.tga
second_hand.tga

Make sure you use clear texture on all other surfaces of prim but the face.

Here are each of the three scripts, separated and with comments removed for your convenience:

1. Hour hand script:

//  Analog Wall Clock
//  Created by Water Rogers for IBM/Opensource
//  Minute hand script

display_minutes()
{
    llRotateTexture(-((llGetWallclock() / 60) * 6) * DEG_TO_RAD, ALL_SIDES);
}
 
default
{
    state_entry()
    {
        display_minutes();
        llSetTimerEvent(60); 
       
    timer()
    {
        display_hours();
    }
}

2. Minute hand script:

//  Analog Wall Clock
//  Created by Water Rogers for IBM/Opensource
//  Hour hand script

display_minutes()
{
    llRotateTexture(-(((llGetWallclock() / 60) / 60) * 30) * DEG_TO_RAD, ALL_SIDES);
}
 
default
{
    state_entry()
    {
        display_minutes();
        llSetTimerEvent(10); 
       
    timer()
    {
        display_minutes();
    }
}

3. Second hand script:

//  Analog Wall Clock
//  Created by Water Rogers for IBM/Opensource
//  Second hand script

display_seconds()
{
    llRotateTexture(-(llGetWallclock() * 6) * DEG_TO_RAD, ALL_SIDES);
}
 
default
{
    state_entry()
    {
        display_seconds();
        llSetTimerEvent(1); 
       
    timer()
    {
        display_seconds();
    }
}

You can just use the first script for each prim of your clock, if you know how to change the events called by the script, but I broke them down into individual scripts, to help you understand better what each event does.