Find if plant is getting light?

Tried doing this myself, but I’m fairly new to Lua and don’t even know where to start. I have a variable “gettinglight” set to false in the Initialize. Should I try to use the ents.FindInSphere? This is for indoor lights, by the way. a straight trace up would be a (crude) way to do it for outside, right?

Edit:
I got it.

[lua]
function ENT:Think()
local minrange = 100
local target = nil
for k,value in pairs( ents.FindInSphere(self.Entity:GetPos(),100 )) do
local distance = value:GetPos():Distance( self.Entity:GetPos() )
if value:GetClass() == “ent_lowlight” and distance < 100 then
target = value
minrange = distance
print( “yay” )
end
end
end[/lua]

[lua]
function ENT:Think()
local minrange = 100
local target = nil

for k, v in pairs(ents.FindInSphere(self.Entity:GetPos(), 100)) do
	if v:GetClass() == "ent_lowlight" then
		target = v
		print( "yay" )
	end
end

end[/lua]

You didn’t need the extra distance check, ents.FindInSphere already grabs any entity within 100 units. Also if you plan on using the variable “target” in the future, I suggest you make the variable unique to the entity.
self.Entity.target = nil

If you wanted to get fancy, With find me’s code, you could do a trace from the light to the plant to make sure that light is actually hitting it instead of just being in that area.

Actually, screw the FindInSphere, it would be more efficient to do ents.FindByClass, then do your distance check. FindInSphere does a distance check against every entity, since you are only looking for the one class it would be more efficient to limit your distance checks to that. So…

[lua]
local thinkdelay = CurTime() --have a delay too, limits the distance checks further :smiley:

function ENT:Think()

if thinkdelay &lt; CurTime() then thinkdelay = CurTime() + 0.5 else return end

local minrange = 100   
local target = nil  

for k, v in pairs(ents.FindByClass("ent_lowlight") doFindInSphere(self.Entity:GetPos(), 100)) do  
    if self.Entity:GetPos():Distance(v:GetPos()) &lt; 100 then  
        target = v  
        print( "yay" )  
    end  
end  

end [/lua]

Anyway, as a side note, I’ve seen certain LS entities that seem to have light detection actually based off the level of light on the entity, I think there IS a way to get the ambient light on an entity but I’m not sure.