Jump to content

How to pause TimerInit()


Recommended Posts

  • Moderators

I've been trying to figure this out all day. I made a game with a timer, this part works great. Now what I need is a button that will pause the timer, then when clicked again it continues the timer from the same spot. This is the part that I can't figure out.

Just to make things easier I made a small example.

#include <GUIConstants.au3>

Global $Paused = False
Global $Timer = 0

GUICreate("", 100, 100)
$Button1 = GUICtrlCreateButton("Pause", 25, 40, 50, 20)
GUISetState()

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            _Pause()
    EndSwitch
    ToolTip("Time: " & _GetTime())
    Sleep(50)
WEnd

Func _Pause()
    $Paused = True
    While $Paused
        If GUIGetMsg() = $Button1 Then
            $Paused = False
        EndIf
        Sleep(10)
    WEnd
EndFunc   ;==>_Pause

Func _GetTime()
    If $Timer Then
        $TimerDiff = Int(TimerDiff($Timer) / 1000)
        Return $TimerDiff
    Else
        $Timer = TimerInit()
    EndIf
EndFunc   ;==>_GetTime

Func _Exit()
    Exit
EndFunc   ;==>_Exit
Link to comment
Share on other sites

  • Moderators

Damn, I thought this was a simple logic issue, I was going to say, just user TimerStop() but that didn't work :D (and it isn't in the help file anymore but it is in the keywords list for SciTe) :D

Anyway, try this and see if it suits your needs:

#include <GUIConstants.au3>

Global $Paused = False
Global $Timer = 0, $PausedTimer = 0, $Timer2 = 0

GUICreate("", 100, 100)
$Button1 = GUICtrlCreateButton("Pause", 25, 40, 50, 20)
GUISetState()

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            _Pause()
    EndSwitch
    ToolTip("Time: " & _GetTime(), 0, 0)
    Sleep(50)
WEnd

Func _Pause()
    $Paused = True
    _PauseTime()
    While $Paused
        If GUIGetMsg() = $Button1 Then $Paused = False
        Sleep(10)
    WEnd
EndFunc   ;==>_Pause

Func _GetTime()
    If $Timer Then
        Return Int(TimerDiff($Timer) / 1000) + $PausedTimer
    ElseIf Not $Timer2 And $PausedTimer Then
        $Timer2 = TimerInit()
        Return $PausedTimer
    ElseIf $Timer2 And $PausedTimer Then
        Return Int(TimerDiff($Timer2) / 1000) + $PausedTimer
    EndIf
    $Timer = TimerInit()
EndFunc   ;==>_GetTime

Func _PauseTime()
    If Not $Timer2 Then
        $PausedTimer += Int(TimerDiff($Timer) / 1000)
        $Timer = 0
    Else
        $PausedTimer += Int(TimerDiff($Timer2) / 1000)
        $Timer2 = 0
    EndIf
EndFunc

Func _Exit()
    Exit
EndFunc   ;==>_Exit

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Damn, I thought this was a simple logic issue, I was going to say, just user TimerStop() but that didn't work :D (and it isn't in the help file anymore but it is in the keywords list for SciTe) :D

Scite4AutoIt3 tries to be compatible for v3.... versions of AutoIt. This would allow you to use older versions that have no COM, Gui etc. :P
Link to comment
Share on other sites

Func TimerPause($timer)

$currentDiff = TimerDiff($timer)

TimerStop($timer)

return $currentDiff

Endfunc

Func TimerRunFromPause($timerpause)

return TimerInit( $Timerpause )

Endfunc

would this work ?

My UDF's : Startet on : 06.06.2006_CaseSearchOrReplaceStr();~> Searches OR Replaces a String,;~> With or Without Casesensivity
Link to comment
Share on other sites

  • Developers

Global $SaveDiff = 0

$t = TimerInit()
Sleep(1000)
ConsoleWrite(TimerPause($t) & @LF)
Sleep(1000)
;
$t = TimerInit()
Sleep(1000)
ConsoleWrite(TimerPause($t) & @LF)
Sleep(1000)
;
$t = TimerInit()
Sleep(1000)
ConsoleWrite(TimerPause($t) & @LF)

Func TimerPause($timer)
    $SaveDiff = $SaveDiff + TimerDiff($timer)
    TimerStop($timer)
    Return $SaveDiff
EndFunc   ;==>TimerPause

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Moderators

Global $SaveDiff = 0

$t = TimerInit()
Sleep(1000)
ConsoleWrite(TimerPause($t) & @LF)
Sleep(1000)
;
$t = TimerInit()
Sleep(1000)
ConsoleWrite(TimerPause($t) & @LF)
Sleep(1000)
;
$t = TimerInit()
Sleep(1000)
ConsoleWrite(TimerPause($t) & @LF)

Func TimerPause($timer)
    $SaveDiff = $SaveDiff + TimerDiff($timer)
    TimerStop($timer)
    Return $SaveDiff
EndFunc   ;==>TimerPause
Nice Jdeb :D

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

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