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 


Cancel A Lua Function

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1667

PostPosted: Fri Jun 07, 2019 11:39 pm    Post subject: Cancel A Lua Function Reply with quote

This performs with delay function made by mgr.inz.Player. I use the function on my project. The problem is how to terminate or cancel the function?

Code:
function performWithDelay(delay,onFinish,onTick,onTickInterval)
 if type(delay)~='number' then error('delay is not a number') end
 if type(onFinish)~='function' then error('onFinish is not a function') end
 if onTick and type(onTick)~='function' then error('onTick is not a function') end
 if onTickInterval and type(onTickInterval)~='number' then
 error('onTickInterval is not a number') end
 onTickInterval = onTickInterval or 1000
 local f = function (t)
 local getTickCount = getTickCount
 local startTick = getTickCount()
 local endTick = startTick + delay
 local nextOnTick = startTick + onTickInterval
 local ticks
 if onTick then while true do ticks=getTickCount()
 if nextOnTick<ticks then nextOnTick=ticks+onTickInterval
 synchronize(onTick,endTick-ticks) end
 if endTick<ticks then break end sleep(1) end else
 while true do ticks=getTickCount() if endTick<ticks then break end
 sleep(1) end end
 if onFinish then synchronize(onFinish) end end
 local t = createNativeThreadSuspended(f) t.name = 'performWithDelay thread' t.resume()
end

performWithDelay(10000,whenFinished,showTimeLeft,10)  --- do countdown 10 seconds

-- whenFinished is a function = do something when countdown timer over
-- showTimeLeft = show the progress on a label


In Corona, I can use, example:

Code:
local spawnTimer = timer.performWithDelay(1000, spawnBadGuy, 0)
timer.cancel( spawnTimer )  -- to cancel the performWithDelay timer.


How in CE Lua script?

_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 458

Joined: 09 May 2003
Posts: 25287
Location: The netherlands

PostPosted: Sat Jun 08, 2019 3:58 am    Post subject: Reply with quote

it's a LOT easier to use a timer, just get rid of that ontickinterval

anyhow, in the f function add a if t.Terminated check, and if so, exit

then make performWithDelay return t

and when you wish to stop it do t.destroy()

_________________
Do not ask me about online cheats. I don't know any and wont help finding them.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1667

PostPosted: Sat Jun 08, 2019 5:13 am    Post subject: Reply with quote

[SOLVED] : Yes, DB. I did your suggestion by change 'perform with delay' function to:

Code:
function SecondsToClock(seconds)
 local seconds = tonumber(seconds)
 hours = string.format("%02.f", math.floor(seconds/3600));
 mins = string.format("%02.f", math.floor(seconds/60 - (hours*60)));
 secs = string.format("%02.f", math.floor(seconds - hours*3600 - mins *60));
 return hours..":"..mins..":"..secs
end


and implemented that to my project, more easier to control by a timer event handler, example:

Code:
function startSpeedHack()
 labelTimer.Font.Size = 26
 labelTimer.Font.Color = pnUp.Color --'65407'
 labelTimer.Left = 61
 pnPBar.Visible = true
 sh = edtSpeed.Text
 st = edtSpTimer.Text
 sh = tonumber(sh)
 sec = tonumber(st) --* 10
 counterTime = sec
 pnPBar.Width = 208
 step = 208 / sec
 decreaseTime = 1
 labelTimer.Caption = SecondsToClock(counterTime)
 tmr.Enabled = true
 pnSetSpeed.Enabled = false
 pnStopSpeed.Enabled = true
end

function bar()
 speedhack_setSpeed(sh)
 sec = sec - 1
 pnPBar.width = pnPBar.width - step
 counterTime = counterTime - decreaseTime
 labelTimer.Caption = SecondsToClock(counterTime)
 if counterTime <= 0 then
  speedhack_setSpeed(1)
  tmr.enabled = false
  edtSpeed.Text = ''
  edtSpTimer.Text = ''
  labelTimer.Font.Size = 20
  labelTimer.Font.Color = '858083'
  labelTimer.Caption = "Time's Up!"
  pnPBar.Visible = false
  pnSetSpeed.Enabled = false
  pnStopSpeed.Enabled = false
 end
end

tmr = createTimer()
tmr.interval = 1000
tmr.enabled = false
tmr.OnTimer = bar

function stopTimer()
 tmr.Enabled = false
 edtSpeed.Text = ''
 edtSpTimer.Text = ''
 labelTimer.Caption = "00:00:00"
 pnPBar.Visible = false
 pnSetSpeed.Enabled = false
 pnStopSpeed.Enabled = false
end

f.show()
pnSetSpeed.onClick = startSpeedHack


Quote:
add a if t.Terminated check, and if so, exit


Also, I try to add 'a check condition' to the 'performWithDelay' function.

Thanks...

_________________
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