Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Counting time in threads (Timer in thread)

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
Traivlin
How do I cheat?
Reputation: 0

Joined: 21 Apr 2022
Posts: 2

PostPosted: Thu Apr 21, 2022 11:30 am    Post subject: Counting time in threads (Timer in thread) Reply with quote

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
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 138

Joined: 06 Jul 2014
Posts: 4275

PostPosted: Thu Apr 21, 2022 12:00 pm    Post subject: Reply with quote

Traivlin wrote:
First, I thought it would work if I create a timer within the thread or through calling a function.
Timers seem to be related to GUI objects in that they can only be used from the main thread.

Just use sleep.
Code:
createThread(function(t)
  sleep(1000)
  print'done'
end)

Or use createTimer from the main thread to delay the creation of the thread until needed.
Code:
createTimer(1000, function()
  createThread(function()
    print'done'
  end)
end)

Traivlin wrote:
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.
Back to top
View user's profile Send private message
AylinCE
Grandmaster Cheater Supreme
Reputation: 31

Joined: 16 Feb 2017
Posts: 1234

PostPosted: Thu Apr 21, 2022 2:11 pm    Post subject: Reply with quote

Here are some usage examples for counting;

Code:
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)

_________________
Hi Hitler Different Trainer forms for you!
https://forum.cheatengine.org/viewtopic.php?t=619279
Enthusiastic people: Always one step ahead
Do not underestimate me Master: You were a beginner in the past
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
Traivlin
How do I cheat?
Reputation: 0

Joined: 21 Apr 2022
Posts: 2

PostPosted: Fri Apr 29, 2022 4:47 pm    Post subject: Reply with quote

Thank you for the answers. As a solution, I ended up using synchronize() and called the timer function with that
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites