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 


About createTimer Functions

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
etioplmld
Advanced Cheater
Reputation: 0

Joined: 09 Feb 2021
Posts: 72

PostPosted: Tue May 23, 2023 11:08 pm    Post subject: About createTimer Functions Reply with quote

Code:

function countdown()
print(1)
end
local cctimer = createTimer()
cctimer.Interval = 1000
cctimer.OnTimer = countdown


This code will output a 1 per second,Why does this code not output
1 ,1 2 , 123 ....,It seems to have created many timers

Code:

for i=1,5 do
  createTimer(1000*i,function() print(i) end )
end



Can ce create two timers at the same time, Start one second after another starts.Two counts timer like this?

Code:

local cctimer = createTimer()
cctimer.Interval = 1000
i = 0
function countdown()
i=i + 1
print(i)
if i>= 5 then
object_destroy(cctimer)
end
end
cctimer.OnTimer = countdown


function countdown()
  local i = 0
  local t = createTimer()
  t.Interval = 1000
  t.OnTimer = function()
  i = i + 1
  print(i)
if i>=5 then
  t.Destroy()
  end
end
end
countdown()
Back to top
View user's profile Send private message
LeFiXER
Grandmaster Cheater Supreme
Reputation: 20

Joined: 02 Sep 2011
Posts: 1053
Location: 0x90

PostPosted: Wed May 24, 2023 4:58 am    Post subject: Re: About createTimer Functions Reply with quote

etioplmld wrote:

Code:

function countdown()
print(1)
end
local cctimer = createTimer()
cctimer.Interval = 1000
cctimer.OnTimer = countdown


This code will output a 1 per second,Why does this code not output
1 ,1 2 , 123 ....,It seems to have created many timers


You have no check to see if the timer exists.
Code:

if cctimer then cctimer.destroy(); cctimer = nil end



etioplmld wrote:

Code:

for i=1,5 do
  createTimer(1000*i,function() print(i) end )
end



This loops and creates a timer with each loop iteration, setting the interval to 1,000 milliseconds multiplied by the iterator. With every timer tick it prints the iterator number.

etioplmld wrote:

Can ce create two timers at the same time, Start one second after another starts.Two counts timer like this?

Code:

local cctimer = createTimer()
cctimer.Interval = 1000
i = 0
function countdown()
i=i + 1
print(i)
if i>= 5 then
object_destroy(cctimer)
end
end
cctimer.OnTimer = countdown


function countdown()
  local i = 0
  local t = createTimer()
  t.Interval = 1000
  t.OnTimer = function()
  i = i + 1
  print(i)
if i>=5 then
  t.Destroy()
  end
end
end
countdown()


I am not entirely sure of the limitation of how many timers that can be created at any one-time; however, I know that it's possible to create several. I think the limitation is based on the amount of threads that your processor can handle although I could be wrong about that.

I would achieve what you want by doing this:
Code:

-- First and foremost, check if the components exist already
if t1 then t1.destroy(); t1 = nil end
if t2 then t2.destroy(); t2 = nil end

-- Variable to store the timer component so that the timers can be children of Cheat Engine
local owner = getMainForm()

-- Variable to store count
local count = 0

-- Initiate timer variables
local t1,t2 = createTimer(owner),createTimer(owner, false)
-- Set interval
t1.Interval = 1000
t2.Interval = 1000
-- Set function based on timer tick
t1.OnTimer = function()
               count = count + 1
               print('Timer1 Iterator:\t' .. count)
               if count == 2 then
                  t2.Enabled = true
               elseif count >= 5 then
                  t1.destroy()
                  t1 = nil
                  count = nil
               end
             end

t2.OnTimer = function()
               print('Timer2 intitiated')
               t2.destroy()
               t2 = nil
             end


I'm not sure why you want this, but it achieves what you requested.
Back to top
View user's profile Send private message
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1667

PostPosted: Thu May 25, 2023 12:14 am    Post subject: Reply with quote

If use system windows. forms timer you can't run multiple timer at the same time, because it is designed for a single-threaded environment.
But if use system timers or system threading timer it will work they designed for multi thread execution.

Anyhow, I don't know which system timer CE design for. Even though
several timers run at the same time, there is still a "delay time" between the timers
Very Happy

_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL
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