Page 1 of 1

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

Posted: Wed Jul 21, 2021 4:47 pm
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??

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

Posted: Wed Jul 21, 2021 6:21 pm
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.

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

Posted: Wed Jul 21, 2021 10:13 pm
by ShyTwig16
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.

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

Posted: Thu Jul 22, 2021 9:51 am
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?

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

Posted: Thu Jul 22, 2021 12:36 pm
by ShyTwig16
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.

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

Posted: Thu Jul 22, 2021 5:13 pm
by hcapinjr
Thank you ShyTwig16 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 !