Jump to content

Lua Timer and AppTime()


diedir
 Share

Recommended Posts

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

AMD Ryzen 5900HX - Nvidia RTX 3070 - 32 Go - 1To SSD - W11

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 !

AMD Ryzen 5900HX - Nvidia RTX 3070 - 32 Go - 1To SSD - W11

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...