Time: Difference between revisions
Initial addition of script API for system time. |
No edit summary |
||
| Line 127: | Line 127: | ||
==== Get Current Unix Timestamp ==== | ==== Get Current Unix Timestamp ==== | ||
Returns the current system time as the number of seconds elapsed since | Returns the current system time as the number of seconds elapsed since <code>1970-01-01 00:00:00 UTC</code>. | ||
<syntaxhighlight lang="c"> | <syntaxhighlight lang="c" line="1"> | ||
int timestamp = datetime_gettimestamp(); | int timestamp = datetime_gettimestamp(); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 135: | Line 135: | ||
==== Get Current Unix Timestamp in Milliseconds ==== | ==== Get Current Unix Timestamp in Milliseconds ==== | ||
Returns the current system time as the number of milliseconds elapsed since | Returns the current system time as the number of milliseconds elapsed since <code>1970-01-01 00:00:00 UTC</code>. | ||
'''Note:''' Millisecond Unix timestamps require a 64-bit integer value. | '''Note:''' Millisecond Unix timestamps require a 64-bit integer value. | ||
<syntaxhighlight lang="c"> | <syntaxhighlight lang="c" line="1"> | ||
int timestamp = datetime_gettimestampms(); | int timestamp = datetime_gettimestampms(); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 145: | Line 145: | ||
==== Format Current Local Date and Time ==== | ==== Format Current Local Date and Time ==== | ||
Formats the current system time as a string using the host system's local time standard. | Formats the current system time as a string using the host system's local time standard. The format string uses standard strftime conversion codes. | ||
'''Note:''' Names such as weekdays and months may vary according to the host system's configured language and locale. | |||
<syntaxhighlight lang="c"> | |||
string date_time = datetime_format("%Y-%m-%d %H:%M:%S"); | |||
</syntaxhighlight> | |||
The following examples demonstrate several common formats: | |||
<syntaxhighlight lang="c"> | <syntaxhighlight lang="c"> | ||
/* | |||
* ISO-style date and 24-hour time: | |||
* 2026-07-12 14:35:08 | |||
*/ | |||
string date_time = datetime_format("%Y-%m-%d %H:%M:%S"); | string date_time = datetime_format("%Y-%m-%d %H:%M:%S"); | ||
/* | |||
* Month, day, and year: | |||
* 07/12/2026 | |||
*/ | |||
void date = datetime_format("%m/%d/%Y"); | |||
/* | |||
* Full weekday and month names: | |||
* Sunday, July 12, 2026 | |||
*/ | |||
void written_date = datetime_format("%A, %B %d, %Y"); | |||
/* | |||
* Abbreviated weekday and month names: | |||
* Sun, Jul 12, 2026 | |||
*/ | |||
void short_date = datetime_format("%a, %b %d, %Y"); | |||
/* | |||
* 12-hour time with AM or PM: | |||
* 02:35:08 PM | |||
*/ | |||
void time_12_hour = datetime_format("%I:%M:%S %p"); | |||
/* | |||
* 24-hour time: | |||
* 14:35:08 | |||
*/ | |||
void time_24_hour = datetime_format("%H:%M:%S"); | |||
/* | |||
* Year, month, day, hour, and minute: | |||
* 20260712_1435 | |||
* | |||
* Useful for save names, logs, and generated filenames. | |||
*/ | |||
void file_timestamp = datetime_format("%Y%m%d_%H%M"); | |||
/* | |||
* Day of the year: | |||
* Day 193 of 2026 | |||
*/ | |||
void year_progress = datetime_format("Day %j of %Y"); | |||
/* | |||
* Weekday name: | |||
* Sunday | |||
*/ | |||
void weekday = datetime_format("%A"); | |||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 155: | Line 217: | ||
Formats a supplied Unix timestamp expressed in milliseconds. | Formats a supplied Unix timestamp expressed in milliseconds. | ||
<syntaxhighlight lang="c"> | <syntaxhighlight lang="c" line="1"> | ||
int timestamp = 1783890000000; | int timestamp = 1783890000000; | ||
string date_time = datetime_format( | |||
string date_time = datetime_format("%Y-%m-%d %H:%M:%S", timestamp); | |||
); | |||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 167: | Line 227: | ||
Formats a supplied Unix timestamp using Coordinated Universal Time instead of the host system's local time. | Formats a supplied Unix timestamp using Coordinated Universal Time instead of the host system's local time. | ||
<syntaxhighlight lang="c"> | <syntaxhighlight lang="c" line="1"> | ||
int timestamp = datetime_gettimestampms(); | int timestamp = datetime_gettimestampms(); | ||
string date_time = datetime_format( | string date_time = datetime_format("%Y-%m-%d %H:%M:%S", timestamp, DATETIME_STANDARD_UTC); </syntaxhighlight> | ||
"%Y-%m-%d %H:%M:%S", | |||
timestamp, | |||
DATETIME_STANDARD_UTC | |||
); </syntaxhighlight> | |||
==== Get Current Year ==== | ==== Get Current Year ==== | ||
| Line 180: | Line 236: | ||
Returns the current four-digit year using the host system's local time standard. | Returns the current four-digit year using the host system's local time standard. | ||
<syntaxhighlight lang="c"> | <syntaxhighlight lang="c" line="1"> | ||
int year = datetime_getyear(); | int year = datetime_getyear(); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 188: | Line 244: | ||
Returns the current calendar month as an integer. | Returns the current calendar month as an integer. | ||
<syntaxhighlight lang="c"> | <syntaxhighlight lang="c" line="1"> | ||
int month = datetime_getmonth(); | int month = datetime_getmonth(); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 196: | Line 252: | ||
Returns the current day of the month as an integer. | Returns the current day of the month as an integer. | ||
<syntaxhighlight lang="c"> | <syntaxhighlight lang="c" line="1"> | ||
int day = datetime_getday(); | int day = datetime_getday(); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 204: | Line 260: | ||
Returns the current hour using the host system's local time standard. | Returns the current hour using the host system's local time standard. | ||
<syntaxhighlight lang="c"> | <syntaxhighlight lang="c" line="1"> | ||
int hour = datetime_gethour(); | int hour = datetime_gethour(); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 212: | Line 268: | ||
Returns the current minute of the hour. | Returns the current minute of the hour. | ||
<syntaxhighlight lang="c"> | <syntaxhighlight lang="c" line="1"> | ||
int minute = datetime_getminute(); | int minute = datetime_getminute(); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 220: | Line 276: | ||
Returns the current second of the minute. | Returns the current second of the minute. | ||
<syntaxhighlight lang="c"> | <syntaxhighlight lang="c" line="1"> | ||
int second = datetime_getsecond(); | int second = datetime_getsecond(); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 228: | Line 284: | ||
Each component function optionally accepts a Unix timestamp expressed in milliseconds. This allows creators to extract individual date and time values from a stored timestamp. | Each component function optionally accepts a Unix timestamp expressed in milliseconds. This allows creators to extract individual date and time values from a stored timestamp. | ||
<syntaxhighlight lang="c"> | <syntaxhighlight lang="c" line="1"> | ||
int timestamp = 1783890000000; | int timestamp = 1783890000000; | ||
| Line 242: | Line 298: | ||
Pass `DATETIME_STANDARD_UTC` as the second argument to interpret the supplied timestamp as Coordinated Universal Time. | Pass `DATETIME_STANDARD_UTC` as the second argument to interpret the supplied timestamp as Coordinated Universal Time. | ||
<syntaxhighlight lang="c"> | <syntaxhighlight lang="c" line="1"> | ||
int timestamp = datetime_gettimestampms(); | int timestamp = datetime_gettimestampms(); | ||
int year = datetime_getyear( | int year = datetime_getyear(timestamp, DATETIME_STANDARD_UTC); | ||
timestamp, | |||
DATETIME_STANDARD_UTC | |||
); | |||
int month = datetime_getmonth( | int month = datetime_getmonth(timestamp, DATETIME_STANDARD_UTC); | ||
timestamp, | |||
DATETIME_STANDARD_UTC | |||
); | |||
int day = datetime_getday( | int day = datetime_getday(timestamp, DATETIME_STANDARD_UTC); | ||
timestamp, | |||
DATETIME_STANDARD_UTC | |||
); | |||
int hour = datetime_gethour( | int hour = datetime_gethour(timestamp, DATETIME_STANDARD_UTC); </syntaxhighlight> | ||
timestamp, | |||
DATETIME_STANDARD_UTC | |||
); </syntaxhighlight> | |||
==== Use Current Time with an Explicit Time Standard ==== | ==== Use Current Time with an Explicit Time Standard ==== | ||
| Line 269: | Line 313: | ||
Pass `NULL()` for the optional timestamp argument when selecting a time standard without supplying a custom timestamp. | Pass `NULL()` for the optional timestamp argument when selecting a time standard without supplying a custom timestamp. | ||
<syntaxhighlight lang="c"> | <syntaxhighlight lang="c" line="1"> | ||
int utc_hour = datetime_gethour( | int utc_hour = datetime_gethour(NULL(), DATETIME_STANDARD_UTC); | ||
); | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Revision as of 18:56, 12 July 2026
Introduction
Timing is crucial to OpenBOR, as it is to most game engines. Understanding how the engine measures time, and how those measurements affect a project, is therefore essential.
OpenBOR’s primary gameplay timing unit is one-half of a centisecond, equivalent to 5 milliseconds. Animation delays, for example, are expressed as integer increments of 0.5 centiseconds. Legacy documentation often refers to these increments as centiseconds, though each increment is technically only half a centisecond. Other units are used where appropriate for particular features, circumstances, or design requirements.
Time in OpenBOR is represented by the following primary definitions:
Game Time: The traditional beat ’em up countdown timer used to maintain the pace of a level. When Game Time expires, players ordinarily lose a life. Game Time is enabled by default, though creators may disable, remove, or otherwise modify it.
Elapsed Time: An integer value reset at the beginning of each active level and incremented once for every processed logical tick. OpenBOR processes 200 logical ticks per second, making each Elapsed Time increment equal to 5 milliseconds, or one-half of a centisecond. Elapsed Time drives much of the engine’s internal timing, including animations, delays, effect durations, and other gameplay processes.
Ticks: The number of milliseconds elapsed since the engine timer began. Tick values are independent of the active level and may be used for timing operations both inside and outside gameplay, including loading processes, recorded player input, and custom script timers. Ticks are generally preferable to manually incrementing a script variable when timing must remain independent of script execution frequency.
System Time: OpenBOR’s scripting system includes an API for accessing the host system clock and obtaining real-world dates and times. System Time access is entirely optional and does not affect native engine timing or gameplay functionality.

