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.
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)?
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.
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
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?
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.