# Finding if a player is inside a certain box defined in LUA

Is it possible to find if a player is inside a certain space? thx

Yes there is.
**[Ents.FindInBox

There’s two things you can do:

[lua]local boxMin = Vector( 0, 0, 0 )
local boxMax = Vector( 128, 128, 128 )
local pos = ply:GetPos()

local inBox = pos > boxMin and pos < boxMax[/lua]

[lua]local boxMin = Vector( 0, 0, 0 )
local boxMax = Vector( 128, 128, 128 )
local inBox = table.HasValue( ents.FindInBox( boxMin, boxMax ), ply )[/lua]

For a single player/entity, you should use the first method, because it’s faster.

thx for the fast reply, trying it now

Are you sure? Creative application of the for loop could yield better results…

[editline]04:01PM[/editline]

This was aimed at Overv. And by better I meant faster.

The efficiency is essentially the same. What do you think the table.HasValue is doing?

-snip-

Apparently there is no higher than operator defined for Vectors by default. Anyway:

[lua]function _R.Vector.__lt( vec1, vec2 )
return vec1.x > vec2.x and vec1.y > vec2.y and vec1.z > vec2.z
end

local boxMin = Vector( 0, 0, 0 )
local boxMax = Vector( 128, 128, 128 )
local ply = LocalPlayer()
local pos = ply:GetPos()

local t1 = os.clock()

for i = 1, 10000 do
local inBox = pos > boxMin and pos < boxMax
end

local t2 = os.clock()

for i = 1, 10000 do
local inBox = table.HasValue( ents.FindInBox( boxMin, boxMax ) )
end

local t3 = os.clock()

print( “Method #1 took " … ( t2 - t1 ) … " seconds.” )
print( “Method #2 took " … ( t3 - t2 ) … " seconds.” )[/lua]

``````
Method #1 took 0.0059999999999718 seconds.
Method #2 took 0.020999999999958 seconds.

``````

That is quite a difference.

[lua]function _R.Vector.__lt( vec1, vec2 )
return vec1.x > vec2.x and vec1.y > vec2.y and vec1.z > vec2.z
end

local boxMin = Vector( 0, 0, 0 )
local boxMax = Vector( 128, 128, 128 )
local ply = LocalPlayer()
local pos = ply:GetPos()

local t1 = os.clock()

for i = 1, 10000 do
local inBox = pos > boxMin and pos < boxMax
end

local t2 = os.clock()

for i = 1, 10000 do
local inBox = table.HasValue( ents.FindInBox( boxMin, boxMax ) )
end

local t3 = os.clock()

local lEnts = ents.FindInBox( boxMin, boxMax )
for i = 1, 10000 do
local inBox = table.HasValue( lEnts )
end

local t4 = os.clock()

print( “Method #1 took " … ( t2 - t1 ) … " seconds.” )
print( “Method #2 took " … ( t3 - t2 ) … " seconds.” )
print( “Method #3 took " … ( t4 - t3 ) … " seconds.” )[/lua]

``````

Method #1 took 0.0049999999999955 seconds.
Method #2 took 0.02000000000001 seconds.
Method #3 took 0.0030000000000001 seconds.

``````

Doing ents.FindInBox 10000 times doesn’t give a comparable time.

So we can conclude that ents.FindInBox is the best solution for multiple players, and the comparison method is the best for only one or a few players?

Your method #3 makes no sense. Instead of checking 10000 times what is in the specified box, it only checks the same table over and over again. When you compare methods, they all have to do the same thing. The equivalent of your method #3 is this:
[lua]local inBox = pos > boxMin and pos < boxMax
for i = 1, 10000 do
inBox = inBox
end[/lua]

Exactly.

That’s exactly what your method #1 does, since you’re storing the player’s position outside the loop. Neither of the methods 1 or 3 won’t know if the player’s position has changed or if there’s new entities inside the box. Method 2 would be the only correct one from there in that case.

You’re right, but method #1 is still significantly faster.

[lua]function _R.Vector.__lt( vec1, vec2 )
return vec1.x > vec2.x and vec1.y > vec2.y and vec1.z > vec2.z
end

local boxMin = Vector( 0, 0, 0 )
local boxMax = Vector( 128, 128, 128 )
local ply = LocalPlayer()

local t1 = os.clock()

for i = 1, 10000 do
local pos = ply:GetPos()
local inBox = pos > boxMin and pos < boxMax
end

local t2 = os.clock()

for i = 1, 10000 do
local inBox = table.HasValue( ents.FindInBox( boxMin, boxMax ) )
end

local t3 = os.clock()

print( “Method #1 took " … ( t2 - t1 ) … " seconds.” )
print( “Method #2 took " … ( t3 - t2 ) … " seconds.” )[/lua]

``````
Method #1 took 0.019999999999982 seconds.
Method #2 took 0.03400000000002 seconds.

``````

Whether method #1 is faster than #2 or is not an argument anyway. When you use ents.FindInBox, it compares against every entity whereas my method only compares against a single entity.