Game Time
Game time is a default countdown timer that appears at top center of the screen during an active level. It starts at 99 and decrements once every two real world seconds. At 0, all active players take damage equal to their current HP, and the game is over if no lives or credits remain. Time resets automatically when players clear a designated level wait and at the start of each new level.

Set Time
settime {int time}
#default
settime 99
Level header command that sets value applied to timer on level start and native resets.
- Accepts any integer value from
0to99. Out of bounds values are ignored. 0= infinite time.
HUD Display
For configuring how timer displays on screen, see Heads Up Display.
Other
data/sounds/timeover.wav
If available, plays once when the game timer expires.
anim lose
If available, entity plays this animation in place of normal death when game timer expires.
- Accessible to script as
openborconstant("ANI_LOSE").
anim falllose
If available and entity does not have a lose animation, entity plays this animation in place of normal fall death when game timer expires.
- Accessible to script as
openborconstant("ANI_FALLLOSE").
Attack types
Attack types applied to players when game time expires.
- openborconstant("ATK_TIMEOVER") - Applied to players without a lose animation.
- openborconstant("ATK_LOSE") - Applied to players with a lose animation.
Script API
Game Time
Current game time is read and write accessible.
Get
int time = openborvariant("game_time");
Get integer representing game time remaining.
Set
int new_time = 50;
setopenborvariat("game_time", new_time);
Set game time remaining.
Timetick Event
If available, data/scripts/timetick.c fires on each game time decrement. Populates the following local variables.
time- Current game time value.
Elapsed Time
In terms of game mechanics, elapsed time is the most important and frequently employed time measurement as it represents OpenBOR's logical clock, though its implementation is relatively simple. For each level, a global elapsed-time counter begins at 0. OpenBOR’s logical clock runs at 200 ticks per second, and the counter increments by 1 for every processed logic tick.
Each logical tick represents 5 milliseconds, or one-half of a centisecond. Therefore, two elapsed-time ticks equal one centisecond, while 200 ticks equal one second.
OpenBOR’s logical clock is separate from its outer update and rendering rate. Depending on the selected FPS setting and display synchronization, the engine may run its outer update cycle more frequently than 200 times per second. Available FPS settings include VSync, limits of 200 or 500 FPS, and an unrestricted option.
Some operations, including the global update.c and updated.c script events, execute once per outer update cycle. These scripts may therefore run more frequently than the 200 Hz logical clock. Other operations, such as entity simulation and entity update scripts, execute according to logical ticks.
OpenBOR also keeps logical timing independent from individual rendered frames. This allows the engine to maintain consistent gameplay timing across different FPS settings. Under unusual performance conditions, the engine may process more than one logical tick during an outer update to keep elapsed time synchronized, though this is rarely noticeable in normal use.
Creators should use elapsed time rather than counting script executions when measuring durations.
Most internal timing works by setting an expiration value to the current elapsed time plus a predetermined duration. The engine then compares that expiration value against the elapsed-time counter. Once elapsed time reaches or passes the expiration value, the timed item may complete, expire, continue, or perform whatever other action its system requires.
Note: In case it matters, the current elapsed time variable is a 32bit unsigned integer. That's enough to run a single level continuously for just over eight months before rolling over.
Script API
Get Elapsed Time
int elapsed_time = openborvariant("elapsed_time");
Set Elapsed Time
Be careful about setting elapsed time during game-play, as again, virtually every active game timing property depends on it.
int new_time = 100
setopenborvariant("elapsed_time", new_time);
Ticks
Ticks is a read only measure of milliseconds starting from game boot. OpenBOR natively uses ticks for the player input recording feature and for timing during load screens. Ticks therefore do not generally affect active game-play, but are useful for user content, scripting, and keeping time outside of active game-play elements when increments are not natively incremented.
Script API
Get Ticks
int ticks = openborvariant("ticks");
System Time
System time is entirely optional and provided for creator defined script use. It is not accessed by native engine logic in any way. System is available as a Unix timestamp in either seconds or milliseconds where supported by hardware. Conversion functions allow outputting a custom formatted string or time parts as integer values for use in logic expressions.
Script API
Get Current Unix Timestamp
Returns the current system time as the number of seconds elapsed since 1970-01-01 00:00:00 UTC.
int timestamp = datetime_gettimestamp();
Get Current Unix Timestamp in Milliseconds
Returns the current system time as the number of milliseconds elapsed since 1970-01-01 00:00:00 UTC.
Note: Millisecond Unix timestamps require a 64-bit integer value.
int timestamp = datetime_gettimestampms();
Format Current Local Date and Time
Formats the current system time as a string using the host system's local time standard. The format string uses standard strftime conversion codes.
Note: Names such as weekdays and months may vary according to the host system's configured language and locale.
string date_time = datetime_format("%Y-%m-%d %H:%M:%S");
The following examples demonstrate several common formats:
/*
* ISO-style date and 24-hour time:
* 2026-07-12 14:35:08
*/
string date_time = datetime_format("%Y-%m-%d %H:%M:%S");
/*
* Month, day, and year:
* 07/12/2026
*/
void date = datetime_format("%m/%d/%Y");
/*
* Full weekday and month names:
* Sunday, July 12, 2026
*/
void written_date = datetime_format("%A, %B %d, %Y");
/*
* Abbreviated weekday and month names:
* Sun, Jul 12, 2026
*/
void short_date = datetime_format("%a, %b %d, %Y");
/*
* 12-hour time with AM or PM:
* 02:35:08 PM
*/
void time_12_hour = datetime_format("%I:%M:%S %p");
/*
* 24-hour time:
* 14:35:08
*/
void time_24_hour = datetime_format("%H:%M:%S");
/*
* Year, month, day, hour, and minute:
* 20260712_1435
*
* Useful for save names, logs, and generated filenames.
*/
void file_timestamp = datetime_format("%Y%m%d_%H%M");
/*
* Day of the year:
* Day 193 of 2026
*/
void year_progress = datetime_format("Day %j of %Y");
/*
* Weekday name:
* Sunday
*/
void weekday = datetime_format("%A");
Format a Unix Timestamp
Formats a supplied Unix timestamp expressed in milliseconds.
int timestamp = 1783890000000;
string date_time = datetime_format("%Y-%m-%d %H:%M:%S", timestamp);
Format Date and Time as UTC
Formats a supplied Unix timestamp using Coordinated Universal Time instead of the host system's local time.
int timestamp = datetime_gettimestampms();
string date_time = datetime_format("%Y-%m-%d %H:%M:%S", timestamp, DATETIME_STANDARD_UTC);
Get Current Year
Returns the current four-digit year using the host system's local time standard.
int year = datetime_getyear();
Get Current Month
Returns the current calendar month as an integer.
int month = datetime_getmonth();
Get Current Day
Returns the current day of the month as an integer.
int day = datetime_getday();
Get Current Hour
Returns the current hour using the host system's local time standard.
int hour = datetime_gethour();
Get Current Minute
Returns the current minute of the hour.
int minute = datetime_getminute();
Get Current Second
Returns the current second of the minute.
int second = datetime_getsecond();
Get Date and Time Parts from a Unix Timestamp
Each component function optionally accepts a Unix timestamp expressed in milliseconds. This allows creators to extract individual date and time values from a stored timestamp.
int timestamp = 1783890000000;
int year = datetime_getyear(timestamp);
int month = datetime_getmonth(timestamp);
int day = datetime_getday(timestamp);
int hour = datetime_gethour(timestamp);
int minute = datetime_getminute(timestamp);
int second = datetime_getsecond(timestamp);
Get Date and Time Parts as UTC
Pass `DATETIME_STANDARD_UTC` as the second argument to interpret the supplied timestamp as Coordinated Universal Time.
int timestamp = datetime_gettimestampms();
int year = datetime_getyear(timestamp, DATETIME_STANDARD_UTC);
int month = datetime_getmonth(timestamp, DATETIME_STANDARD_UTC);
int day = datetime_getday(timestamp, DATETIME_STANDARD_UTC);
int hour = datetime_gethour(timestamp, DATETIME_STANDARD_UTC);
Use Current Time with an Explicit Time Standard
Pass `NULL()` for the optional timestamp argument when selecting a time standard without supplying a custom timestamp.
int utc_hour = datetime_gethour(NULL(), DATETIME_STANDARD_UTC);