Difference between revisions of "Global Command: getTime"
From The Official Visionaire Studio: Adventure Game Engine Wiki
Line 1: | Line 1: | ||
− | + | Returns the number of milliseconds since the "getTime()" function was first called or since the timer was reset. | |
− | |||
− | |||
− | |||
− | |||
− | |||
+ | Use this command for relative time measurements; for example: call "getTime()" at the beginning of an action and then again when the action has finished to get the time taken to perform said action. | ||
− | |||
− | |||
− | + | == Syntax == | |
− | Syntax | + | <syntaxhighlight lang="lua"> |
− | <syntaxhighlight> | + | getTime(flags) |
− | getTime( | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | == Parameters == | |
− | |||
− | |||
− | |||
− | - | + | {| class="ts" |
− | + | |- | |
− | + | ! style="width:15%" | Parameter | |
− | + | ! style="width:15%" colspan="2" | Type/Structure | |
− | + | ! Description | |
− | + | |- | |
− | + | | rowspan="2" | flags | |
+ | | rowspan="2" | table | ||
+ | | flags = 1 | ||
+ | | indicates flags table | ||
+ | |- | ||
+ | | reset (bool) | ||
+ | | If true the timer is reset back to zero. If the "flags" parameter is omitted, this defaults to false. | ||
+ | |} | ||
− | |||
− | |||
+ | == Return values == | ||
+ | {| class="ts" | ||
+ | |- | ||
+ | ! style="width:15%" | Type | ||
+ | ! Description | ||
+ | |- | ||
+ | | int | ||
+ | | Returns the number of milliseconds since the command was first called or since the timer was reset. | ||
+ | |} | ||
− | |||
− | + | == Examples == | |
+ | '''Example 1:''' Execute an action based on how much time elapsed since the game started or since the action was last executed. | ||
+ | <syntaxhighlight lang="lua"> | ||
+ | -- Call getTime() to start time measuring initially | ||
+ | getTime() | ||
− | + | -- If more than 3 seconds elapsed, do something and reset the timer to start counting from zero again; | |
+ | -- this function would be most effective if included in a "mainLoop" event handler | ||
+ | function checkTime() | ||
+ | if getTime() > 3000 then | ||
+ | startAction(Actions["cock_a_doodle_doo"]) | ||
+ | getTime({flags=1, reset=true}) | ||
+ | end | ||
+ | end | ||
− | |||
− | |||
− | + | </syntaxhighlight> | |
− | + | {{toc}} | |
− | |||
− | |||
− |
Revision as of 19:07, 2 May 2023
Returns the number of milliseconds since the "getTime()" function was first called or since the timer was reset.
Use this command for relative time measurements; for example: call "getTime()" at the beginning of an action and then again when the action has finished to get the time taken to perform said action.
Syntax
getTime(flags)
Parameters
Parameter | Type/Structure | Description | |
---|---|---|---|
flags | table | flags = 1 | indicates flags table |
reset (bool) | If true the timer is reset back to zero. If the "flags" parameter is omitted, this defaults to false. |
Return values
Type | Description |
---|---|
int | Returns the number of milliseconds since the command was first called or since the timer was reset. |
Examples
Example 1: Execute an action based on how much time elapsed since the game started or since the action was last executed.
-- Call getTime() to start time measuring initially
getTime()
-- If more than 3 seconds elapsed, do something and reset the timer to start counting from zero again;
-- this function would be most effective if included in a "mainLoop" event handler
function checkTime()
if getTime() > 3000 then
startAction(Actions["cock_a_doodle_doo"])
getTime({flags=1, reset=true})
end
end