Solmyr
Ultra Cool Member
- Reaction score
- 30
Gametime
Introduction:Gametime is a snippet that is used to provide a standardized platform for tracking the duration of the game using a global timer and a time counter.
It is written in Zinc and requires the Jass NewGen Pack along with the latest version of JassHelper.
It is written in Zinc and requires the Jass NewGen Pack along with the latest version of JassHelper.
Credits:
- Vexorian for Zinc / JassHelper and showing that timers with high timeouts can be a bit inaccurate.
- All of the contributors to the NewGen editor.
The snippet:
JASS:
/**
* GAMETIME
* written by: Solmyr
*
* Requires the latest version of JassHelper.
*
* This is just an ordinary snippet that you might find useful.
*
* The main purpose is to simply provide a standardized platform for tracking the
* duration of the game using a global timer and a time counter.
* It is done the way it is because timers with high expiration times can
* a bit inaccurate.
*
* The output can be:
*
* 1. A real that is returned by the Time.getExact(), the Time.getRounded() and the
* Time.getDecimal() functions. The first two return the whole elapsed game time,
* either in an exact accurate or a rounded manner, while the third one returns the
* decimal part of the currently elapsed time.
*
* 2. An integer that is returned by the Time.getSeconds() function. The integer
* equals the amount of fully elapsed seconds.
*
* 3. A string that is returned by the Time.getStamp() function. The string represents
* a timestamp in the HH:MM:SS format.
*
*/
//! zinc
library Gametime {
public struct Time extends array {
private {
static timer mainTimer = CreateTimer();
static integer timeCounter = 0;
static string timeStamp = "";
}
/* All of the following static methods are inline-friendly, except for getStamp(). */
static method getSeconds() -> integer {
return thistype.timeCounter;
}
static method getDecimal() -> real {
return TimerGetElapsed(thistype.mainTimer);
}
static method getExact() -> real {
return (TimerGetElapsed(thistype.mainTimer) + thistype.timeCounter);
}
static method getRounded() -> real {
return ((R2I(thistype.getExact() + 0.5)) * 1.0);
}
static method getStamp() -> string {
integer seconds = R2I(thistype.getExact() + 0.5), minutes = 0, hours = 0;
thistype.timeStamp = "";
hours = (seconds / 3600);
seconds -= (3600 * hours);
minutes = (seconds / 60);
seconds -= (60 * minutes);
if (hours < 10) { thistype.timeStamp = "0"; }
thistype.timeStamp += I2S(hours) + ":";
if (minutes < 10) { thistype.timeStamp += "0"; }
thistype.timeStamp += I2S(minutes) + ":";
if (seconds < 10) { thistype.timeStamp += "0"; }
return (thistype.timeStamp + I2S(seconds));
}
private static method onInit() {
TimerStart(thistype.mainTimer, 1.00, true, static method() { timeCounter += 1; });
}
}
}
//! endzinc
This snippet provides the following functions (static methods) to the user:
- [ljass]function Time.getExact takes nothing returns real[/ljass]
- [ljass]function Time.getRounded takes nothing returns real[/ljass]
- [ljass]function Time.getDecimal takes nothing returns real[/ljass]
- [ljass]function Time.getSeconds takes nothing returns integer[/ljass]
- [ljass]function Time.getStamp takes nothing returns string[/ljass]