How the hell do you use trace bitmasks?

So I’ve made a trace and it works fine and dandy but i’ve come along a problem while i’m trying to detect if there is world above the player and to output its vector. I can get it to detect the world above the player but my if then else statement isn’t working because the HitPos is always outputting the EndPos.

is there a way to output the HitPos only when the Trace hits the world? And could someone clarify how to use bit masks?

[lua]
function OverHeadView( ply, origin, angles, fov )

local pos = ply:GetPos()

local tracedata = {}
tracedata.start = pos
tracedata.endpos = pos+Vector( 0, 0, 600 )

// tracedata.filter = MASK_SOLID + CONTENTS_SOLID
// tracedata.mask =
// tracedata.contents =
local trace = util.TraceLine(tracedata)

	if trace.HitPos then
		
		
		tracehitworld = trace.HitPos
	
		local viewelse = {}			
		local view = {}

		view.origin = origin + Vector(0,0,tracehitpos.z-1)
		view.angles = Angle(90,angles.y,angles.r)
		
		return view
		
		else
		
		local viewelse = {}			
		viewelse.origin = origin + Vector(0,0,tracehitpos.z-1)
		viewelse.angles = Angle(90,angles.y,angles.r)

		return viewelse

	
	end

end
[/lua]

What you’re looking for are bitwise operations and they are documented [here

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?search=Bitwise).

So I believe you would use MASK_SOLID | CONTENTS_SOLID for your filter.

And you can check the boolean value trace.HitWorld to see if it hit the world or not.

Awesome, but how would I use those with my Trace?

Would it be

tracedata.filter = MASK_SOLID | CONTENTS_SOLID

Yes, yes it would. :smile:

Amazing, I didn’t even know about bitwise operators, now, can you only use these operators when you’re dealing with trace filters or can it be any type of data? Oh, would it be better if I used
tracedata.filter = MASK_SOLID & CONTENTS_SOLID instead or would it make much of a difference?

That would result in 0. And bitwise operators can be used with any number. These MASK_ and CONTENTS_ enumerations are really integers.

Well, I did what I thought would work but it doesn’t, could you help me find out whats wrong?

What it’s supposed to do is set the players view 600 units above them and lock their view. (Vertical aiming is locked in other code). But when a player enters a building (What the trace is for) it should lower the players view to the trace’s end position -1.
[lua]
function OverHeadView( ply, origin, angles, fov )

local pos = ply:GetPos()

local tracedata = {}
tracedata.start = pos
tracedata.endpos = pos+Vector( 0, 0, 600 )
tracedata.filter = MASK_SOLID | CONTENTS_SOLID
local trace = util.TraceLine(tracedata)
	
	if trace.HitWorld then
		
		
		local tracehitpos = Vector(trace.HitPos)
		local view = {}

		view.origin = origin + Vector(0,0,tracehitpos.z-1)
		view.angles = Angle(60,angles.y,angles.r)
		
		return view
		
		else
		
		local viewelse = {}			
		
		viewelse.origin = origin + Vector(0,0,600)
		viewelse.angles = Angle(90,angles.y,angles.r)

		return viewelse

	
	end

end
[/lua]

Hmm maybe you should know that tracedata.filter are stuff the trace will NOT hit. So all you need in there is the player’s entity.

Sooo, I make it into [lua]tracedata.filter = self.Player[/lua]

self is not defined, self.Player isn’t either. If you took a look at your script you would see you (or the people who’s script you’ve copy-pasted) have used the variable **ply **to represent the player object.

So it would be : [lua]tracedata.filter = ply[/lua]

I just did that, and I wrote all of this myself. I’ve only been using the wiki for reference. I’m new to lua so meh, i’ll miss stuff like that.

Why don’t you just check if trace.HitWorld is true instead of using bit masks?