Difference between revisions of "Global Command: getTime"

From The Official Visionaire Studio: Adventure Game Engine Wiki
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
{| class="ts" style="width:100%"
+
Returns the number of milliseconds since the "getTime()" function was first called or since the timer was reset.
|-
 
! style="text-align:left" | Function History
 
|-
 
| Available since v3.0
 
|}
 
  
 +
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.
  
Returns the number of milliseconds since the command was first called - or since the timer was reset! <br/>
 
Use this command for relative time measurements; for example: call getTime() at the beginning of an action & 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({flags=1, reset})
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
  
Example:
+
== Parameters ==
<syntaxhighlight>
 
-- let's store the current time in a variable & then check it against current time!
 
local initialTime = getTime()
 
  
-- we'll use this function to check current time elapsed & decided what action to perform!
+
{| class="ts"
function checkTime()
+
|-
if getTime() >= initialTime + 3000 then -- if current time more than equals initial time + 3 seconds then ...
+
! style="width:15%" | Parameter
  -- do some action!
+
! style="width:15%" colspan="2" | Type/Structure
  getTime({flags=1, reset=true}) -- let's reset timer back to zero!
+
! Description
end
+
|-
end
+
| 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.
 +
|}
  
-- the checkTime() function would be most effective, if included inside a mainLoop event handler function
 
</syntaxhighlight>
 
  
 +
== 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.
 +
|}
  
<span class="bold underline">Arguments</span>
 
  
none
+
== 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()
  
<span class="bold underline">Flags</span>
+
-- 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
  
<span class="bold">r/reset</span> <br/>
 
True resets timer back to zero; false does nothing (if no flags are set then reset will be false by default)
 
  
 
+
</syntaxhighlight>
<span class="bold underline">Return</span>
+
{{toc}}
 
 
<span class="bold">time</span> <br/>
 
The integer value of the current time (in ms)
 

Latest revision as of 11:53, 22 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