diedir Posted October 30, 2011 Share Posted October 30, 2011 Hi all i can't figure out a way to create a timer in LUA, with AppTime() function set on a variable, i can't do any( t1 - t2) compare, seems to get always the same values. If i try to UpdateAppTime() on a loop, my game slows down as hell. --before loop starttime = AppTime() ---in loop: current = AppTime() elapsedtime = (current - starttime) Could someone give me a hint on what to do ? Thank you Quote AMD Ryzen 5900HX - Nvidia RTX 3070 - 32 Go - 1To SSD - W11 Link to comment Share on other sites More sharing options...
macklebee Posted October 30, 2011 Share Posted October 30, 2011 look at some of the inherent lua functions: os.difftime() Quote Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590 LE / 3DWS / BMX / Hexagon macklebee's channel Link to comment Share on other sites More sharing options...
Rick Posted October 30, 2011 Share Posted October 30, 2011 Here is what I do for timers in Lua. Below is usage: -- This is the timer method that gets called when the interval is up function MoveTimer(tmr, param1, param2) -- Can stop the timer if I want with the below code StopTimer(tmr) end moveTimer = {} CreateTimer(moveTimer, 50, MoveTimer) AddGlobalTimer(moveTimer, 10, "hello") -- call the below method in your main loop every cycle and it'll manage all the timers for you that you added to AddGlobalTimer() UpdateGlobalTimers() Below is Timers.lua file I add to my project function CreateTimer(tmr, interval, eventHandler) tmr.onTick = eventHandler tmr.lastTime = 0 tmr.interval = interval tmr.enabled = false end function StartTimer(tmr) tmr.lastTime = os.clock() * 1000 tmr.enabled = true end function UpdateTimer(tmr, param1, param2) if tmr.enabled == true then if (os.clock() * 1000) > tmr.lastTime + tmr.interval then tmr.lastTime = os.clock() * 1000 --if param1 ~= nil and param2 == nil then tmr.onTick(tmr, param1, param2) --else -- tmr.onTick(tmr) --end end end end function StopTimer(tmr) tmr.enabled = false end globalTimers = {} globalTimerCount = 1 function AddGlobalTimer(tmr, param1, param2) -- Store off the parameter inside the timer tmr.parameter1 = param1 tmr.parameter2 = param2 -- Add an index ID tmr.id = globalTimerCount -- Insert into global table globalTimers[globalTimerCount] = tmr globalTimerCount = globalTimerCount + 1 --table.insert(globalTimers, tmr) end function RemoveGlobalTimer(tmr) -- Setting to nil removes the entry from the table globalTimers[tmr.id] = nil end function UpdateGlobalTimers() for k,v in pairs(globalTimers) do UpdateTimer(v, v.parameter1, v.parameter2) end end Quote Link to comment Share on other sites More sharing options...
diedir Posted October 31, 2011 Author Share Posted October 31, 2011 Thank you Rick for sharing your code, it will help me (and others) for setting up timers needed by my racing game I guessed it was something like this (classes,methods) but couldn't draw it up by myself, really thank you a lot. i will try to make it work in my scripts. @Macklebee i tried os.time() and os.difftime(t2 - t1) with no luck, perhaps i messed the code for me: putting os.time() in a variable before loop when a key pressed in the loop : passing os.time() to another variable then: os.difftime(second_var, first_var) printing value is always "0" or a random fixed number which never change.... will try Rick's way, LUA is sometimes discouraging... i'll put my tests later Thank you both EDIT : ok i get it to work at last with os.difftime() : put a func : function time() current = os.clock() end ---before loop i initiate startime: starttime = os.clock() ---then in my loop (when key pressed for my purpose) : elapsedtime = os.difftime( os.clock(), starttime) ---here is my diff time show starttime = time() --- re-assign my starttime Finally it works need to see now how i can put more timers mixing with Rick's classes Thank you all ! Quote AMD Ryzen 5900HX - Nvidia RTX 3070 - 32 Go - 1To SSD - W11 Link to comment Share on other sites More sharing options...
Rick Posted October 31, 2011 Share Posted October 31, 2011 Is that returning ms for you? Looking http://lua-users.org/wiki/OsLibraryTutorial it looks like it's in seconds. Not sure if you require ms accuracy or not, but in mine I multiply os.clock by 1000 to get ms and then I just subtract that the values to get the difference in ms. Quote Link to comment Share on other sites More sharing options...
diedir Posted October 31, 2011 Author Share Posted October 31, 2011 No they are seconds afaik, seconds are enough accurate for me, it's just a toy racing car, nothing elaborate ... i will dig in it perhaps ms is more cute, i'll see now that's working Quote AMD Ryzen 5900HX - Nvidia RTX 3070 - 32 Go - 1To SSD - W11 Link to comment Share on other sites More sharing options...
Rick Posted October 31, 2011 Share Posted October 31, 2011 guess I ask because I primarily use these for animations which requires ms. I use it for other stuff as well and being in ms let's me use it for more precise timing in ms or less precise timing in seconds. It's more flexible I find. I don't use the fmod() way in the tutorials to do animations. I just find it easier to work with timers to increase the frames myself. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.