• How to measure amount of time elapsed?
    7 replies, posted
I need a way to delete an entity after a certain amount of time. How would I do this?
[lua] local function delete() local pTime = CurTime() + 30 if( CurTime() >= pTime ) then //delete entity end end[/lua] [editline]1st May 2011[/editline] untested btw but i use code very similar in i script i have open right now. [editline]1st May 2011[/editline] replace 30 with the amount of time you want to wait to delete
Sorry for my noobishness, but I don't know how to implement that into my SWEP. I'm making an AK47 that shoots babies, so i'd need a way for each baby fired to delete after 5 seconds.
[lua]function SWEP:SpawnFunction(ent,tr) self.RemoveTime = CurTime() + 30 end function SWEP:Think() if self.RemoveTime < CurTime() then self:Remove() end end [/lua] Written in the reply box so ignore the lack of tabbing.
[QUOTE=Nick Clegg;29544163][lua]function SWEP:SpawnFunction(ent,tr) self.RemoveTime = CurTime() + 30 end function SWEP:Think() if self.RemoveTime < CurTime() then self:Remove() end end [/lua] Written in the reply box so ignore the lack of tabbing.[/QUOTE] There's 3 things wrong with that. That would remove the weapon, and you would not want to remove things like all of these baby bullets in a weapons think hook because if you put the weapon away nothing will happen. And the third thing wrong with that is if you kept firing you'd have to wait 5 seconds after cease fire before only 1 baby was removed. The most simple and reliable way would be this. [LUA] function SWEP:PrimaryFire() local baby = ents.Create("prop_physics") //This would be your baby bullet however you create yours timer.Simple( 5, function() if(IsValid(baby))then baby:Remove() end) end [/LUA]
[QUOTE=-TB-;29544722]There's 3 things wrong with that. That would remove the weapon, and you would not want to remove things like all of these baby bullets in a weapons think hook because if you put the weapon away nothing will happen. And the third thing wrong with that is if you kept firing you'd have to wait 5 seconds after cease fire before only 1 baby was removed. [/LUA][/QUOTE] Didn't read the post about firing babies - I assumed it was removing the weapon 30 seconds after spawning it. Guess I should read whole threads.
[code]SafeRemoveEntityDelayed( baby, delay )[/code]
[QUOTE=Kogitsune;29544868][code]SafeRemoveEntityDelayed( baby, delay )[/code][/QUOTE] Thanks!
Sorry, you need to Log In to post a reply to this thread.