Jump to content

Issue with checking variables with timer


Sora

Recommended Posts

Hello guys, i have been recently trying to make a game mode, with a custom map timer

i made this code and it works but has one issue which is that i can't confirm if the minutes variable has the same time as the map does

-- counter 
  
ms = 0 
s = 0 
m = 0 
  
  
function getMapTime(map) 
        mapt = get(map.. '.duration') 
        em = tonumber(mapt) /60 
        Ctimer = setTimer(startCounter,50,0) 
end 
  
  
function calculateTime() 
    if Ctimer ~= nil then 
        if ms == 1000 then 
            s = s + 1 
            ms = 0 
        elseif s == 60 then 
            s = 0 
            m = m + 1 
        end 
    end 
end 
  
  
function startCounter() 
    if m == em then 
            killTimer(Ctimer) 
            Ctimer = nil 
            outputChatBox("Map time has ended "..em.." ") 
    end 
outputChatBox("* "..m..":"..s..":"..ms.." ",root,255,255,255,true) 
outputChatBox(em) 
ms = ms + 1 
calculateTime() 
end 
  
  

it does count the time but it doesn't stop when it reaches the map time, as if it was 1 min for example it won't stop there but keeps going on, i tried changing functions positions in the file but nothing changes :?

Link to comment

You are doing it wrong.

  
local duration = 8 -- lets say 8 seconds 
  
function startCounter ( )  
  
end 
  
Ctimer = setTimer(startCounter,duration*1000,1) 
  
ms, _, _= getTimerDetails ( Ctimer ) 
s = ms/1000 
m = ms/60 

Also I'm sure a while loop in a thread is used for getTimerDetails since for a custom timer you will need to check every instance how much time has passed. You are getting wrong time because your code is wrong but since we already have getTimerDetails function so you definitely don't need to waste time writing a new one. ;)

EDIT: use isTimer( TimerVariable ) to check if timer still exists because timer is not set to nil and also don't check like this:

if s == 60 then

Instead do it like this:

if s >= 60 then -- greater than or equal to

Edited by Guest
Link to comment

Hmm, its bad to use a timer in this case. Wait, a timer is ok but not every 50ms and so many global variables.. :/

Maybe you can do it like this:

  
function getMapTime(map) 
    local mapDuration = get(map .. ".duration") * 1000 -- if we get the duration in seconds, multiplicate with 1000 to get ms 
    mapTimer = setTimer(stopMap, mapDuration) -- To prevent errors, make sure that the duration is correct 
end 
  
function stopMap() 
    outputChatBox("Time elapsed lel..") 
end 
  

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...