How to destroy a timer created with timer.Simple()

Hi forum,

My code is like follows:



          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


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?



timer.Simple( TimerTime,
                     function ()
                       if(Addtion) then
                        Addtion:SetSolid( Solid ) 
                       else
                        --- Delete the timer here ....
                       end
                     end)


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?

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 "



timer.Simple( TimerTime,
                     function ()
                       if(Addtion) then
                        Addtion:SetSolid( Solid ) 
                       end
                     end)


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 IsValid(ENT).

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”)

no

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 "



              timer.Simple( TimerTime,function()
                                        if(Addition) then
                                          Addition:SetSolid( Solid )
                                        end
                                      end)


[editline]31st October 2014[/editline]

I already know that, managing the names is the hard part :slight_smile:

You can name them with the entity index.



"EntityRemove_"..Addition:EntIndex()


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?

What do you mean?

Nice one :wink: But then I will need the Exact opposite of http://wiki.garrysmod.com/page/Entity/GetParent
Returning a table of parented ents, that are conveted to IDls to kill their timers :slight_smile:

[editline]31st October 2014[/editline]

… Previous posts pls … ( Mostly the first one. )

And that would be http://steamcommunity.com/sharedfiles/filedetails/?id=287012681

You need to explain thoroughly. You aren’t asking what you’re trying to do.

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.

Edit:
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)

I have read your first post. I don’t understand a fucking word of it.