ID:141753
 
Code:
mob/proc/updatetime()
var/t=time2text(world.realtime,"hh:mm:ss")
winset(src,"Browser.time",{"text="[t]""})
spawn(10) updatetime()


Problem description:I am trying to output the time in a label and update it every second but this doesn't work. Can anyone correct me please?

Also, is there a way to change it from a 24hour view to the 12hour view (as in p.m. and a.m.)?

mob
proc
updatetime()
for()
var/T = time2text(world.realtime,"hh:mm:ss")
winset(src,"Browser.time","text=\"[T]\"")
sleep(10)


for() and while(1) are better for doing infinites loops intead of re-reading the proc, by the way.
In response to Danny Kenobi
I'm pretty sure a for proc needs an argument but regardless, this doesn't work for me. Is there any other way?
In response to DisturbedSixx
Nope. Mine works just fine without the argument :)
In response to Mizukouken Ketsu
But then again, just because it works doesn't mean it's right.
In response to DisturbedSixx
It's an ugly way of doing an infinite loop. Everyone but Kaioken and newer people that have seen that post by Kaioken use while(1) or some similiar constant.
In response to Popisfizzy
What's ugly is subjective. I consider while(1) to be utterly ugly and rather silly to use. What you're doing with that is basically a hack, a workaround - "I want an infinite loop, but damn, the while() construct requires a condition. But I don't need a condition, I want the loop to always continue... well, I'll just stick a fake, constantly true condition in there, so every time it checks it, it will still be true and continue forever."
Instead, I prefer using the proper method. Since there is a looping construct that doesn't require a condition, there's no need to use a workaround to trick one that does to continue infinitely. And it's naturally more efficient to boot.
In response to Kaioken
Hm, neither the while(1) nor the for() seem to work for me.
Don't pay any attention to those guys. Your problem isn't with the loop. Besides, your method is perfectly fine for this case.

You're not getting any output because you're using world.realtime. You should use world.timeofday instead, since it's more accurate.

As far as getting 12 hour time, that's tricky. Luckily for you, though, I've already done the hard work for Chatters, so here's a snippet that should get you going.

mob
var
_24hr_time = 0 // if 1, use 24 hour time, otherwise use 12 hour
list/time_format = list("hh",":","mm",":","ss") // the format time should be in
time_offset = 0 // offset in hours from the server (can be a decimal)
ticker = 1 // whether or not to show the : in the time output

Login()
..()
spawn() updatetime()

proc
updatetime()
ticker ^= 1 // toggles the ticker, making it flash on and off
var/t=ParseTime(ticker)
winset(src,"Browser.time",{"text="[t]""})
spawn(5) updatetime() // update every half a second to animate the ticker

ParseTime(hide_ticker)
var/parsed_msg = "", hh, mm, ss, am = TRUE, timestamp = world.timeofday+(src.time_offset*36000)

for(var/t in time_format)
if(t == "hh")
if(!_24hr_time)
hh = text2num(time2text(timestamp, "hh"))
if(hh>12 || !hh)
hh = hh%12
am = FALSE
if(!hh) hh = 12
hh = "[hh]"
else
hh = time2text(timestamp, "hh")
if(!("mm" in time_format) && !("ss" in time_format) && !_24hr_time)
hh +=" [am ? "am" : "pm"]"
parsed_msg += hh
else if(t == "mm")
mm = time2text(timestamp, "mm")
if(!("ss" in time_format) && !_24hr_time)
mm += " [am ? "am" : "pm"]"
parsed_msg += mm
else if(t == "ss")
ss = time2text(timestamp, "ss")
if(!_24hr_time)
ss += " [am ? "am" : "pm"]"
parsed_msg += ss
else if((t == ":") && hide_ticker)
parsed_msg += " "
else
parsed_msg += t

return parsed_msg
In response to Xooxer
Ah, thank you very much for letting me use such a niffy snippet! I'm very grateful. I plan on using this system with a day/night system and I was just wondering if you could let me in on a way of checking if its a new hour so it turns to night and in an hour it turns back to day. I'd like to make the day and night alternate every hour for now.
In response to DisturbedSixx
That would be better handled by another global loop. Really, everything that loops can be handled by a single global loop. I usually like to setup a loop datum and just let it call any atom's Loop() proc that's in its loop list every cycle. This has the added advantage of putting the whole game's speed control in one place, so you can tweak the game speed if things are running slowly.

