Pausing event.time

It may seem strange request but I am controlling my gameloop through system.getTimer and then calculating the time between the last iterations to ensure the main loop is optimised and as smooth as possible. So i am doing something like this:

[lua]tPrevious = system.getTimer()

– then in game loop
local tDelta = event.time - tPrevious
– Update the previous variable for the next time its called
tPrevious = event.time[/lua]

I could do a really clunky calculation and determine the length of system time each time its paused and then subtract from the overall delta but this seems very hacky.

I’m sure as always there is a better way of handling this so would be very pleased to here

J [import]uid: 103970 topic_id: 22413 reply_id: 322413[/import]

Here is what I did:

I have a module called gameClock.lua:

local M = {}  
local pausedTime = 0  
local startTime = 0  
local pauseStartTime = 0  
  
local function init()  
 startTime = os.time()  
 pauseStartTime = 0  
 pausedTime = 0  
end  
M.init = init  
  
local function pause()  
 if pauseStartTime == 0 then  
 pauseStartTime = os.time()  
 end  
end  
M.pause = pause  
  
local function resume()  
 if pauseStartTime \> 0 then  
 pausedTime = pausedTime + os.time() - pauseStartTime  
 pauseStartTime = 0  
 end  
end  
M.resume = resume  
  
local function time()  
 if pauseStartTime \> 0 then  
 return pauseStartTime - startTime - pausedTime  
 else  
 return os.time() - startTime - pausedTime  
 end  
end  
M.time = time  
return M  

In main.lua I have:
gameClock = require( “gameClock” )
gameClock.init()

When the game is paused I call gameClock.pause() and when it is resumed I call gameClock.resume()

Then when I need to use the time passed since the game started I use gameClock.time(). This method substracts the paused time.

If you need finer granularity (seconds was enough for me) you can use system.getTimer() instead of os.time() like I did…
[import]uid: 80469 topic_id: 22413 reply_id: 89434[/import]

@gury.traub this is awesome thanks ever so much for sharing

So in my example where I am inferred the following:
[lua]tPrevious = system.getTimer()
– then in game loop
local tDelta = event.time - tPrevious
– Update the previous variable for the next time its called
tPrevious = event.time[/lua]

I would change to:

[lua]gameClock = require( “gameClock” )
gameClock.init()
– then in game loop
local tDelta = event.time - gameClock.time()[/lua]

or would the last line simply be:

[lua]-- then in game loop
local tDelta = gameClock.time()[/lua]

I understand how you are handling the pause time but have I understood correctly how to handle the standard game loop time handling?

J [import]uid: 103970 topic_id: 22413 reply_id: 89500[/import]