Help me find an infinite loop (yay!)

[lua]
function CapZonesThink()
local all = ents.GetAll()

for _,v in pairs(all) do
	if v:GetName() == "nectar_cap_red" then
		RedCap = v
	end
	
	if v:GetName() == "nectar_cap_blue" then
		BlueCap = v
	end
end

if RedCap:IsValid() and BlueCap:IsValid() then
	local minr, maxr = RedCap:WorldSpaceAABB()
	local minb, maxb = BlueCap:WorldSpaceAABB()
	
	local in_r = ents.FindInBox(minr, maxr)
	local in_b = ents.FindInBox(minb, maxb)
	
	local k = 0
	while k <= #in_r do
		k = k + 1
		local v = in_r[k]
		if v != nil then
			if v:GetClass() != "ants_nectar" then
				table.remove(in_r, k)
				k = k - 1
			end
		else
			table.remove(in_r, k)
			k = k - 1
		end
	end
	
	k = 0
	while k <= #in_b do
		k = k + 1
		local v = in_b[k]
		if v != nil then
			if v:GetClass() != "ants_nectar" then
				table.remove(in_b, k)
				k = k - 1
			end
		else
			table.remove(in_b, k)
			k = k - 1
		end
	end
	
	for _,v in pairs(in_r) do
		//v:SetNWBool("capped", true)
		v:SetNWInt("capped_team", 1)
	end
	
	for _,v in pairs(in_b) do
		//v:SetNWBool("capped", true)
		v:SetNWInt("capped_team", 2)
	end
	
	local uncapped_nectars = ents.FindByClass("func_nectar")
	//Find the nectars that have been uncapped, and set their NWInts accordingly. ("capped_team" = 0)
	//Using that NWint, we'll be able to limit gravgun on nectars that were already capped by your team. (below)
	
	k = 0
	while k <= #uncapped_nectars do
		k = k + 1
		CurN = uncapped_nectars[k]
		
		if CurN != nil then
			local FoundRed = false
			
			for _,v in pairs(in_r) do
				if CurN == v then
					table.remove(uncapped_nectars, k)
					k = k - 1
					FoundRed = true
				end
			end
			
			if !FoundRed then
				for _,v in pairs(in_b) do
					if CurN == v then
						table.remove(uncapped_nectars, k)
						k = k - 1
					end
				end
			end
		else
			table.remove(uncapped_nectars, k)
			k = k - 1
		end
	end
	
	for _,v in pairs(uncapped_nectars) do
		v:SetNWInt("capped_team", 0)
	end
	
	team.SetScore(1, #in_r)
	team.SetScore(2, #in_b)
else
	print("no cap zones!")
end

end
hook.Add(“Think”, “CapZonesThink”, CapZonesThink)
[/lua]

Could you guys tell me where the infinite loop is in this? I can’t find it anywhere! Thanks…

[lua] local k = 0
while k <= #in_r do
k = k + 1
local v = in_r[k]
if v != nil then
if v:GetClass() != “ants_nectar” then
table.remove(in_r, k)
k = k - 1
end
else
table.remove(in_r, k)
k = k - 1
end
end[/lua]

Nice try but there’s something really wrong here. When you put #in_r in a loop like that, the loop will use the size of the table at the moment you entered it. It will not check it again after every iteration.
So if there is at least one entity which isn’t an “ants_nectar”, your table will be smaller, yet the loop will still attempt to reach the initial size of your table. You will end up reading nil values, removing them, decrementing k, and so on, and you have just created an infinite loop.

You’re overcomplicating your code.

[lua]
function CapZonesThink()
RedCap = ents.FindByName(“nectar_cap_red”)[1]
BlueCap = ents.FindByName(“nectar_cap_blue”)[1]

if RedCap:IsValid() and BlueCap:IsValid() then
	local red_score, blue_score = 0, 0
	
	for _,v in pairs(ents.FindByClass("func_nectar")) do
		v.capped_team = 0
	end
	
	for _,v in pairs(ents.FindInBox(RedCap:WorldSpaceAABB())) do
		if v:GetClass() == "ants_nectar" then
			v.capped_team = 1
			red_score = red_score + 1
		end
	end
	
	for _,v in pairs(ents.FindInBox(BlueCap:WorldSpaceAABB())) do
		if v:GetClass() == "ants_nectar" then
			v.capped_team = 2
			blue_score = blue_score + 1
		end
	end
	
	for _,v in pairs(ents.FindByClass("func_nectar")) do
		v:SetNWInt("capped_team", v.capped_team)
	end
	
	team.SetScore(1, red_score)
	team.SetScore(2, blue_score)
else
	print("no cap zones!")
end

end
hook.Add(“Think”, “CapZonesThink”, CapZonesThink)
[/lua]

I’m storing the capping team in a separate variable before setting it with SetNWInt, this way it doesn’t set the networked variable twice. If I remember correctly, the value of a networked var is sent to all clients every time it changes, so that wouldn’t be very optimized.

[editline]10:04AM[/editline]

Now that I think of it, your ants_nectar is some kind of prop, right? And func_nectar is a trigger, right?

You should code your trigger so that it increases the score of its team when an ants_nectar enters it, and decreases it when one leaves it. That would be way more optimized than a Think hook.

**[ENT.StartTouch

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=ENT.StartTouch)**
**[ENT.EndTouch

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=ENT.EndTouch)**