Reputation: 227
Is there a way to measure a certain time in the game? For example, i want to measure the time after the player pressed play button and until he lost/won the game. I know i should use os.time() but how to measure the time in the middle of the game?So would it be like this:
local startTime=os.time()
local gameStart = os.time()-startTime
local gameEnd = os.time()-gameStart
the gameStart is the time when the player presses the start button. the gameEnd is the time when the player is playing the game till he lost or won.
Upvotes: 0
Views: 169
Reputation: 1783
Yeah, that's totally correct.
Whenever you want to measure a time interval just subtract the startTime
from the current time. However, be sure to use the system.getTimer()
function as it has millisecond precision:
local startTime = system.getTimer()
-- some time later ...
local interval = system.getTimer() - startTime
On a side note, according to the Lua 5.1 Reference there's a good reason to avoid subtracting dates:
The returned value is a number, whose meaning depends on your system. In POSIX, Windows, and some other systems, this number counts the number of seconds since some given start time (the "epoch"). In other systems, the meaning is not specified, and the number returned by
time
can be used only as an argument todate
anddifftime
.
Upvotes: 1