-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
NPC Class Documentation
This page lists methods and fields pertaining to the NPC
class. This page is useful for understanding what to use in ModNPC.SetDefaults
and ModNPC.AI
.
Only common fields and methods are listed. The full documentation should be consulted for a full listing of methods and fields not listed here. You can also see the names and documentation for these methods and fields directly in your IDE once it is setup correctly.
Index |
---|
Fields |
Methods |
You can assign these fields to give your ModNPC
various values. Typically you'll want to refer to this page when writing code for ModNPC.SetDefaults
and ModNPC.AI
. Be sure to visit Vanilla NPC Field Values to see what values vanilla npc use for these fields. All fields listed are public unless otherwise noted.
Field | Type | Default Value | Description |
---|---|---|---|
width | int | 0 | The width of the npc hitbox |
height | int | 0 | The height of the npc hitbox |
aiStyle | int | 0 (-1 for ModNPC) | Determines which AI code to run. Many NPC share the same AI code by having the same aiStyle. ModNPC can utilize vanilla aiStyle values in addition to ModNPC.aiType to mimic vanilla behavior to some degree. If you wish to further customize the AI of a ModNPC, you should read Advanced Vanilla Code Adaption. If you are using completely custom AI, set this to -1 since an aiStyle of 0 will face the player automatically, which may interfere with your logic. |
damage | int | 0 | The amount of damage this NPC will deal on collision. Usually npc.damage is scaled by some factor when NPC spawn projectiles with Projectile.NewProjectile in AI code. For example, passing in (int)(npc.damage * 0.5f) as the Damage parameter. |
defDamage | int | 0 | Stores the value of damage at the end of SetDefaults. Useful for scaling damage in AI code. |
defense | int | 0 | How resistant to damage this NPC is. |
defDefense | int | 0 | Stores the value of defense at the end of SetDefaults. Useful for scaling defense in AI code, like how King Slime changes defense as it gets smaller. |
lifeMax | int | 0 | The maximum life of this NPC. |
life | int | 0 | The current life of the NPC. Automatically assigned to lifeMax at the end of SetDefaults. |
realLife | int | -1 | This value is used for multi-part npcs that share life while maintaining separate hitboxes. This is useful for worm enemies. The value represents the index in Main.npc of the npc that has the actual life for this multi-part npc. All the segments will point to the same npc, usually the "head" of the npc, and the head and all other non-multi-part npcs will have the default value of -1 still. |
HitSound | LegacySoundStyle | null | The sound that plays when this npc is hit. |
DeathSound | LegacySoundStyle | null | The sound that plays when this npc dies. |
alpha | int | 0 | 0 is opaque, and 255 is transparent. This is the opposite of typical representations of alpha in computer graphics, so be aware of that. |
color | Color | ||
value | float | 0f | How many copper coins the NPC will drop when killed (100 copper coins = 1 silver coin etc.). |
rarity | int | 0 | How rare the NPC is for the Lifeform Analyzer. 4 is for Tim and Mimics, 1 is for bound town NPCs. |
immune | int[] | all 0 | This determines if an NPC can be hit by a item or projectile owned by a particular player (it is an array, each slot corresponds to different players (whoAmI)). It is decremented towards 0 every update. Melee items set immune[player.whoAmI] to player.itemAnimation, which starts at item.useAnimation and decrements towards 0. Penetrating projectiles usually set immune to 10, while non-penetrating projectiles do not set immune. ExampleAnimatedPierce.cs explains more about options for npc and projectile immunity. |
buffImmune | bool[] | all false | Make the NPC immune to buffs/debuffs. Example: npc.buffImmune[BuffID.OnFire] = true;
|
knockBackResist | float | 1f | How much of the knockback it receives will actually apply. 1f: full knockback; 0f: no knockback. |
scale | float | 1f | Makes the NPC bigger or smaller. Bigger than 1f = bigger. |
townNPC | bool | false | If true, the npc counts as a town NPC. Has multiple implications (i.e. npc shop, housing, hostile spawn rate reduction). |
noGravity | bool | false | If true, the npc will not be affected by gravity. Demon Eyes and other floating npc use this. |
noTileCollide | bool | false | If true, the npc does not collide with tiles, making the npc pass through tiles freely. Meteor Head and Worm npc use this. |
npcSlots | float | 1f | Represents how much this npc counts towards the npc spawn limit. Small critters like worms or flys have values around .1f and .25. Enemies that are more difficult than normal have values higher than 1f, and bosses and mini bosses have values around 6. Proper npcSlots values help balance the game and prevents the player from randomly being overwhelmed by powerful enemies. Check the Vanilla NPC Field Values spreadsheets to determine a suitable value for your NPC. |
boss | bool | false | Set to true if the NPC is a boss. Prevents off-screen despawn. |
netAlways | bool | false | If true, makes sure that this NPC is always synced in multiplayer for newly joined players (same happens if it's a boss or townNPC). |
Static fields are accessed by the classname, not the instance. For example, we write NPC.downedPlantBoss
to check if Plantera has been defeated yet.
Field | Type | Default Value | Description |
---|---|---|---|
killCount | int[] | 0 | Indexed by BannerID. Returns how many kills of the specified BannerID have been killed. Many NPC can share the same BannerID and many NPC actually don't have a Banner ID at all. This code will retrieve the bannerID of an NPC, or -1 if it does not exist: int bannerID = Item.NPCtoBanner(npc.BannerID());
|
savedTaxCollector savedGoblin savedWizard savedMech savedAngler savedStylist savedBartender |
bool | false | These values indicate which NPC have been found in the wild. You can use these in various conditions in your mod. |
downedBoss1 downedBoss2 downedBoss3 downedDeerclops downedQueenBee downedSlimeKing downedGoblins downedFrost downedPirates downedClown downedPlantBoss downedGolemBoss downedMartians downedFishron downedEmpressOfLight downedHalloweenTree downedHalloweenKing downedChristmasIceQueen downedChristmasTree downedChristmasSantank downedAncientCultist downedMoonlord downedTowerSolar downedTowerVortex downedTowerNebula downedTowerStardust downedTowers downedQueenSlime downedMechBossAny downedMechBoss1 downedMechBoss2 downedMechBoss3 |
bool | false | These values indicate which bosses have been defeated in the world. Most are self explanitory. downedBoss1 refers to Eye of Cthulu, downedBoss2 refers to either Brain of Cthulhu or Eater of Worlds, and downedBoss3 referes to Skeletron. downedMechBoss 1, 2, and 3 refer to The Destroyer, The Twins, and Skeletron Prime respectively. |
Field | Type | Default Value | Description |
---|---|---|---|
modNPC | ModNPC | null | The ModNPC instance that controls the behavior of this NPC. This property is null if this is not a modded NPC. |
globalNPCs | internal GlobalNPC[] | new GlobalNPC[0] | Do not touch. Use NPC.GetGlobalNPC
|
Remember that static methods are called by writing the classname and non-static methods use the instance name. NPC.NewNPC(...)
vs npc.CloneDefaults(...)
public static int NewNPC(int X, int Y, int Type, int Start = 0, float ai0 = 0f, float ai1 = 0f, float ai2 = 0f, float ai3 = 0f, int Target = 255)
Spawns an NPC in the world. Use this to spawn minions from bosses.
Example: NPC.NewNPC((int)npc.position.X, (int)npc.position.Y, ModContent.NPCType<ExampleBossMinion>();
The ai
parameters can initialize an NPC with particular data if needed for special AI behavior.
Returns true if there are any NPC of the supplied type alive in the world. Useful for boss spawning items or anytime you need to check if an NPC is alive.
Example: if (NPC.AnyNPCs(ModContent.NPCType<CaptiveElement2>())) {
Same as AnyNPCs
except returns the number of active NPC of the supplied type.
Returns the index of the first NPC of the supplied type. Returns -1 if not found. Use to index into Main.npc
to get the NPC instance.
Spawns a boss somewhere off screen of the specified player. This is the most common way of spawning bosses.
Gets the GlobalNPC instance (with the given name and added by the given mod) associated with this npc instance.
Same as the other GetGlobalNPC, but assumes that the class name and internal name are the same.
Same as the other GetGlobalNPC, but assumes that the class name and internal name are the same, as well as the Mod. This is the one you should use 99% of the time.
Allows you to copy the defaults of a different type of NPC.