Hi, I'm creating lua code and need to add delay. Is there any way to call such a function? Sorry for my english.
I am doing a modification to the dynamic turn signals and I need to be able to delay them temporarily
What you're looking for is a timer. BeamNG has a global called timer, but I have no experience with using it. There's also this: Code: -- This Source Code Form is subject to the terms of the bCDDL, v. 1.1. -- If a copy of the bCDDL was not distributed with this -- file, You can obtain one at http://beamng.com/bCDDL-1.1.txt -- this is a little helper that lets you schedule things into the future -- works like `schedule()` -- created by BeamNG --[[ example usage: local events = require('timeEvents').create() -- print sth in 5 seconds events:addEvent( 5, function() print ("hello world!") end) -- update the module in the main update loop: events:process(dtSim) --]] local M = {} local mt = {} mt.__index = mt function mt:addEvent(time, fn) table.insert(self, {fn = fn, time = time}) end function mt:clear() table.clear(self) end function mt:process(dt) local size = #self for i = size, 1, -1 do self[i].time = self[i].time - dt if self[i].time < 0 then self[i].fn() table.remove(self, i) end end end function M.create() local data = {} setmetatable(data, mt) return data end return M
I don't know a lot about dynamic signals in BeamNG, I prefer driving/modding older vehicles BeamNG has a built in function 'dt' which can be used for timers indeed. It's frame rate depending as opposed to the pc's internal clock or os clock. To make use of it you should place a local variable in your lua file. Since your file will probably be a lua table (like all files starting with local M = {} ) this variable is local in the complete file hierarchy but 'global' in your lua table. They are mostly called updateTimer by the devs. For clarity let's call it myTimer in this example. Once the variable is declared we give it properties in the updateGFX function and so it somewhat 'acts like a clock'. Then you can link an argument (if .... then ....or so) to the timer that's just created. Code: -- This Source Code Form is subject to the terms of the bCDDL, v. 1.1. -- If a copy of the bCDDL was not distributed with this -- file, You can obtain one at http://beamng.com/bCDDL-1.1.txt local M = {} local myTimer = 0 local function updateGFX(dt) myTimer = myTimer + dt if myTimer > 0.5 then ... end end -- public interface M.updateGFX = updateGFX return M Notice that the 3 dots (...) in the example need to be replaced by your preferred action(s) ! You most probably want to start and reset your timer too in relation to what your signals are doing. So that would look (to me) like If my signal is on I start the timer, after a while the signals actually work, if they go off again i reset the timer Code: -- This Source Code Form is subject to the terms of the bCDDL, v. 1.1. -- If a copy of the bCDDL was not distributed with this -- file, You can obtain one at http://beamng.com/bCDDL-1.1.txt local M = {} local myTimer = 0 local function updateGFX(dt) if ... then -- here instead of ... you turn on your signal myTimer = myTimer + dt else myTimer = myTimer end if myTimer > 0.5 then ... -- here instead of ... the actual signal starts to work end if ... then -- here instead of ... you declare the signal is deactivated myTimer = myTimer myTimer = 0 end end -- public interface M.updateGFX = updateGFX return M You might want to do something entirely different with the signals but this should make clear how the update timer works in BeamNG.