How to stop a function and nil a variable properly

Hi, I said before I’m a beginner struggling on my first app which is a virtual pet kind of game.
Lately I’ve been trying to make it leak-free and I ran into one problem.

I have a global function gamescreen() for everything in-game.
Inside it there is a local updatePet() function which changes the state of the pet as the hunger changes.
At the end of the gameScreen function (if the pet dies) I call a destroyAll() function that removes and nils all display objects and also the main variables such as hunger (which is local to the gameScreen() function).

It looks kinda like this:

function gameScreen()

local function updatePet()
if hunger pet:changes…
elseif hunger<=0 then
end
end
timerUpdate = timerPerformWithDelay (1,updatePet,-1)

destroyAll()

function destroyAll()
timer.cancel(timerUpdate)
hunger=nil
…remove all…
end

end

And it actually does prevent almost all leaks (almost :))
The problem is while in the restart menu (after hunger = nil) it keeps spamming a mistake that in the line in updatePet() it tries to compare a nil to a number.
Why is the updatePet() function still running? I thought I ended it and also cancelled the timer… What did I forget?

Thanks!
[import]uid: 161390 topic_id: 29565 reply_id: 329565[/import]

function gameScreen()

local function updatePet()
if hunger <100 then
pet:changes…
elseif hunger<=0 then
end
end
timerUpdate = timerPerformWithDelay (1,updatePet,-1)

destroyAll()

function destroyAll()
timer.cancel(timerUpdate)
hunger=nil
…remove all…
end

end
And it actually does prevent almost all leaks (almost :))
The problem is while in the restart menu (after hunger = nil) it keeps spamming a mistake that in the line in updatePet() it tries to compare a nil to a number.
Why is the updatePet() function still running? I thought I ended it and also cancelled the timer… What did I forget?

Thanks!
PS Sorry for multiple posts!

[import]uid: 161390 topic_id: 29565 reply_id: 118679[/import]

First, type in your code with < code > < / code > tags at the beginning and end (minus the spaces) so it looks like real code. You made some fairly obvious code typos as well but I can see what you’re trying to do.

It’s because of this line:

timerUpdate = timer.performWithDelay(1, updatePet, -1)

That says to call updatePet() every 1 second, continuously, until you stop it. (I think. According to the docs you should pass zero, not -1, for infinite loops, but I haven’t checked it lately.)

Normally what you should do is declare the variable at the beginning of your file (just type “local timerUpdate”) - that way, your destroyAll() code is sure to find timerUpdate. But if this is a specific pet I prefer doing this:

[code]-- updatePet()
pet.timer = timer.performWithDelay(1, updatePet, 0)

– destroyAll()
timer.cancel(pet.timer)
pet.timer = nil[/code]

That way your timer is directly connected to the object (pet) itself and it’s easy for you to find. [import]uid: 41884 topic_id: 29565 reply_id: 118683[/import]

Sorry for not using the tags :slight_smile:
Thanks for the reply, didn’t help yet, but I’ll try to check my code in more detail [import]uid: 161390 topic_id: 29565 reply_id: 118685[/import]