PDA

View Full Version : BEGGING LUA HELP - Countdown in Seconds



marketability
June 6th, 2009, 07:00 AM
Apologies but I'm about to jump off a cliff - my lua ambitions far outweigh my skills

I want a script to display (in a variable) the number of seconds remaining from a set countdown

the context is I have a 45 second delay until the shutdown of a server occurs and want to show the remaining seconds during which an abort button can be pressed

I've tried girder schedules but cant seem to get the remaining seconds into a variable

All help gratefully received!

dsmes
June 7th, 2009, 06:21 PM
Here is an example to do it from Girder, but a timer function also exists for NR if you want to do it from within a NR Lua function. When you want to trigger the countdown, use something like this which triggers a function every 1000mS which equals one second:
if not Tremaining then
Tremaining = gir.CreateTimer("", function () ZoneUpdate(); end, "", true)
Tremaining:Arm(1000)
endEvery second the ZoneUpdate function will be run where you can update the NR countdown variable and do other things:
ZoneUpdate = function()
ZoneTimeLeft = ZoneTimeLeft - 1
if ZoneTimeLeft == 0 then
Tremaining:Cancel()
Tremaining:Destroy(); Tremaining = nil
-- Insert code to shut down server here
elseif self.ZoneTimeLeft < 0 then
self.ZoneTimeLeft = 0
else
print("=========== In the ZoneUpdate function. Time left is ".. ZoneTimeLeft .." Seconds")
end
NetRemote.SetVariable("Zone.Time", ZoneTimeLeft .." Seconds")
endBefore you start all this code runing, you'd set ZoneTimeLeft =45 and it will count down from 45 seconds. NOTE- untested code (I copied & edited from some existing code of mine). Hopefully this will get you going in the right direction.

marketability
June 8th, 2009, 10:05 AM
Thanks alot - I'll give it a go now
Cheers

marketability
June 8th, 2009, 12:50 PM
You're a star!

I went the NR route...



function setcountdown()
xrem = NetRemote.SetTimer( 1000 , runcountdown );
xcounter = 44
NetRemote.SetVariable("PME.dispcounter",xcounter)
end;

function runcountdown ( )
xcounter = xcounter - 1
if xcounter < 0 then
NetRemote.SetVariable("PME.debug",'xcounter<0')
NetRemote.SetVariable("PME.dispcounter",xcounter)
NetRemote.SetVariable("frame-bigpigpower",0)
NetRemote.SetVariable("showabort",0)
NetRemote.KillTimer( xrem )
end
NetRemote.SetVariable("PME.dispcounter",xcounter)
end;

NetRemote.RegisterVariableWatch('PME.Start',setcou ntdown);


When I have it settled down - i'll redo it in Girder so that the shutdown warning is sent to all clients - thanks again