public class MyNewNPC : ModNPC { public override void SetStaticDefaults() { // Set the display name DisplayName.SetDefault("My New NPC"); } public override void SetDefaults() { // Set the NPC current health npc.lifeMax = 250; // Set the NPC maximum movement speed npc.aiStyle = 1; aiType = NPCID.DemonEye; // Set the NPC damage and defense npc.damage = 20; npc.defense = 5; // Set the NPC sound effects npc.HitSound = SoundID.NPCHit1; npc.DeathSound = SoundID.NPCDeath1; // Set the animation frame count for the NPC npc.aiStyle = 10; } public override float SpawnChance(NPCSpawnInfo spawnInfo) { // Set the NPC spawn rate in the game world return SpawnCondition.Underground.Chance * 0.1f; } public override void NPCLoot() { // Add items to the NPC loot table Item.NewItem(npc.getRect(), ItemID.DirtBlock, Main.rand.Next(1, 4)); } }
public class CustomNPC : GlobalNPC { public override bool PreAI(NPC npc) { // Change the NPC movement speed npc.velocity.X = 2f; return true; } }In this example, we customize an existing NPC's movement speed by changing its horizontal velocity to 2f. We do this by creating a new class called "CustomNPC" that inherits from the "GlobalNPC" class and overrides the "PreAI" method. This method is called before the NPC update logic is executed, allowing us to modify the NPC's behavior. Package library: Terraria.ModLoader.