# Entity:GetAttachment

- **Ortam:** İstemci, Sunucu
- **İngilizce aslı:** https://wiki.facepunch.com/gmod/Entity:GetAttachment
- **Çeviri tarihi:** 2025-09-08

```lua
table veya nil Entity:GetAttachment( number attachmentId )
```

## Açıklama

Eklentinin ID'sine göre yönünü ve pozisyonunu alır, eklenti mevcut değilse hiçbir şey döndürmez.

> **NOT:** Bu fonksiyonun güncelleme hızı, [Scripted Entities](https://gmod.tr/wiki/Scripted_Entities) için ENT.AutomaticFrameAdvance ayarı ile sınırlıdır!

> **HATA:** Bu fonksiyon, [GM:CalcView](https://gmod.tr/wiki/GM:CalcView) içerisinde kullanıldığında viewmodel'lar için hatalı değerler döndürür.
> Sorun Takipçisi: [1255](https://github.com/Facepunch/garrysmod-issues/issues/1255)

## Argümanlar

1. [number](https://gmod.tr/wiki/number) **attachmentId**: Eklentinin dahili ID'si.

## Döndürülen Değerler

1. [table](https://gmod.tr/wiki/table) ([AngPos yapısı](https://gmod.tr/wiki/Structures/AngPos) ile) veya [nil](https://gmod.tr/wiki/nil): Eklentinin açısını ve pozisyonunu içeren tablo veya mevcut değilse `nil`. [AngPos yapısına](https://gmod.tr/wiki/Structures/AngPos) bakınız. En önemlisi, tablo `Ang` ve `Pos` anahtarlarının yanı sıra `Bone` anahtarını da içerir.

### Örnek

Bir oyuncunun view model'ının namlu (muzzle) pozisyonunu alır.

```lua
local vm = ply:GetViewModel()
local obj = vm:LookupAttachment( "muzzle" )

if (obj > 0) then
	local muzzle = vm:GetAttachment( obj )
	print( muzzle.Pos, muzzle.Ang )
end
```

### Örnek

Eğer modelin bir "muzzle" eklentisi varsa, oyuncunun view model'ının namlusuna yeşil bir küp çizer. Bu, eklentiyi view model projeksiyon uzayına doğru bir şekilde çevirir.

```lua
local function FormatViewModelAttachment(vOrigin, bFrom --[[= false]])

	local view = render.GetViewSetup()

	local vEyePos = view.origin
	local aEyesRot = view.angles
	local vOffset = vOrigin - vEyePos
	local vForward = aEyesRot:Forward()

	local nViewX = math.tan( view.fovviewmodel_unscaled * math.pi / 360)

	if (nViewX == 0) then
		vForward:Mul(vForward:Dot(vOffset))
		vEyePos:Add(vForward)

		return vEyePos
	end

	local nWorldX = math.tan( view.fov_unscaled * math.pi / 360)

	if (nWorldX == 0) then
		vForward:Mul(vForward:Dot(vOffset))
		vEyePos:Add(vForward)

		return vEyePos
	end

	local vRight = aEyesRot:Right()
	local vUp = aEyesRot:Up()

	if (bFrom) then
		local nFactor = nWorldX / nViewX
		vRight:Mul(vRight:Dot(vOffset) * nFactor)
		vUp:Mul(vUp:Dot(vOffset) * nFactor)
	else
		local nFactor = nViewX / nWorldX
		vRight:Mul(vRight:Dot(vOffset) * nFactor)
		vUp:Mul(vUp:Dot(vOffset) * nFactor)
	end

	vForward:Mul(vForward:Dot(vOffset))

	vEyePos:Add(vRight)
	vEyePos:Add(vUp)
	vEyePos:Add(vForward)

	return vEyePos
end

SWEP.BoxAttachment = "muzzle"
SWEP.BoxMins = Vector(-2, -2, -2)
SWEP.BoxMaxs = Vector(2, 2, 2)
SWEP.BoxColor = Color(0, 255, 0)

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(self.BoxAttachment)
	if (uAttachment < 1) then return end

	local tAttachment = pViewModel:GetAttachment(uAttachment)
	if (tAttachment == nil) then return end

	render.DrawWireframeBox(FormatViewModelAttachment(tAttachment.Pos, false), tAttachment.Ang, self.BoxMins, self.BoxMaxs, self.BoxColor, true)
end
```

- [GMod Wiki ana sayfası](https://gmod.tr/wiki)

---

[Tüm sunucular](https://gmod.tr/) · [GmodTR Launcher](https://gmod.tr/launcher) · [Rehber](https://gmod.tr/rehber) · [GMod Wiki](https://gmod.tr/wiki) · [GmodTR nedir?](https://gmod.tr/hakkinda) · [Sunucu ekle](https://gmod.tr/sunucu-ekle) · [Discord](https://discord.gg/gmodtr)

Kaynak: https://gmod.tr/wiki/Entity:GetAttachment
