ModNPC Class
Base class for creating custom NPCs, enemies, and bosses.
Overview
The ModNPC class allows you to create custom non-player characters including friendly NPCs, enemies, and bosses with custom AI behaviors. This builds on concepts from ModItem, so review that first if you're new to tModLoader development.
Basic Enemy Example
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;
namespace YourMod.NPCs
{
public class ExampleEnemy : ModNPC
{
public override void SetStaticDefaults()
{
Main.npcFrameCount[Type] = 6; // Number of animation frames
}
public override void SetDefaults()
{
NPC.width = 40;
NPC.height = 40;
NPC.damage = 20;
NPC.defense = 10;
NPC.lifeMax = 100;
NPC.HitSound = SoundID.NPCHit1;
NPC.DeathSound = SoundID.NPCDeath1;
NPC.value = 100f; // Money dropped
NPC.knockBackResist = 0.5f;
NPC.aiStyle = NPCAIStyleID.Fighter; // Zombie-like AI
}
public override float SpawnChance(NPCSpawnInfo spawnInfo)
{
return SpawnCondition.OverworldNightMonster.Chance * 0.1f;
}
}
}
Creating a Boss
Bosses require more complex setup with custom AI:
public class ExampleBoss : ModNPC
{
public override void SetStaticDefaults()
{
Main.npcFrameCount[Type] = 4;
NPCID.Sets.BossBestiaryPriority.Add(Type);
}
public override void SetDefaults()
{
NPC.width = 100;
NPC.height = 100;
NPC.damage = 40;
NPC.defense = 15;
NPC.lifeMax = 5000;
NPC.boss = true; // Mark as boss
NPC.npcSlots = 10f;
NPC.value = Item.buyPrice(gold: 5);
NPC.knockBackResist = 0f; // Immune to knockback
NPC.noGravity = true; // Flies
NPC.noTileCollide = true;
Music = MusicID.Boss1;
}
public override void AI()
{
// Custom AI code here
}
}
NPC Properties Guide
aiStyle
Built-in AI patterns:
- • Fighter (zombie-like)
- • Flying (bat-like)
- • Worm (segmented)
- • Custom (write your own)
boss
Mark NPC as boss:
- • Shows health bar
- • Prevents despawning
- • Boss music plays
- • Special death effects
For complete ModNPC documentation:
View Full Documentation