Garry's Mod Wiki

Revision Difference

Entity:GetAttachment#526774

<function name="GetAttachment" parent="Entity" type="classfunc"> <description> Gets the orientation and position of the attachment by its ID, returns nothing if the attachment does not exist. <note>The update rate of this function is limited by the setting of ENT.AutomaticFrameAdvance for <page>Scripted Entities</page>!</note> <bug issue="1255">This will return improper values for viewmodels if used in <page>GM:CalcView</page>.</bug> </description> <realm>Shared</realm> <args> <arg name="attachmentId" type="number">The internal ID of the attachment.</arg> </args> <rets> <ret name="" type="table">The angle and position of the attachment. See the <page>Structures/AngPos</page>. Most notably, the table contains the keys "Ang" and "Pos".</ret> </rets> </function> <example> <description>Grabs the muzzle position of a player's view model</description>⤶ <description>Grabs the muzzle position of a player's view model.</description>⤶ <code> local vm = ply:GetViewModel() local obj = vm:LookupAttachment( "muzzle" ) local muzzlepos = vm:GetAttachment( obj )⤶ ⤶ if (obj < 1) then⤶ local muzzle = vm:GetAttachment( obj )⤶ print( muzzle.Pos, muzzle.Ang )⤶ end⤶ </code>⤶ ⤶ </example>⤶ ⤶ <example>⤶ <description>Draws a green cube at the player's view model muzzle if the model has a "muzzle" attachment. This properly translates the attachment into the view model projection space.</description>⤶ <code>⤶ -- FIXME: The nFOV parameter should be replaced with ViewModelFOV() when it's binded⤶ local function FormatViewModelAttachment(nFOV, vOrigin, bInverse --[[= false]])⤶ local vEyePos = EyePos()⤶ local aEyesRot = EyeAngles()⤶ local vTransformed = vOrigin - vEyePos⤶ local vForward = aEyesRot:Forward()⤶ ⤶ local flViewX = math.tan(nFOV * math.pi / 360)⤶ ⤶ if (flViewX == 0) then⤶ vForward:Mul(vForward:Dot(vTransformed))⤶ vTransformed:Add(vForward)⤶ ⤶ return vTransformed⤶ end⤶ ⤶ -- FIXME: LocalPlayer():GetFOV() should be replaced with EyeFOV() when it's binded⤶ local nWorldX = math.tan(LocalPlayer():GetFOV() * math.pi / 360)⤶ ⤶ if (nWorldX == 0) then⤶ vForward:Mul(vForward:Dot(vTransformed))⤶ vTransformed:Add(vForward)⤶ ⤶ return vTransformed⤶ end⤶ ⤶ local nFactor = nWorldX / flViewX⤶ local vRight = aEyesRot:Right()⤶ local vUp = aEyesRot:Up()⤶ ⤶ if (bInverse) then⤶ vRight:Mul(vRight:Dot(vTransformed) / nFactor)⤶ vUp:Mul(vUp:Dot(vTransformed) / nFactor)⤶ else⤶ vRight:Mul(vRight:Dot(vTransformed) * nFactor)⤶ vUp:Mul(vUp:Dot(vTransformed) * nFactor)⤶ end⤶ ⤶ vForward:Mul(vForward:Dot(vTransformed))⤶ ⤶ vTransformed:Add(vRight)⤶ vTransformed:Add(vUp)⤶ vTransformed:Add(vForward)⤶ ⤶ return vTransformed⤶ end⤶ ⤶ local vBoxMins = Vector(-32, -32, -32)⤶ local vBoxMaxs = Vector(32, 32, 32)⤶ local colGreen = Color(0, 0, 255)⤶ ⤶ function SWEP:ViewModelDrawn()⤶ local pOwner = self:GetOwner()⤶ if (not pOwner:IsValid()) then return end⤶ ⤶ local pViewModel = pOwner:GetViewModel()⤶ if (not pViewModel:IsValid()) then return end⤶ ⤶ local uAttachment = pViewModel:LookupAttachment("muzzle")⤶ if (uAttachment < 1) then return end⤶ ⤶ local tAttachment = pViewModel:GetAttachment(uAttachment)⤶ if (tAttachment == nil) then return end⤶ ⤶ -- FIXME: This should be removed when ViewModelFOV() is binded⤶ local nFOV = self.ViewModelFOV⤶ if (not isnumber(nFOV)) then nFOV = 62 end⤶ ⤶ render.DrawBox(FormatViewModelAttachment(nFOV, tAttachment.Pos, false), tAttachment.Ang, vBoxMins, vBoxMaxs, colGreen)⤶ end⤶ </code> </example>