How would I delay a single thing that is in ENT:Think without stopping all thinking? Or is there an alternate method?

Hello. I’m making a nextbot. I’m using ENT:Think to make it shoot grenades at a player. However, setting a delay for when the grenade is fired(by using


self:NextThink( CurTime() + 2.00 )

doesn’t work. It just stops the whole entity from thinking for 2 seconds, meaning that it moves every 2 seconds then stops, and repeats. How can I stop my grenade-firing part of the hook without stopping all thinking? I need help. BTW, this is my ENT:Think() hook:


function ENT:Think()

local ent = ents.Create("ent_nz_grenade")
	if ( self:GetEnemy() and IsValid( self:GetEnemy() ) ) then
		ent:SetPos(self:EyePos() + Vector(0,0,100))
		ent:Spawn()
		ent:SetOwner( self )
				
	local phys = ent:GetPhysicsObject()
	if phys:IsValid() then
	local ang = self:EyeAngles()
	ang:RotateAroundAxis(ang:Forward(), math.Rand(-10, 10))
	ang:RotateAroundAxis(ang:Up(), math.Rand(-10, 10))
	phys:SetVelocityInstantaneous(ang:Forward() * math.Rand( 1000, 1500 ))
				
	end
	end
end


Or is there an alternate method?(P.S. I’m basing this bot off of the Gmod Wiki example, so the alternate methods will be in that code)

Save a variable on your entity for the last think (in initialize, set it to 0). In your think, check if this variable is less than CurTime(). If it is, run the code you want, and set the variable to CurTime() + Delay.


--In ENT:Initialize(Part of the code, not the whole hook)
lastthink = 0



function ENT:Think()

if lastthink <= CurTime() then 

local ent = ents.Create("ent_nz_grenade")
	if ( self:GetEnemy() and IsValid( self:GetEnemy() ) ) then
		ent:SetPos(self:EyePos() + Vector(0,0,100))
		ent:Spawn()
		ent:SetOwner( self )
				
	local phys = ent:GetPhysicsObject()
	if phys:IsValid() then
	local ang = self:EyeAngles()
	ang:RotateAroundAxis(ang:Forward(), math.Rand(-10, 10))
	ang:RotateAroundAxis(ang:Up(), math.Rand(-10, 10))
	phys:SetVelocityInstantaneous(ang:Forward() * math.Rand( 1000, 1500 ))
				
	end
	end
	
	self:NextThink( CurTime() + lastthink )
	
	end
end

Give me as many boxes as you want, but what do you mean? I tried this but it doesn’t work, I got a little confused. I’m not the world’s best Lua coder, I’ve only been coding for a few months(I know what variables are, though, however I got confused with the way you explained your answer))…

[lua]

function ENT:Initialize()
self.lastthink = 0
end

function ENT:Think()

if self.lastthink &lt; CurTime() then 

	if ( self:GetEnemy() and IsValid( self:GetEnemy() ) ) then
		local ent = ents.Create("ent_nz_grenade")
		ent:SetPos(self:EyePos() + Vector(0,0,100))
		ent:Spawn()
		ent:SetOwner( self )
				
		local phys = ent:GetPhysicsObject()
		if phys:IsValid() then
			local ang = self:EyeAngles()
			ang:RotateAroundAxis(ang:Forward(), math.Rand(-10, 10))
			ang:RotateAroundAxis(ang:Up(), math.Rand(-10, 10))
			phys:SetVelocityInstantaneous(ang:Forward() * math.Rand( 1000, 1500 ))	
		end
	end
	
	self.lastthink = CurTime() + 1 -- Where 1 is the delay

end

end
[/lua]

What I did was make lastthink a member of the ENT table, and made Think update it when needed.

Oh! Thanks!

I know I’m late on the ball with this one but setting Cooldown timers is a VERY common task; I’d suggest making helper functions such as: _ent:HasCooldown( “name” ); and _ent:SetCooldown( _name, _time );

Definitely makes code read better: https://dl.dropboxusercontent.com/u/26074909/tutoring/logic/simple_cooldown_examples.lua.html ( the linked version has evolved to allow networking the cooldown with an extra arg to make it local and 2 helpers which set it to true so _ent:HasLocalCooldown and _ent:SetLocalCooldown … I may swap the default to non-networked; we’ll see… the new version also allows setting cases of what to do when you call SetCooldown while one exists such as always overwriting the value, only overwriting if higher, adding the value, etc… )

https://dl.dropboxusercontent.com/u/26074909/tutoring/logic/simple_cooldown.lua.html - remove .html to view .lua

The CurTime solution works well but I’m just offering this up as an alternative.