ID:1292180
 
(See the best response by Pirion.)
Code:
proc
Time()
spawn(72)
if(TimeM<59)
TimeM+=1
else
TimeM=00
spawn(4320)
if(TimeH<12)
TimeH+=1
else
TimeH=0
Day+=1


mob/Stat()
stat("Date","[Day]")
stat("Time","[TimeH]:[TimeM]")


Problem description:

I'm wondering how to make it so that the time updates in the statpanel every time(or every now and then) the time proc changes the time variables. At the moment, the Time stat is stuck at 0:0

Any help is appreciated.
mob
Stat()
stat("Time:","[h]:[m]:[s]")
stat("Day:","[d]")
var
h = 0
m = 0
s = 0
d = 1
world/New()
spawn(0)
time
s++
if(s==60){;s=0;m++}
if(m==60){;m=0;h++}
if(h==24){;h=0;d++}
sleep(10)
goto time

That works. When the world starts up, it runs in the background a loop (1s delay) that counts. Every second s gets +1 in value, and everytime it exceeds 59 (60+) it resets to 0 and makes m +1. Of course, there are many better ways of doing this, but I took the way that seemed most similar with yours. I hope it helped.
In response to MasterSpectra
Don't make loops that run off of world/New()
Also, adding time using some addition in a proc is bad because if your server slows down or lags, your time accuracy will decrease, you should be using comparing to get your values...

var/start_time = 0
world/New()
start_time = world.timeofday
..()
proc/get_hours_since_startup()
return world.timeofday - start_time //vague example
In response to FIREking
Hours since startup would be more like:
proc/hours_since_startup()
return world.time / 36000

...except it wouldn't be too accurate according to real time.
This is what I use:

world
proc
get_uptime_text()
//returns approximately how long the server has been running in the form of hours, minutes, and seconds
//timing is based off of ticks, which can skip / slow down hence its inaccuracy

return "[round(world.time / 36000)]:[round(world.time % 3600 / 600)]:[round(world.time % 600 / 10)]"

get_date_text(s)
//returns the current date, in text form

if(!s) s = "-"
return time2text(src.realtime, "MM[s]DD[s]YY")

get_time_text(s)
//returns the current time, in text form

if(!s) s = "."
return time2text(src.timeofday, "hh[s]mm[s]ss")


Which gives you hh:mm:ss for up time
I don't want real time, that's the thing, I want a time I set with the code I made. I'd use real time, but then there'd be some times(usually really late/early) where no one'd be on, and coding for that would be a bit of a waste, since most people would never see it.

But the point is, I want to know how to get the code I typed to show and update in my stats panel(or something close, I don't want real-time). As you can see by my code, it should add 1 to TimeM every 7.2 seconds(I believe I'm remembering the time in byond right, it's tenths of a second right?) leaving the entire thing ticking at about 7.2 in game days for every normal day or so(used a calculator, might have messed up the math a bit, but it's 10368 'minutes' a day for the game, and that's a lot more than the 1440 of a real-time day, then divide them together and that's 7.2).
Best response
You should be able to use the following type of code to customize time flow.


time
New()
spawn() loop()

var
counter = 0
saved_counter = 0
starttime
lasttime = 0
ticks_per_second = 10 //realtime

proc
loop()
starttime = world.timeofday
while(1)
Tick(100)
Tick()
if(lasttime >= world.timeofday)
starttime = world.timeofday
saved_counter += counter
saved_counter += tick - world.timeofday
counter = saved_counter + world.timeofday - starttime
lasttime = world.timeofday

GetTime()
var
tick_second = ticks_per_second
tick_minute = ticks_per_second * 60
tick_hour = ticks_per_second * 60 * 60
tick_day = ticks_per_second * 60 * 60 * 24

tick_day_remainder = counter % tick_day
tick_day_count = (counter - tick_day_remainder)/tick_day

tick_hour_remainder = tick_day_remainder % tick_hour
tick_hour_count = (tick_day_remainder - tick_hour_remainder)/tick_hour

tick_minute_remainder = tick_hour_remainder % tick_minute
tick_minute_count = (tick_hour_remainder - tick_minute_remainder)/tick_minute

tick_second_remainder = tick_minute_remainder % tick_second
tick_second_count = (tick_minute_remainder - tick_second_remainder)/tick_second

tick_ms_count = tick_second_remainder\ticks_per_second

world << "[tick_day_count] day(s), [tick_hour_count] hour(s), [tick_minute_count] minute(s), [tick_second_count] second(s), [tick_ms_count] ms"
so, there's no less complicated way? I'll do it in that sort of way if I have to, but isn't there a way to use something a bit less complex? 'cause that's a lot of typing for 3 variables :/
If you don't want realtime, then you come up with a comparison to realtime... like by multiplying, or dividing.
ok, so I DO need to do it all complicated? well, that'll be fun :/ thanks for the help though.
This is an old code I used for time that's fairly simple:



Hope it helps.
that is simple, how 'bout that, thanks, I think I can get what I want from this. *bows to the better coders* xP
No. Don't use goto. Dont use loops in world/New(). Don't use screenshots to show code on the forums. Just...no. JS173, do not use Konlet's example, whatever you do.
In response to Konlet
Konlet wrote:
This is an old code I used for time that's fairly simple:



Hope it helps.

This is horrible. The goto is wrongly used i know more then you and i started two months ago disappointment.
You're all making this harder than it is, you can just scale time. If you want time to be slower, you would just take current time and multiply it by a decimal point... if you wanted time to be twice as long as real time, it would be realtime * 2

To display the time, you just take the current time scaled by the time factor, and then divide 3 different times (hours, minutes, seconds) to get your values...

Example:

var/time_factor = 3.4 //game time is 3.4 times longer than normal earth time

proc/get_gametime_hours()
return round((world.time * time_factor) / 36000)

proc/get_gametime_minutes()
return round((world.time * time_factor) % 3600 / 600)

proc/get_gametime_seconds()
return round((world.time * time_factor) % 600 / 10)

proc/get_gametime_text()
return "[get_gametime_hours()]:[get_gametime_minutes()]:[get_gametime_seconds()]"

//show it in a stat panel
var/last_time_text = "" //we store the time to save cpu some grief

world/New()
..()
spawn time_updater() //we call the time updater at startup

proc/time_updater()
//the time updater grabs the time, in text, and stores it, then repeats this action 10 seconds later.
last_time_text = get_gametime_text()
spawn(100) time_updater()

mob/player/Stat()
//we don't want to call get_gametime_text here because it would be recalculating the time too often
stat("Current Time:", last_time_text)


This is just one way to do it, you could also base it off of world.timeofday (resets every 24 hours) or world.realtime (only has minute precision, not seconds precision).
planet
parent_type = /area

var
currS
currM
currH
currT
currTime
currMoonCycle=1
currSunrise
currSunset
currWeather
currSeason
currDAY=1
currMONTH=1
currYEAR=1
canSave=0
seasons_enabled=1
moon_cycles[] = list("New","Waxing Crescent","First Quarter","Waxing Gibbous","Full","Waning Gibbous","Third Quarter","Waning Crescent")

proc
area_contents(){
var
players[] = list()

for(var/mob/Player/M in contents){
players.Add(M)
}

return players
}

return_time(){
var
H
M

if(currM < 10){
M = "0[currM]"
}
else{
M=currM
}

if(currH < 10){
H = "0[currH]"
}
else{
H=currH
}

return "[H]:[M] [currT]"
}

return_date(){
var
D
M
Y

if(currDAY < 10){
D = "0[currDAY]"
}
else{
D=currDAY
}

if(currMONTH < 10){
M = "0[currMONTH]"
}
else{
M=currMONTH
}

if(currYEAR < 10){
Y = "0[currYEAR]"
}
else{
Y=currYEAR
}

return "[M]/[D]/[Y]"
}

CheckMonthAmount(){
if(currMONTH == 1){
return 31
}
else if(currMONTH == 2){
return 28
}
else if(currMONTH == 3){
return 31
}
else if(currMONTH == 4){
return 30
}
else if(currMONTH == 5){
return 31
}
else if(currMONTH == 6){
return 30
}
else if(currMONTH == 7){
return 31
}
else if(currMONTH == 8){
return 31
}
else if(currMONTH == 9){
return 30
}
else if(currMONTH == 10){
return 31
}
else if(currMONTH == 11){
return 30
}
else if(currMONTH == 12){
return 31
}
}

tick_time(){
while(1){
currS++

if(currS >= 60){
currS=0
currM++
}

if(currM >= 60){
currM=0
currH++
if(currH == 12 && currT == "AM"){
currT = "PM"
}
else if(currH == 12 && currT == "PM"){
currT = "AM"
currDAY++
}
}

if(currH > 12){
currH=1
}

if(currDAY > CheckMonthAmount()){
currDAY=1
currMONTH++
}

if(currMONTH > 12){
currMONTH=1
currYEAR++
}
sleep((world.tick_lag*6))
}
}

tick_weather(){
while(1){
if(currWeather!="{YClear{x" && prob(2.50)){
if(currWeather=="{BRain{x"){
mb_msgout("The light rain stops.",area_contents())
}
else if(currWeather=="{bHeavy Rain{x"){
mb_msgout("The heavy rain stops.",area_contents())
}
else if(currWeather=="{WSnowing{x"){
mb_msgout("The snow stops falling.",area_contents())
}
else if(currWeather=="{CFreezing Rain{x"){
mb_msgout("The freezing rain stops.",area_contents())
}
currWeather="{YClear{x"
}
else if(currWeather=="{YClear{x"){
if(prob(5) && (currSeason=="{CSpring{x" || currSeason=="{YSummer{x" || currSeason=="{yAutumn{x")){
mb_msgout("A light drizzle of rain starts to fall from the sky.",area_contents())
currWeather="{BRain{x"
}
else if(prob(5) && (currSeason=="{CSpring{x" || currSeason=="{YSummer{x" || currSeason=="{yAutumn{x")){
mb_msgout("Rain starts to pour from the sky.",area_contents())
currWeather="{bHeavy Rain{x"
}
else if(prob(5) && (currSeason=="{WWinter{x" || currSeason=="{yAutumn{x")){
mb_msgout("Snow starts falling from the clouds above.",area_contents())
currWeather="{WSnowing{x"
}
else if(prob(5) && currSeason=="{WWinter{x"){
mb_msgout("Freezing rain starts to pour from the sky.",area_contents())
currWeather="{CFreezing Rain{x"
}
}
sleep((world.tick_lag*240))
}
}