var/GameLoop/GameMan

GameLoop
var
delay = 1
list/loop_list

New()
loop_list = new()
..()
spawn(delay) Loop()

proc
Loop()
for(var/atom/A in loop_list)
spawn()
if(A) A.Loop()
else loop_list -= A
spawn(delay) .()

atom
proc
Loop()


For changing time, you could make an object called Chronos (any opportunity to name game controllers after ancient gods must not be missed), who can track when the day should turn to night, the seasons, time of year and much more.

Then just add him to the GameLoop's loop_list and his Loop() proc will be called every cycle, giving Chronos another chance to check the time and such.

obj/Chronos
var
midnight = "00:00"
sunrise = "06:30"
midday = "12:00"
sunset = "18:30"

delay = 600 // only needs to be called once a minute
// would need adjusting if you change
// the game loop's delay to anything but 1

New()
..()
if(!GameMan) GameMan = new() // Make sure the Game loop manager exists
GameMan.loop_list += src // add Chronos to the loop list

Loop()
if(delay-->0) return // wait for the delay to be decremented to 0
delay = initial(delay) // then reset it to its initial value (600)

var/time = time2text(world.timeofday, "hh:mm") // what time is it?
if(time == midnight) // is it midnight?
world << "The witching hour is upon us!"
if(time == sunrise)
world << "A new day has dawned."
if(time == midday)
world << "The sun reaches its zenith."
if(time == sunset)
world << "The sun has set on another day."
In response to Xooxer
How would this affect people in different time zones? Is it only according to the host? Is it only according to BYOND time?
In response to Mizukouken Ketsu
Well, I would assume that the game's day/night is consistent for everyone across all time zones. I mean, it wouldn't make much sense if I'm playing the same time as you in the game, but you're seeing night while I'm seeing day. :/
In response to Xooxer
Time is a very confusing topic...

That would make the best real life sense however; Asia is seeing dawn while the Unitied States is seeing dusk.

My question is why doesn't it? What is holding the time? What/who defines that time? How does the client know to ONLY use ONE world.realtime for that type of loop, rather than anyone who logs in?

Here's where I'm confused big time. There's a guy living 1 hour behind me. An event is set to trigger at 09:00 MY time, and every 2 hours after that. The event SHOULD trigger whenever someone hits 09:00. In this case, every 1 hour, rather than 2 hours.
In response to Mizukouken Ketsu
Mizukouken Ketsu wrote:
That would make the best real life sense however; Asia is seeing dawn while the Unitied States is seeing dusk.

Um... the game isn't real life.

My question is why doesn't it?

Why doesn't it what; make sense to have the time in the game based on each player's current real time? Let's think about that for a second:

PlayerUSA: "Hey, where do I find the tough monsters?"
PlayerAZN: "Right here, can't you see them?"
PlayerUSA: "No, do I need a special spell or something?"
PlayerAZN: "Of course not. They come out every night."
PlayerUSA: "Oh, but it's daytime right now."
PlayerAZN: "No it's not. Why do you think I'm fighting monsters?"
PlayerUSA: "I don't see any monsters. I thought you were just crazy."


Weird, huh?

What is holding the time?

The server.

What/who defines that time?

Same server.

How does the client know to ONLY use ONE world.realtime for that type of loop, rather than anyone who logs in?

Because the one loop uses only the server time. If you want offsets for different players, you need to add them to the server's time for each player.

Here's where I'm confused big time. There's a guy living 1 hour behind me. An event is set to trigger at 09:00 MY time, and every 2 hours after that. The event SHOULD trigger whenever someone hits 09:00. In this case, every 1 hour, rather than 2 hours.

Then you'll have to account for that with the player's offset from the server. See the code snippet I posted in my first reply for an example of adjusting the time with an offset.
In response to Xooxer
Oh okay. I thnk I get it. It's like GMT time.