Posts ID:265890 Favorites Creations
ID:265890

Well it's basically a countdown system:

 ```proc/Countdown() var/seconds = timer%60 var/minutes = round(seconds/60) while(seconds > 0 || minutes > 0) while(seconds > 0) seconds-- worldWinset("timer","text=\"[minutes]:[seconds < 10 ? "0[seconds]" : "[seconds]"]\"") sleep(10) minutes-- seconds=60 worldWinset("timer","text=\"[minutes]:[seconds < 10 ? "0[seconds]" : "[seconds]"]\"") ```

worldWinset is a proc that loops through all of the mobs in the world and uses winset on them.

 ```proc/worldWinset(control, parameter) for(var/mob/m in world) if(m.client) winset(m, control, parameter) ```

- One less while() loop, substituted by the use of % and one division. (Thanks to Vermolius)
Edit:

Just saw your edit... and unfortunately, the minutes var part is wrong. seconds will only have a result of 0-59 so really you are making minutes always 0. See how I was able to make minutes a whole number without round() and avoiding that problem<hr>The modulus procedure is very helpful for this type of thing:
 ```proc/Countdown() var seconds = timer%60 minutes = (timer-seconds)/60 ```

Essentially what the above has done is obtain how many seconds has passed (see the modulus (%) entry in the DM reference) and subtract that from the timer (so timer has all minute based values) and divided that by 60

For example: Lets say timer is 125 (2 min 5 sec).
seconds = 5 (125%60 = 5)
minutes = 2 ( (125-5)/60 = 120/60 = 2)

That takes of your first while() there.

 ``` while(1) while(--seconds > 0) // takes 1 from seconds and THEN see if that is > 0. worldWinset("timer",{"text="[minutes]:[seconds < 10 ? "0[seconds]" : "[seconds]"]""}) // Nothing biggy up there, I just hate \"s sleep(10) if(--minutes < 0) // Not <= 0 because after 1 minute... well, you can guess what happens. break seconds=60 worldWinset("timer",{"text="[minutes]:[seconds < 10 ? "0[seconds]" : "[seconds]"]""}) ```

In reality, the above snippet really didn't do anything much as the first snippet... so you can disregard this one if you wish.

For your second snippet, you can loop for all clients instead and winset() that client (you can use either the /mob or the /client as the first argument):
 ```proc/worldWinset(control, parameter) for(var/client/C) // Edi2t: Foomer had a better alternative. If you have a list defined for clients/mobs of the player - you can go through that instead of making DM search the whole world. winset(C, control, parameter) ```
Using the modulus (%) operator could clean up that first part, and for the second part, I would organize it a bit differently using a do... while() loop:
 ```objectofsomekind //? var/timer=600 //? proc/Countdown() var/minutes = round(timer/60) var/seconds = timer%60 do worldWinset("timer","text=\"[minutes]:[seconds < 10 ? "0[seconds]" : "[seconds]"]\"") if(--seconds<0 && minutes>0) seconds=59 minutes-- if(seconds) sleep(10) while(seconds > 0) ```

*Edit*
Doh, beaten by a minute. I need to work faster D:
In response to GhostAnime (#1)
GhostAnime wrote:
 ```> while(1)> while(--seconds > 0) // takes 1 from seconds and THEN see if that is > 0.> worldWinset("timer",{"text="[minutes]:[seconds < 10 ? "0[seconds]" : "[seconds]"]""})> // Nothing biggy up there, I just hate \"s> sleep(10)> if(--minutes < 0) // Not <= 0 because after 1 minute... well, you can guess what happens.> break> seconds=60> worldWinset("timer",{"text="[minutes]:[seconds < 10 ? "0[seconds]" : "[seconds]"]""}) ```

There were some problems with that snippet like the counter not reaching 0:00 at all or it jumping 2 seconds at the start, from 2 minutes to 1:58. Anyways, not anything I couldn't fix and thanks !

With your help(and Vermolius and yes, even you DarkCampainger :]) here's how it currently looks:

 ``` proc/Countdown() var/seconds = timer%60 var/minutes = (timer-seconds)/60 for() worldWinset("timer","text=\"[minutes]:[seconds < 10 ? "0[seconds]" : "[seconds]"]\"") sleep(10) while(seconds > 0) seconds-- worldWinset("timer","text=\"[minutes]:[seconds < 10 ? "0[seconds]" : "[seconds]"]\"") sleep(10) if(--minutes < 0) break seconds=59 worldWinset("timer","text=\"[minutes]:[seconds < 10 ? "0[seconds]" : "[seconds]"]\"")proc/worldWinset(control, parameter) for(var/client/C) winset(C, control, parameter) ```
 #4 Jan 13 2009, 9:01 pm In response to Andre-g1 (#3) Keep in mind that the timer may not be completely accurate if worldWinset() is having to loop through a decent amount of players. Likewise, 10 ticks may take more than 1 second to get through. So depending on the degree of accuracy you're looking for you may want to periodically compare and adjust using world.timeofday and world.realtime as references (world.time is based off ticks so may also not be completely accurate).