1. Game not starting lately?
    Fixed in
    More information available here

    Dismiss Notice
  2. Intel iGPU (6xx series) crashes
    Fixed drivers available!
    Instructions here

    Dismiss Notice

Quantizing LUA dt function with OS clock

Discussion in 'Utilities and programming' started by NOCARGO, Mar 17, 2020.


    Expand Collapse

    Apr 1, 2019
    As quite a few here on the forums know the dt (difference in time or delta time ?) function in LUA
    is depending on frame rate (fps) and is not an actual clock device like the one sitting in the pc itself.
    Using LUA we can call the OS clock in certain ways but they are limited. It's very hard (if not impossible)
    to use the OS clock as a timer, right ? I AM right am I ? Anyway it's been bothering me for quite a while
    so I have been doing some experiments. I wanted to be able to create a timer ( that also is capable of
    displaying tenths of a second) that doesn't loose track of real time because of the fps not being accurate
    enough to act as a true clock. So I tried different things and now something useful (at least to me) seems to have come out of it. I thought it would be interesting to share. I don't know if I did something special by achieving this but I never saw anyone coming up with a solution in this way.

    What you are looking at on the vid is a clock with the hand that shows the seconds running backwards.
    That is just for fun and for my brains not getting too bored by staring at it ! :D
    Now the interesting part, the hands of hour and minute are just using the OS clock data. But,
    the hand of the seconds is moving with LUA's dt function. To keep it running synchronized with the
    real clock I shortly 'inject' it with a time stamp from the OS clock once on init and also every time it transits
    zero. This way it always stays on trajectory if compared with real time. You can even see it correcting
    itself during volatile frame rate, sometimes a little harsh but mostly quite smooth.
    The vid :
    1- you see it running with soft transition
    2- I press ctrl+r to show it starts in the right position
    3- I do the same as in 2 (press ctr+r again)
    4- a harsh transition after 'being forced into os time again'
    5- a soft transition again
    6- if the hand suddenly jumps into a complete different direction it means that the streamable vid is
    repeating itself ! :D

    So from here on I guess over time I will be able to divide the movement in increments of my choice,
    such as half seconds or 0.25 or tenths, hundredths and maybe thousandths. And I guess it might be
    useful for other tasks too of course. :)

    edit : in my case personal the arrow represents a digital timer I'm making for my mod

    Here's the code :
    local M = {}
    local clockTimer = 0
    local handPos = 0
    local function onInit()
        electrics.values['clockh'] = 0
        electrics.values['clockmin'] = 0
        electrics.values['clocksec'] = 0
    local function reset()
    local function updateGFX(dt)
        clockTimer = clockTimer + dt
        local time = os.date("*t", os.time())
        local hour = time.hour % 60
        electrics.values['clockh'] = hour*30
        electrics.values['clockmin'] = time.min*6
        if clockTimer > 0.075 then handPos = handPos + (dt * 1) -- 0.075 seems to work, 0.05 seems to fast
        else handPos = time.sec
        if handPos > 60 then handPos = 0 clockTimer = 0 end
        electrics.values['clocksec'] = handPos*6
    -- public interface
    M.onInit    = onInit
    M.onReset   = reset
    M.updateGFX = updateGFX
    return M
    #1 NOCARGO, Mar 17, 2020
    Last edited: Mar 18, 2020
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice