How do I get the value of a timer? Say i have:
[lua]timer.Create("Test", 60, 1)[/lua]
How would i get the number of seconds that the timer has counted? If i use:
[lua]print(Test)[/lua]
It prints "nil" in the console. I don't know how to get the value of a timer or if im using the wrong function. Any help will be greatly appreciated.
Sadly, you cannot. However, the most common method of getting the value of a "timer" is using the CurTime() function. For example:
[lua]local function DrawTimerUp()
local time = CurTime()
hook.Add("HUDPaint","DrawTimerUp",function()
draw.DrawText(CurTime()-time,"ScoreboardText",5,5,color_white,0)
end)
end[/lua]
This draws a timer counting up in the corner of your screen.
How does CurTime() work, just it just count up in seconds from the moment its initialised? And can you pause/reset it ? Sorry, I don't really understand what it does.
I thought I should mention, im trying to make a round timer. Just in case that changes things.
Timers themselves can't give you any time. You need to get it yourself. Probably the easiest way would be to create a wrapper functions..
[lua]
local times = { };
function CreateTimer( name, ... )
times[ name ] = CurTime( );
timer.Create( name, ... );
end
function GetPassedTime( name )
return( CurTime( ) - times[ name ] );
end
[/lua]
Use CreateTimer instead of timer.Create to create your timers and then use GetPassedTime to get the time passed.
I used timer.Create in most of my codes, but you could do:
[lua]
timer.Create("BuildTimer",100,1,SwitchModes) --SwitchModes here is a custom function
...
SetGlobalInt("ModeTime",100-CurTime())--This would set ModeTime to the buildtime - current time, but once it hit 100 then you'd start getting negatives.
[/lua]
[editline]10:39AM[/editline]
That's not all of it, and you'd have to have some thing that reset that, but something like that could work.
[QUOTE=Nevec;16855898]Timers themselves can't give you any time. You need to get it yourself. Probably the easiest way would be to create a wrapper functions..
[lua]
local times = { };
function CreateTimer( name, ... )
times[ name ] = CurTime( );
timer.Create( name, ... );
end
function GetPassedTime( name )
return( CurTime( ) - times[ name ] );
end
[/lua]
Use CreateTimer instead of timer.Create to create your timers and then use GetPassedTime to get the time passed.[/QUOTE]
I tired your way, but keep getting this error:
[code]Timer Error: attempt to call a nil value
Hook 'CheckTimers' Failed: includes/modules/timer.lua:164: attempt to compare function with number
[/code]
This was when i tried:
[lua]local X = GetPassedTime()[/lua]
I dont understand what im doing wrong, please help. Thanks.
Edit.
Everytime you wrote 'name' I put in Roundtimer - are the names supposed to differ? And also in the -function CreateTimer( name, ... ) what is the ... for?
Edit.
I solved my problem with the following code:
[lua] local Roundtime = 0
timer.Create("Roundtimer", 1, 0, function()
Roundtime = Roundtime + 1
end )[/lua]
Roundtime increase by 1 every second, and I can check how many seconds have passed with print(Roundtime). Thanks for everyones help though.
Sorry, you need to Log In to post a reply to this thread.