tick_season(){
while(1){
if(!seasons_enabled){
currSeason="{RNONE{x"
}
else if(currMONTH == 3 || currMONTH == 4 || currMONTH == 5){
currSeason="{CSpring{x"
}
else if(currMONTH == 6 || currMONTH == 7 || currMONTH == 8){
currSeason="{YSummer{x"
}
else if(currMONTH == 9 || currMONTH == 10 || currMONTH == 11){
currSeason = "{yAutumn{x"
}
else if (currMONTH <= 12 || currMONTH <= 1 || currMONTH <= 2){
currSeason = "{WWinter{x"
}
sleep((world.tick_lag*5))
}
}

tick_sun_moon(){
while(1){
if(return_time() == currSunrise && currTime=="Night"){
mb_msgout("The sun rises in the east as the [moon_cycles[currMoonCycle]] moon sets in the west.",area_contents())
currTime="Day"
currMoonCycle++
if(currMoonCycle == moon_cycles.len){
currMoonCycle=1
}
}

if(return_time() == currSunset && currTime=="Day"){
mb_msgout("The [moon_cycles[currMoonCycle]] moon in the east rises as the sun sets in the west.",area_contents())
currTime="Night"
}

sleep((world.tick_lag*5))
}
}

load_planet_settings(planet){
var/DBQuery/Q=Query("SELECT * FROM `planets` WHERE `planet`='[planet]';")
Q.NextRow()
var/list/d = Q.GetRowData()

currS=text2num(d["currS"])
currM=text2num(d["currM"])
currH=text2num(d["currH"])
currDAY=text2num(d["currDAY"])
currMONTH=text2num(d["currMONTH"])
currYEAR=text2num(d["currYEAR"])
currMoonCycle=text2num(d["currMoonCycle"])
currT=d["currT"]
currTime=d["currTime"]
currWeather=d["currWeather"]
currSunrise=d["currSunrise"]
currSunset=d["currSunset"]
}

tick_save(){
while(1){
if(canSave){
save_planet_settings()
}
sleep((world.tick_lag*60))
}
}

save_planet_settings(){
Query("UPDATE `planets` SET `currS`='[currS]', `currM`='[currM]', `currH`='[currH]', `currDAY`='[currDAY]', `currMONTH`='[currMONTH]', `currYEAR`='[currYEAR]', `currT`='[currT]', `currTime`='[currTime]', `currWeather`='[currWeather]', `currSunrise`='[currSunrise]', `currSunset`='[currSunset]', `currMoonCycle`='[currMoonCycle]', `seasons_enabled`='[seasons_enabled]' WHERE `planet`='[name]';")
}

New(){
spawn(){load_planet_settings(name)}
spawn(1){tick_time()}
spawn(1){tick_weather()}
spawn(1){tick_season()}
spawn(1){tick_sun_moon()}
spawn(1){tick_save()}
return ..()
}

Del(){
save_planet_settings(name)
}

EARTH
name="EARTH"
currS=15
currM=32
currH=4
currYEAR=1932
currT="PM"
currTime = "Day"
currWeather = "{YClear{x"
currSunrise = "05:45 AM"
currSunset = "08:15 PM"
canSave=1

SAFE_ZONE

gero_lab
Up(mob/m)
{
m.loc=locate(308,221,1)
}

gero_entrance
Down(mob/m)
{
m.loc=locate(42,34,9)
}

NAMEK
name="NAMEK"
currS=22
currM=38
currH=8
currYEAR=4078
currT="PM"
currTime = "Day"
currWeather = "{YClear{x"
currSunrise = "CONSTANT"
currSunset = "NEVER"
seasons_enabled=0
canSave=1

SAFE_ZONE

VEGETA
name="VEGETA"
currS=55
currM=56
currH=8
currYEAR=872
currT="PM"
currTime = "Day"
currWeather = "{YClear{x"
currSunrise = "04:45 AM"
currSunset = "11:15 PM"
canSave=1

SAFE_ZONE

FRIEZA
name="FRIEZA"
currS=33
currM=32
currH=11
currYEAR=893
currT="PM"
currTime = "Day"
currWeather = "{YClear{x"
currSunrise = "07:45 AM"
currSunset = "05:15 PM"
canSave=1

SAFE_ZONE


Something i wrote for a game..it's not the best but it worked good nuff for me.
Wouldn't it be better to use switch(currMONTH) in CheckMonthAmount()? ^^
In response to Kozuma3
Kozuma3 wrote:
Wouldn't it be better to use switch(currMONTH) in CheckMonthAmount()? ^^

prob.

It should prob be rewritten Q_Q.
In response to Kozuma3
Or a look-up table.

var list/days = list(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)

proc/CheckMonthCount()
return days[currMONTH]
Page: 1 2