How to destroy a timer created with timer.Simple()
20 replies, posted
Hi forum,
My code is like follows:
[code]
Addtion:SetParent(Base)
Base:DeleteOnRemove(Addtion)
if(TimerTime > 0) then
local Solid = GetSolid() or -1
if(Solid >= 0) then
timer.Simple( TimerTime,function () Addtion:SetSolid( Solid ) end)
else
timer.Simple( TimerTime,function () Addtion:SetSolid( SOLID_NONE ) end)
end
end
[/code]
If the "Base" gets deleted, "Addtion" will be also, however the timer will continue to run while the "Base" is processed to be deleted or not.
As the "Base" gets deleted, leaving the method "SetSolid()" with no object --> NULL:SetSolid() leading to an error.
My question is:
How should I delete the timer when "Addtion" is null?
[code]
timer.Simple( TimerTime,
function ()
if(Addtion) then
Addtion:SetSolid( Solid )
else
--- Delete the timer here ....
end
end)
[/code]
Or does it has come kind of a lifespan.....
timer.Simple is not meant to be modified. It's a simple timer that executes and that's it.
If you need a timer that you can modify/pause, use timer.Create.
timer.Simple( TimerTime, function() ... ) will be ran when the timer is done. I don't see why you would want to delete that timer after it's already ran? Or what timer are you trying to delete?
[editline]31st October 2014[/editline]
Just do return end to make the function not get ran?
[QUOTE=Author.;46375286]timer.Simple( TimerTime, function() ... ) will be ran when the timer is done. I don't see why you would want to delete that timer after it's already ran? Or what timer are you trying to delete?
[editline]31st October 2014[/editline]
Just do return end to make the function not get ran?[/QUOTE]
I just wanna avoid the NULL:SetSolid() ERROR when the Base is removed while the timer is running since its creation.
"SetSolid() tried to use a NULL Entity "
[code]
timer.Simple( TimerTime,
function ()
if(Addtion) then
Addtion:SetSolid( Solid )
end
end)
[/code]
This will not ERROR, but is it going to exist after I do "Base:Remove()" ?
If I spawn too may objects with a timer like this and remove them, what happens with the timers ?
In the timer simply check if the objects exists first [B]IsValid(ENT)[/B].
[QUOTE=_FR_Starfox64;46376037]In the timer simply check if the objects exists first [B]IsValid(ENT)[/B].[/QUOTE]
Yep that was my very 1-st idea, but will the timer leak memory because of its own existence ?
If you still wish to know how to remove timers
timer.Create("Name", delay,1,function()
swag
end)
timer.Remove("Name")
[QUOTE=dvd_video;46376168]Yep that was my very 1-st idea, but will the timer leak memory because of its own existence ?[/QUOTE]
no
[QUOTE=Leystryku;46376276]no[/QUOTE]
K then xD. So far this seems the most promising, taking into consideration how can I manage the names of 100 timers and what name should I get to delete the desired one.
P.S. There is no need for IsValid(), because the "Addition" is already validated via " if(Addition and Addition:IsValid()) then "
[code]
timer.Simple( TimerTime,function()
if(Addition) then
Addition:SetSolid( Solid )
end
end)
[/code]
[editline]31st October 2014[/editline]
[QUOTE=Xaotic;46376253]If you still wish to know how to remove timers
timer.Create("Name", delay,1,function()
swag
end)
timer.Remove("Name")[/QUOTE]
I already know that, managing the names is the hard part :)
You can name them with the entity index.
[CODE]
"EntityRemove_"..Addition:EntIndex()
[/CODE]
Dude what are you even trying to do? Stop posting weird code snippets, and just say what you're trying to do, and I'd love to help you.
What exactly are you doing anyway?
[QUOTE=dvd_video;46376770][code]
timer.Simple( TimerTime,function()
if(Addition) then
Addition:SetSolid( Solid )
end
end)
[/code]
[editline]31st October 2014[/editline]
I already know that, [B]managing the names is the hard part :)[/B][/QUOTE]
What do you mean?
[QUOTE=_FR_Starfox64;46376972]You can name them with the entity index.
[CODE]
"EntityRemove_"..Addition:EntIndex()
[/CODE][/QUOTE]
Nice one ;) But then I will need the Exact opposite of [url]http://wiki.garrysmod.com/page/Entity/GetParent[/url]
Returning a table of parented ents, that are conveted to IDls to kill their timers :)
[editline]31st October 2014[/editline]
[QUOTE=Author.;46376977]Dude what are you even trying to do? Stop posting weird code snippets, and just say what you're trying to do, and I'd love to help you.[/QUOTE]
... Previous posts pls ... ( Mostly the first one. )
And that would be [url]http://steamcommunity.com/sharedfiles/filedetails/?id=287012681[/url]
You need to explain thoroughly. You aren't asking what you're trying to do.
[QUOTE=mib999;46377191]You need to explain thoroughly. You aren't asking what you're trying to do.[/QUOTE]
Read the firs post pls
Jesus christ. We are asking you to explain it because we can't understand what you're trying to do in the first post.
Just do as I said and check if your object IsValid() in your timer that way if the base does dot exist any more it won't execute the function on a nil object.
[B]Edit:[/B]
That's what you want to do when you use timers on entities that can be removed before the timer runs out, always check if the entity IsValid() before doing anything on it, Case closed!
In general when using timers assume that everything you validated previously is garbage and must be rechecked when the timer fires
(hardly anyone actually does this)
[QUOTE=dvd_video;46377198]Read the firs post pls[/QUOTE]
I have read your first post. I don't understand a fucking word of it.
[QUOTE=mib999;46377601]I have read your first post. I don't understand a fucking word of it.[/QUOTE]
How should I delete the timer when "Addtion" is null?
What's not to understand ??? [ I hope that you understand this question too xD ]
Anyway all I got the desired result in the last update:
[url]http://steamcommunity.com/sharedfiles/filedetails/changelog/287012681[/url]
Marking as "Solved"
Sorry, you need to Log In to post a reply to this thread.