Timer (script)

From Elanthipedia
Jump to navigation Jump to search
Timer (script)
Category information
Front-end Genie
Author User:Shift3


Include this script into your genie scripts to have a timer functionality. Works cross script. To import, save the script as Timer.cmd and type: "include timer.cmd" (without the quotes) at the top of your script.

If anyone else wants it, see below. Just include it into whatever script you're using and then use one of the following commands:

`gosub Timer.Refresh <timername>` - Updates this timer

`gosub Timer.Reset <timername>` - Sets the timer back to 0

`gosub Timer.SetTime <timermane> <seconds>` Sets the time to this value, backdates the start time.

All three functions above will store the response in both the global vars var `$Timer.<timername>.elapsed` and `$Timer.<timername>.start` for when the timer start is in gametime.

- Two local vars `%Timer.lastVarName` and `%Timer.lastVarTime` are refreshed every time one of the three functions above are run.

- If the timer didn't exist then it'll be created as part of the call.

- will update the timer and store the results of the timer in `%Timer.lastVarName` = name of the timer, and `%Timer.lastVarTime` = time in seconds that have elapsed since start up to the last refresh.

I've noticed a variance of 1-3 seconds due to rounding on `$gametime` and processing time.

### import script only - meant to be used in other scripts - uses global variables that persist across sessions ###

goto Timer.Imported


### -------------------------------------------------------------------------------------------------
### ---- Actual timer code
### -------------------------------------------------------------------------------------------------
# starts the timer if it's not set
# call this before reading a timer
# single word timers
# timers are global and not reset when scripts start and shared across scripts
# Common timers: 
	#   - almanac = last time read an almanac
	#   - spider = HE spider gift
	#   - cube = he cube gift

Timer:
	var Timer.lastVarTime -1
	var Timer.lastVarName NULL

	### internal function only ###
	### Calling this in your script creates an infinite loop
	Timer.INTERNAL.Verify.Exists:
		pause .0001
		if !($Timer.%Timer.lastVarName.elapsed >= 0) then {
			put #var Timer.%Timer.lastVarName.start $gametime
			put #var Timer.%Timer.lastVarName.elapsed 0
		}
	return


	### public functions ###
	Timer.Refresh:
		pause .0001
		var Timer.lastVarName $1

		gosub Timer.INTERNAL.Verify.Exists

		MATH Timer.TMP set $gametime
		MATH Timer.TMP subtract $Timer.%Timer.lastVarName.start
		put #var Timer.%Timer.lastVarName.elapsed %Timer.TMP
		var Timer.lastVarTime $Timer.%Timer.lastVarName.elapsed
	return

	#hardcodes a timer with seconds $1 = timer, $2 = seconds
	#example: gosub SetTimer myTimer 200
	#if timer hasn't started, it starts it first
	#sets start time to current - seconds
	Timer.SetTime: 
		pause .0001
		var Timer.lastVarName $1
		var Timer.lastVarTime $2

		gosub Timer.INTERNAL.Verify.Exists
		var Timer.tmp $gametime
		MATH Timer.tmp SUB %Timer.lastVarTime
		put #var Timer.%Timer.lastVarName.start %Timer.tmp
		gosub Timer.Refresh %Timer.lastVarName
	return

	Timer.Reset: 
		pause .0001
		gosub Timer.SetTime $1 0
	return

	### returns 
	Timer.Pause:
		put #echo red >ScriptLog  Pause Timer not implemented

return


### -------------------------------------------------------------------------------------------------
### ----   Unit tests for the functions above; shows usage
### -------------------------------------------------------------------------------------------------
### unit testing ###

20231119.UnitTest:
	gosub 20131119.testTimer alchemyApp Timer.Reset
	gosub 20131119.testTimer alchemyApp Timer.Refresh
	gosub 20131119.testTimer alchemyApp Timer.SetTime 999

	gosub 20131119.testTimer alchemyAppB Timer.Refresh
	gosub 20131119.testTimer alchemyAppB Timer.Reset
	gosub 20131119.testTimer alchemyAppB Timer.Refresh

goto Timer.UnitTestComplete

20131119.TestTimer:
	var tmp $1 $3
	gosub $2 %tmp
	put #echo >Log ---- $2 -----
	put #echo >Log green --- fed: %tmp
	put #echo >Log blue --- name: %Timer.lastVarName
	put #echo >Log yellow --- time: %Timer.lastVarTime
	put #echo >Log pink --- Debbug start: $Timer.%Timer.lastVarName.start  Debug Elapsed: $Timer.%Timer.lastVarName.elapsed
	pause 5
return

### -------------------------------------------------------------------------------------------------

Timer.UnitTestComplete:
Timer.imported: