Can't create timer

Discussion in 'Programming' started by Occam's Razer, Nov 13, 2015.

  1. Occam's Razer

    Occam's Razer
    Expand Collapse

    Joined:
    Aug 7, 2013
    Messages:
    1,260
    Does anyone know how to create a timer in Lua? I've been following this guide, but I keep being told that "start" is a nil value.
     
  2. tdev

    tdev
    Expand Collapse
    Developer
    BeamNG Team

    Joined:
    Aug 3, 2012
    Messages:
    3,085
    code pls? :)
     
  3. Scepheo

    Scepheo
    Expand Collapse

    Joined:
    Feb 10, 2015
    Messages:
    601
    That's a guide to using the timer that's part of the libraries on the TI-Nspire, and isn't a part of standard Lua. Are you programming on the TI-Nspire, or for BeamNG (or something else)?
     
  4. Occam's Razer

    Occam's Razer
    Expand Collapse

    Joined:
    Aug 7, 2013
    Messages:
    1,260
    Damn, I thought I actually found some tangible documentation on lua. Yeah, this is for BeamNG.

    Code:
    local M = {}
    
    print("Hello World!")
    
    local function onInit()
        electrics.values['input'] = 0
        timer.start(0.5)
    end
    
    local function onTimer()
        electrics.values['input'] = 1
    end
    
    M.onInit      = onInit
    M.onTimer      = onTimer
    
    return M
    But as Scepheo mentioned above, the reference I got this from is not valid for vanilla lua. The print line is irrelevant, except perhaps to underline my incompetence at coding.
     
  5. Scepheo

    Scepheo
    Expand Collapse

    Joined:
    Feb 10, 2015
    Messages:
    601
    If you're writing this script for a scenario, there's the onRaceTick (I'm not 100% sure on the name, and can't check right now) callback. It gets passed the time difference as the first parameter, so you can do this:
    Code:
    local M = {}
    
    local timer = 0
    local timerTrigger = 0.5
    
    local function onTimer()
        -- Blablabla
    end
    
    local function onRaceTick(dt)
        timer = timer + dt
        if timer > timerTrigger then
            timer = timer - timerTrigger
            onTimer()
        end
    end
    
    M.onRaceTick = onRaceTick
     
  6. Occam's Razer

    Occam's Razer
    Expand Collapse

    Joined:
    Aug 7, 2013
    Messages:
    1,260
    No, sadly, not for a scenario. Just a vehicle. One word of note is that the timer is intended to trigger an action that will occur repeatedly. Would it be possible to use the method used for turn signals?
     
  7. Scepheo

    Scepheo
    Expand Collapse

    Joined:
    Feb 10, 2015
    Messages:
    601
    For a vehicle you can do the exact same thing, except with the "update" or "updateGFX" function, as both are passed the time difference with the previous call. If I'm not mistaken, "update" will be called every physics frame, and "updateGFX" will be called every graphics frame, so the latter will usually do.
     
    • Like Like x 1
  8. Occam's Razer

    Occam's Razer
    Expand Collapse

    Joined:
    Aug 7, 2013
    Messages:
    1,260
    Thanks, got it working now. Had a bit of trouble with delta time, but it's doing what it should now.
     
  9. tdev

    tdev
    Expand Collapse
    Developer
    BeamNG Team

    Joined:
    Aug 3, 2012
    Messages:
    3,085
    Thanks for helping Scepheo :)
     
  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