Posted: Thu Apr 21, 2022 11:30 am Post subject: Counting time in threads (Timer in thread)
Hi, I'm new to Lua and trying to do something within a thread after a certain amount of time has passed. First, I thought it would work if I create a timer within the thread or through calling a function.
Either way, the OnTimer function of a created timer doesn't seem to function for me within threads.
I'm guessing this is intentional and it's just me misunderstanding it fundamentally.
What other options are there? I thought about using os.clock().
That works, but if I do that in a new loop within the thread, the rest of the thread won't be running.
If I just let it run in the main loop, I fear that the timing in some cases might not be precise enough?
Do I need to make another thread just for counting time?
I'm probably missing the obvious. Help is appreciated
I fear that the timing in some cases might not be precise enough?
You're probably doing something wrong or misunderstanding something. It would be better if you gave more information about what you're trying to do. _________________
I don't know where I'm going, but I'll figure it out when I get there.
if tim1 then tim1.Destroy() tim1=nil end
tim1=createTimer() tim1.Interval=100 tim1.Enabled=false
local tim1Tick=0
local exIndex=0
function printer()
exIndex=tonumber(exIndex) + 1
print("exIndex: "..exIndex)
if exIndex==5 then
print("exIndex: "..exIndex.." - Timer: false")
tim1Tick=0
exIndex=0
tim1.Enabled=false
end
end
function thread()
tim1Tick=tonumber(tim1Tick) + 1
if tim1Tick==20 then
printer()
tim1Tick=0
end
end
if timKey then timKey.Destroy() timKey=nil end
timKey=createHotkey(function() sleep(100)
if tim1.Enabled==false then
tim1.OnTimer=thread
tim1.Enabled=true
else
tim1.Enabled=false
tim1Tick=0
exIndex=0
end
end, VK_F8)
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum