Author Topic: Object Counter  (Read 2075 times)

Offline XanthorXIII

  • Mr. Polyvector
  • ***
  • Posts: 197
    • View Profile
Object Counter
« on: 2011-May-20 »
Thought I would share this handy object counter. Essentially it's an Eggtimer that you set and check to see if a specified time period in seconds has been reached. Thanks to MrTAToad for the concept from his AppTimer especially the Pause Function from it that solved a math issue I was having.

You start by creating the object with a given time in seconds using the InitializeTimer function. Then once per update loop you call the UpdateTimer on the object. Then after that you call the CheckTimer when you want to see if the Timer is up. If it is, it will reset itself for the next round. If you want to reset the timer before it hits the time it has been set to without changing that value, call the ResetTimer function. If you want to to change the timer to a new value call
ChangeTimer and pass the seconds you want to change the timer to which also resets the timer at the same time.

By the way if you have a better way of doing this or any suggestions/errors you find, please let me know, I'm open to all possibilities. :)

 

Code: (glbasic) [Select]
TYPE GameObjectTimer

initialTime = 0
deltaTime = 0
countTime = 0
pausedTime = 0
elapsedTime = 0
paused = FALSE

// Initialize the amount of time you want to pass before the trigger
FUNCTION InitializeTimer: countTo
self.initialTime = GETTIMERALL()
self.countTime = countTo
self.paused = FALSE
ENDFUNCTION

// Call once per update loop
FUNCTION UpdateTimer:
IF self.paused = FALSE
self.deltaTime = GETTIMERALL()-self.initialTime
ENDIF
ENDFUNCTION

// Call after the UpdateTimer. This needs to be called or the timer will never reset
FUNCTION CheckTimer:
IF self.paused = FALSE
IF INTEGER(self.deltaTime/1000) >= self.countTime
self.ResetTimer()
RETURN TRUE
ENDIF
ENDIF
ENDFUNCTION

// If you want to reset the timer but keep the orignal value for your check, use this
FUNCTION ResetTimer:

self.initialTime = GETTIMERALL()

ENDFUNCTION

// If you want to change the value that gets checked on, it also calls ResetTimer()
FUNCTION ChangeTimer: startTime

self.countTime = startTime
self.ResetTimer()
ENDFUNCTION

//Call this function when you pause the game passing in TRUE to pause and FALSE to unpause it
FUNCTION PauseTimer: isPaused
IF isPaused
self.pausedTime = GETTIMERALL()
self.paused = TRUE
ELSE
self.elapsedTime = GETTIMERALL()-self.pausedTime
self.initialTime = self.initialTime+self.elapsedTime
self.paused = FALSE
ENDIF

ENDFUNCTION

ENDTYPE


Sample Program

Code: (glbasic) [Select]
LOCAL testTime AS GameObjectTimer
LOCAL pauseTheTime = FALSE
LOCAL PkeyPressed = FALSE
LOCAL UkeyPressed = FALSE

testTime.InitializeTimer(10)

WHILE NOT KEY(01)

IF KEY(25) AND PkeyPressed = FALSE
testTime.PauseTimer(TRUE)
PkeyPressed = TRUE
ENDIF

IF NOT KEY(25)
PkeyPressed = FALSE
ENDIF

IF KEY(22) AND UkeyPressed = FALSE
testTime.PauseTimer(FALSE)
UkeyPressed = TRUE
ENDIF

IF NOT KEY(22)
UkeyPressed = FALSE
ENDIF

testTime.UpdateTimer()
PRINT testTime.deltaTime,10,10
PRINT testTime.initialTime,10,20
PRINT testTime.pausedTime,10,30
testTime.CheckTimer()
SHOWSCREEN
WEND

END
Owlcat has wise