ID:2345704
 
(See the best response by Kats.)
Code:
    Clock
icon='Clock.dmi'
icon_state="12"
layer=MOB_LAYER+5
density=1
Click()
usr<<"You read the time, it is [time2text(world.realtime,"hh:mm")]"


Problem description: I need help figuring out how to make a clock display the time. Hour hands and Minute hands will be separate.

I.E. I want the Icon State for hour and minute hands to update (Hour hand every Hour and Minute hand every 5 minutes)


Best response
You could do individual icon states, but given you want a state for every 5 minutes of a 12-hour period that's 144 icon states and you're only limited to 5 minute increments, which would look jumpy and kinda bad.

If you want a super clever way of doing this, try this:

Have the clock face, minute hand and hour hand each separate icons.

The clock face is the base of the icon and you give it minute hand and hour hand objects that overlay it that it can manipulate.

So, if you do the math, there are 360 degrees in a circle and 60 minutes in an hour. Ideally you could make that minute hand tick every degree of that circle by just iterating it once every 10 seconds.

To do this, you get the number of deciseconds (1/10th of seconds) in the day by doing world.timeofday which spits you out an integer.

If you want to get the degrees the hand should be, this equation should suffice:

var/minute_hand_angle = round(world.timeofday/10%3600/10)

Now, that looks like black magic, I know, but it will spit out a number between 0 and 359 that you can then directly apply to the icon of the minute hand and have it always turned the right direction.

The hour hand is literally just as simple, just a little bit different math:

var/hour_hand_angle = round(world.timeofday/10%43200/120)

Again, more black magic math, but if you take the time to calculate out angles based on modulo (clock) arithmetic, then you'll find that it comes out correctly.

Not only that, but your hour hand will now slowly creep along the hour exactly in line with the time the minute hand is showing. If you set the time to 4:30, then the minute hand will be on the 30 and the hour hand will be halfway between 4 and 5, just like it should be.

Hope this helps you, conceptually.
In response to Kats
Works wonderfully! Thanks a million :D

In response to Kats
Turns out the clock is showing an hour behind the time it should

    Minute_Hand
icon='Clock.dmi'
icon_state="Minute Hand"
Click()
var/minute_hand_angle
while(1)
minute_hand_angle = round(world.timeofday/10%3600/10)
src.transform = turn(src.transform, minute_hand_angle)
sleep(100)
src.transform = turn(src.transform, - minute_hand_angle)
Hour_Hand
icon='Clock.dmi'
icon_state="Hour Hand"
var/hour_hand_angle
Click()
var/hour_hand_angle
while(1)
hour_hand_angle = round(world.timeofday/10%43200/120)
src.transform = turn(src.transform, hour_hand_angle)
sleep(100)
src.transform = turn(src.transform, - hour_hand_angle)


Mind my terrible scripting.