Lua script timer doesn't stop after the timer is destroyed

Want Cheat Engine to do something specific and no idea how to do that, ask here. (From simple scripts to full trainers and extensions)
Post Reply
hcapinjr
Noobzor
Noobzor
Posts: 9
Joined: Wed Oct 21, 2020 11:27 am
Reputation: 4

Lua script timer doesn't stop after the timer is destroyed

Post by hcapinjr »

Hey all,
Been working on scripting with lua but for some odd reason anytime I use a timer and disable it via timer.destroy() the script keeps running until I logout of win10 or restart the system... Any ideas on why this is happening or how I can fix it??

User avatar
GreenHouse
GreenHouse!
GreenHouse!
Posts: 668
Joined: Fri Oct 12, 2018 10:25 pm
Reputation: 529

Re: Lua script timer doesn't stop after the timer is destroyed

Post by GreenHouse »

Make sure that you're not creating another timer with the same name while one is already active. As when you try to destroy it, you'll destroy the new one, but not the old one which will keep running.

User avatar
TheyCallMeTim13
Administration
Administration
Posts: 1688
Joined: Fri Mar 03, 2017 12:31 am
Reputation: 886

Re: Lua script timer doesn't stop after the timer is destroyed

Post by TheyCallMeTim13 »

I like to use a global variable, say KILL_SOME_TIMER, and if it's set then kill the timer in the OnTimer function.

Code: Select all

local function someTimer_OnTimer(timer)
	if KILL_SOME_TIMER then
	    timer.destroy()
	    return
	end
	-- body
end
This way it will always kills any running timers created using the same on timer function.

hcapinjr
Noobzor
Noobzor
Posts: 9
Joined: Wed Oct 21, 2020 11:27 am
Reputation: 4

Re: Lua script timer doesn't stop after the timer is destroyed

Post by hcapinjr »

Here is the script
As to using the same timer as far as I know they are all unique as to naming.
Here is the script it is a simple health regeneration as the game doesn't have regeneration of any kind.
I would do this in asm if I knew a simpler way lol.
{$lua}
[ENABLE]
healthRegenTimer = createTimer()
healthRegenTimer.Interval = 1000

local health = '[[[[[Dungeon Siege III.exe+02D8F3F0]+00]+60]+C4]+64]+14'
local maxHealth = '[[["Dungeon Siege III.exe"+02D8F3DC]+30]+64]+18'
local healthRegen = 1
healthRegenTimer.OnTimer = function()
local curHealth = readFloat(health)
local maxHealth = readFloat(maxHealth)
if curHealth and curHealth < maxHealth then
writeFloat(health, curHealth+healthRegen)
end
end
[DISABLE]
healthRegenTimer.destroy()
when I disable the script I then write a new value to the pointer and the value starts increasing until the max is reached.
Where would I place the kill timer function?

User avatar
TheyCallMeTim13
Administration
Administration
Posts: 1688
Joined: Fri Mar 03, 2017 12:31 am
Reputation: 886

Re: Lua script timer doesn't stop after the timer is destroyed

Post by TheyCallMeTim13 »

hcapinjr wrote:
Thu Jul 22, 2021 9:51 am
Here is the script
As to using the same timer as far as I know they are all unique as to naming.
Here is the script it is a simple health regeneration as the game doesn't have regeneration of any kind.
I would do this in asm if I knew a simpler way lol.
{$lua}
[ENABLE]
healthRegenTimer = createTimer()
healthRegenTimer.Interval = 1000

local health = '[[[[[Dungeon Siege III.exe+02D8F3F0]+00]+60]+C4]+64]+14'
local maxHealth = '[[["Dungeon Siege III.exe"+02D8F3DC]+30]+64]+18'
local healthRegen = 1
healthRegenTimer.OnTimer = function()
local curHealth = readFloat(health)
local maxHealth = readFloat(maxHealth)
if curHealth and curHealth < maxHealth then
writeFloat(health, curHealth+healthRegen)
end
end
[DISABLE]
healthRegenTimer.destroy()
when I disable the script I then write a new value to the pointer and the value starts increasing until the max is reached.
Where would I place the kill timer function?
Put it in the OnTimer function.

Code: Select all

{$lua}
[ENABLE]
KILL_HEALTH_REGEN_TIMER = false
healthRegenTimer = createTimer()
healthRegenTimer.Interval = 1000

local health = '[[[[[Dungeon Siege III.exe+02D8F3F0]+00]+60]+C4]+64]+14'
local maxHealth = '[[["Dungeon Siege III.exe"+02D8F3DC]+30]+64]+18'
local healthRegen = 1
healthRegenTimer.OnTimer = function(timer)
	if KILL_HEALTH_REGEN_TIMER then
		timer.destroy()
	end
	local curHealth = readFloat(health)
	local maxHealth = readFloat(maxHealth)
	if curHealth and curHealth < maxHealth then
		writeFloat(health, curHealth+healthRegen)
	end
end
[DISABLE]
KILL_HEALTH_REGEN_TIMER = true
And on a side note. If the code was longer and not indented like you have it, I probably wouldn't have replied. Make life easier for those helping to be more likely to get more replies.

hcapinjr
Noobzor
Noobzor
Posts: 9
Joined: Wed Oct 21, 2020 11:27 am
Reputation: 4

Re: Lua script timer doesn't stop after the timer is destroyed

Post by hcapinjr »

Thank you TheyCallMeTim13 for your help and input. It works, I also noted I made and error in the scripting as far as lua scripting oddly myself and CE didn't catch it.. the current health pointer shouldn't have worked that I know of.. Very new to lua scripting here.. Anywho, the pointer is missing "
local health = '[[[[[Dungeon Siege III.exe+02D8F3F0]+00]+60]+C4]+64]+14'
and should have been local health = '[[[[["Dungeon Siege III.exe"+02D8F3F0]+00]+60]+C4]+64]+14' because that seemed to as well have fixed the issue.
Again thank you all for your input it is highly appreciated !

Post Reply

Who is online

Users browsing this forum: No registered users