# NPC:GetEnemy

- **Ortam:** Sunucu
- **İngilizce aslı:** https://wiki.facepunch.com/gmod/NPC:GetEnemy
- **Çeviri tarihi:** 2025-09-08

```lua
NPC NPC:GetEnemy()
```

## Açıklama

Bu NPC'nin savaşmaya çalıştığı varlığı (entity) döndürür.

> **HATA:** Eğer NPC'nin bir düşmanı yoksa bu fonksiyon nil döndürür. Düşmanın geçerliliğini doğrulamak için dönüş değerinde [IsValid](https://gmod.tr/wiki/Global.IsValid) (nil ve NULL değerlerini hesaba katar) kullanmalısınız.
> Sorun Takipçisi: [3132](https://github.com/Facepunch/garrysmod-issues/issues/3132)

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

1. [NPC](https://gmod.tr/wiki/NPC): Düşman NPC.

### Örnek

İlk oyuncuyu düşmanı olarak belirleyen herhangi bir NPC'yi öldürür.

```lua
local function Think()
	for i, ent in ipairs( ents.GetAll() ) do
		if ent:IsNPC() and ent:GetEnemy() == Entity( 1 ) then
			ent:TakeDamage( 999 )
		end
	end
end

hook.Add( "Think", "Kill My Enemies", Think )
```

**Çıktı:** Düşmanını Entity( 1 ) olarak belirleyen herhangi bir NPC ölür.

### Örnek

Düşmanı olmayan (ve tercihen Oyunculara karşı bir [D_HT](https://gmod.tr/wiki/Enums/D) ilişkisine sahip olan) her NPC'nin rastgele bir Oyuncuya saldırmaya başlamasını sağlar. Bu kod, [My Base Defence](https://steamcommunity.com/sharedfiles/filedetails/?id=1647345157) oyun modundan kopyalanmıştır:

```lua
-- ...
local function _SetRandomPlayerTargetForNPC(npc)
	if (npc:IsNPC()) then
		if (!IsValid(npc:GetEnemy())) then
			local _allPlayers	= player.GetAll()
			local _winnerPlNr	= math.random(1, #_allPlayers)

			timer.Simple(0.15, function()
				local __Player = _allPlayers[_winnerPlNr]
				--
				--- Set the enemy for the NPC, so it does not just stand there doing nothing
				-- lika young lazy teen or something
				if (!npc:IsValid() or !__Player:IsValid()) then return end
				npc:SetEnemy(__Player)
				npc:UpdateEnemyMemory(__Player, __Player:GetPos())
				npc:SetSchedule(SCHED_SHOOT_ENEMY_COVER)
			end)
		end
	end
end
-- - - ---
-- --
-- Make every NPC that might not have a target, recive one random Player..:>>
-- (..you can place this loop inside an interval timer or something...)
for i, ent in ipairs( ents.GetAll('npc_*') ) do
	if ent:IsValid() then _SetRandomPlayerTargetForNPC( ent ) end
end
```

**Çıktı:** SUNUCUDAKİ tüm NPC'lerin, eğer zaten bir düşmanları yoksa, hafızaları güncellenir ve düşmanın bilinen son konumuna doğru hareket edip saldırmaya çalışırlar. Bu düşman, rastgele bir Oyuncu olacaktır. Ayrıca düşman siperlerine ateş etmeye de çalışacaklardır.

- [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/NPC:GetEnemy
