Getting "Timer" time at the end of a level

Hello, I’m making a game and I have a timer that starts counting up once the level begins. Once the player reaches the final scene the time stops. This works fine, but what I’d like to do is have the final time recorded along with the final score (best time along high score). I’m able to record my score but I have no way of pulling the final time.

Here is the code from the scene where the timer starts (upon entering the scene).

currentTime = 0 local currentTimeText = display.newText(currentTime, 160, 20, native.systemFontBold, 24) currentTimeText:setTextColor(255,0,0) local function timerUp() currentTime = currentTime + 1 currentTimeText.text = currentTime

(I’m not even sure if that was done correctly).

Now here is the code on the final scene that stops the timer.

local time = timerUp timer.cancel(timerUpTimer) local time = timerUpTimer

This will stop the timer but I can’t figure out how to save the final time.

I’ve read all the documentation I could find online.

Any suggestions would be greatly appreciated.

Thank you.

Try

local function timerUp(event)      currentTime = currentTime + 1         event.source.finalTime = currentTime      ... end ... local myTimer = timer.performWithDelay( 10, timerUp, -1 )

Later

  timer.cancel(myTimer)   local finalTime = myTimer.finalTime 

@Idurniat thank you for the suggestions.

I have tried to make the changes you suggested but I get new errors.

It seems to have trouble with the “event.source.finalTime” line (I think)

If I remove that one line I’m able to proceed forward but I get errors on the last scene (I think when trying to display the final time).

local textTimea = display.newText( "time = " .. finalTime, display.contentCenterX, display.contentCenterY, native.systemFontBold, 82 ) textTimea:setTextColor( 1, 0.5, 0 ) mainGroup:insert( textTextTimea )

What kind of errors you get? 

if I leave in the line:

event.source.finalTime = currentTime

I get error saying "error on line 22 (the line with “event.source.finalTime = currentTime”) stack traceback in "eventListener

?: function

?:function"

The app won’t load at that point.

It is hard to say what is wrong but try this

  1. It could be scope problem. Timer variable (myTimer in my code) can’t be accessible in place where you want use it. So try 

    – put declaration at top of file local myTimer 

  2. Use finalTime variable. This should works :slight_smile:

    – put it at top of file local finalTime = 0 local function timerUp(event)      currentTime = currentTime + 1         finalTime = currentTime      … end … local myTimer = timer.performWithDelay( 10, timerUp, -1 )

Thank you once again. I tried to code and it start the timer off no problem, but now I get an error when the last page tries to load. 

The error message is vague “Cannot load page”.

If I remove the new script it loads. but I still can’t get the timer to give me the final time.

If I remove the “Local myTimer” from the top of the file it will then load the final scene and give me an error on the "print text line:

local textTime = display.newText( "timer = " .. finalTime, display.contentCenterX, display.contentCenterY, native.systemFontBold, 64 ) textTime:setTextColor( r ) mainGroup:insert( textTime ) textTime:toFront( ) mainGroup:insert( textTime )

Why can’t you log the start time and then at the end compare current time to the start time - this will give the total time played without the need for a timer?

I’m still very new. I don’t know how to do that. sounds like it might work, but no idea how to code that. :frowning:

Local startTime --when you start your game record the time startTime = os.time() --when you finish your game local timePlayed = os.time() - startTime

timePlayed will be the amount of seconds played 

That worked! thank you so much! @Sphere Game Studios

Try

local function timerUp(event)      currentTime = currentTime + 1         event.source.finalTime = currentTime      ... end ... local myTimer = timer.performWithDelay( 10, timerUp, -1 )

Later

  timer.cancel(myTimer)   local finalTime = myTimer.finalTime 

@Idurniat thank you for the suggestions.

I have tried to make the changes you suggested but I get new errors.

It seems to have trouble with the “event.source.finalTime” line (I think)

If I remove that one line I’m able to proceed forward but I get errors on the last scene (I think when trying to display the final time).

local textTimea = display.newText( "time = " .. finalTime, display.contentCenterX, display.contentCenterY, native.systemFontBold, 82 ) textTimea:setTextColor( 1, 0.5, 0 ) mainGroup:insert( textTextTimea )

What kind of errors you get? 

if I leave in the line:

event.source.finalTime = currentTime

I get error saying "error on line 22 (the line with “event.source.finalTime = currentTime”) stack traceback in "eventListener

?: function

?:function"

The app won’t load at that point.

It is hard to say what is wrong but try this

  1. It could be scope problem. Timer variable (myTimer in my code) can’t be accessible in place where you want use it. So try 

    – put declaration at top of file local myTimer 

  2. Use finalTime variable. This should works :slight_smile:

    – put it at top of file local finalTime = 0 local function timerUp(event)      currentTime = currentTime + 1         finalTime = currentTime      … end … local myTimer = timer.performWithDelay( 10, timerUp, -1 )

Thank you once again. I tried to code and it start the timer off no problem, but now I get an error when the last page tries to load. 

The error message is vague “Cannot load page”.

If I remove the new script it loads. but I still can’t get the timer to give me the final time.

If I remove the “Local myTimer” from the top of the file it will then load the final scene and give me an error on the "print text line:

local textTime = display.newText( "timer = " .. finalTime, display.contentCenterX, display.contentCenterY, native.systemFontBold, 64 ) textTime:setTextColor( r ) mainGroup:insert( textTime ) textTime:toFront( ) mainGroup:insert( textTime )

Why can’t you log the start time and then at the end compare current time to the start time - this will give the total time played without the need for a timer?

I’m still very new. I don’t know how to do that. sounds like it might work, but no idea how to code that. :frowning:

Local startTime --when you start your game record the time startTime = os.time() --when you finish your game local timePlayed = os.time() - startTime

timePlayed will be the amount of seconds played