Fandom

Surfpup's tConfig Mod Wiki

NPC Class

124pages on
this wiki
Add New Page
Comments41 Share
The information in this article is for Terraria v1.0.6.1.

This class is used to control the behaviors of every N.P.C. in the game, whether a friendly N.P.C. or a enemy N.P.C. This includes their AI, position in the world, sprites used, and pretty much any other thing that an N.P.C. has. 
NPC 53

The Groom

Read More


PropertiesEdit

Name Type Description
active bool Set to false to kill the NPC
ai float[] An array of four float variables you can use to sync NPC information between different clients on a server.
aiAction int holds 'state' or 'current action' of npc. FindFrame uses it to animate slimes without checking details of ai[]
aiStyle int
alpha int
behindTiles bool
boss bool Sets if the N.P.C. is a boss.
buffImmune bool[] Sets whichbuffs or de-buffs the N.P.C. is immune to.
buffTime int[]
buffType int[]
closeDoor bool
collideX bool
collideY bool
color Color
damage int Sets how much damage the N.P.C. does.
defense int Sets how much defense the N.P.C. has.
direction int
directionY int
dontTakeDamage bool Sets if the N.P.C. is invincible.
doorX int
doorY int
frame Rectangle

frame is a rectangle with the follow properties: X, Y, Width, Height. Width and Height are the width and height of the frame, while X and Y are the position of the sprite on the sprite strip.

frameCounter double
friendly bool Sets if the N.P.C. is aggressive to the player character.
friendlyRegen int
height int Sets the height of the sprite of the N.P.C.
homeless bool
homeTileX int
homeTileY int
immune int[]
justHit bool
knockBackResist float Resistance to nockback multiplier for NPCs. 0.1 = 90% reduction in knockback (10% as much), 2.0 = knocked back twice as far, etc.
lavaImmune bool Sets if the N.P.C. is immune to lava.
lavaWet bool
life int Sets the N.P.C.'s current life.
lifeMax int Sets the N.P.C.'s max life.
lifeRegen int Sets the N.P.C.'s life regen rate.
lifeRegenCount int
name string Sets the name of the N.P.C.
netUpdate bool
noGravity bool Sets if the N.P.C. is affected by gravity.
noTileCollide bool Sets if the N.P.C. collides with tiles.
oldDirection int
oldDirectionY int
oldPosition Vector2
oldTarget int
oldVelocity Vector2 (see below)
onFire bool Sets if the N.P.C. is on fire.
poisoned bool Sets if the N.P.C. is poisoned.
position Vector2
rotation float
scale float
soundDelay int
soundHit int Sets the sound when the N.P.C. is hit.
soundKilled int Sets the sound when the N.P.C. is killed.
spriteDirection int -1 to face left, 1 to face right.
target int The playerID(whoAmI) of the player the npc is currently targeting.
targetRect Rectangle
timeLeft int
townNPC bool Sets if the N.P.C. can move into houses.
type int
value float Sets how many coins the N.P.C. drops on death.
velocity Vector2
wet bool
wetCount byte
whoAmI int further testing needed: the position of the NPC in the array Main.npc
width int Sets the width of the sprite of the N.P.C.

Static PropertiesEdit

Name Type Description
downedBoss1 bool Eye of Cthulhu killed?
downedBoss2 bool Eater of Worlds killed?
downedBoss3 bool Skeletron killed?
downedClown bool Killed a clown in Hardmode.
downedFrost bool Defeated Frost Legion.
downedGoblins bool Defeated Goblin Army.
immuneTime int Time invincible after a hit (?)
maxAI int
maxAttack int
maxSpawns int default value is 5 and it gets multiplied to a higher amount based on the area.
savedGoblin bool Goblin can move in.
savedWizard bool Wizard can move in.
savedMech bool Mechanic can move in.
sHeight int Screen height?
spawnRate int the interval between spawns , every frame it is set to its default value , 600 and then gets modified to a smaller amount based on the biomes and time of day (that's per player).

spawnSpaceX

int
spawnSpaceY int
sWidth int Screen width?

MethodsEdit

Method Name Description
AddBuff()
public void AddBuff(int type, int time, [bool quiet = false])
{
}

Adds a buff with a given type and duration to the NPC. Time is in frames, 60 frames per second. Unknown what quite it does.

  • Returns no value
AI()
public void AI()
{
}

When invoked, method calls the AI for the NPC, as specified by the NPC's AI value.

  • Returns no value
changeFrame()
public void changeFrame(int frameNum)
{
}

Further testing needed: Changes the NPC's Frame.

  • Returns no value
CheckActive()
public void CheckActive()
{
}

Checks to see if the NPC is alive.

  • Returns no value
Clone()
public object Clone()
{
}

Creates a copy of the NPC.

  • Returns copy of NPC as a new object
DelBuff()
public void DelBuff(int b)

Removes a buff from the NPC. Unknown whether b refers to the index of the NPC's buffs, or the ID of the buff itself.

  • Returns no value
FindFrame()
public void FindFrame()
{
}

Handles frames and NPC Direction and stuff like that.

  • Returns no value
GetAlpha()
public Color GetAlpha(Color newColor)
{
}

Returns the alpha blending value of the NPC.

  • Returns alpha as Color
GetChat()
public string GetChat()
{
}

???

  • Returns ??? as string
GetColor()
public Color GetColor(Color newColor)
{
}

Returns the color blending value of the NPC.

  • Returns NPC's color as Color
getFrame()
public int getFrame()
{
}

???

  • Returns ??? as int
HitEffect()
public void HitEffect(int hitDirection = 0, double dmg = 10)
{
}

Deals damage to the NPC, with a given damage and direction of damage. Unsure of the difference between this and StrikeNPC() - this may be cosmetic only.

  • Returns no value
NPCLoot()
public void NPCLoot()
{
}

Handle death events and drop items.

  • Returns no value
SetDefaults()
public void SetDefaults(int Type, [float scaleOverride = -1f])
{
}

???

  • Returns no value
SetDefaults()
public void SetDefaults(string Name)
{
}

???

  • Returns no value
StrikeNPC()
public double StrikeNPC(int Damage, float knockBack, int hitDirection)
{
}

Damages the NPC with a given damage, knockback, and direction the damage came from.

  • Returns ??? as double (likely damage done)
TargetClosest()
void TargetClosest(bool faceTarget = true)
{
}

Targets the closest player(?). If passed as a parameter, a bool will determine whether it should face the target or not.

  • Returns no value
Transform()
public void Transform(int newType)
{
}

???

  • Returns no value
UpdateNPC()
public void UpdateNPC(int i)
{
}

???

  • Returns no value

Static MethodsEdit

Method Name Description
AnyNPCs()
public static bool AnyNPCs(int Type)
{
}

For a given type, checks if any NPCs of that type exist.

  • True if they true do
NearSpikeBall()
public static bool NearSpikeBall(int x, int y)
{
}

Checks whether an NPC is near a spikeball at the given coordinates? 

NewNPC()
int NewNPC(int X, int Y, int Type, [int Start = 0])
{
}

Spawns a new MOBNPCof the given type at the specified location.

  • This is turn NPXC
SpawnNPC()
public static void SpawnNPC()
{
}

Controls where and when the NPC spawns.

  • This is no value
SpawnOnPlayer()
public static void SpawnOnPlayer(int plr, int Type)
{
}

Spawns the given NPC on top of the given player.

  • This is no value
SpawnSkeletron()
void SpawnSkeletron()
{
}

Spawns Skeletron!

  • This is no value

NotesEdit

npc.oldVelocityEdit

While messing with this field (not exactly sure what it's use is yet), I thought you could use it to determine monster facing in custom frame sequences (since figuring out which way they should be facing while not moving is difficult). It turns out I was wrong. But here's a way to do that, just in case you were curious:

float lastvelocityX = 0;
public void FindFrame(int currentframe)
{
    int frameh = 1;
    if (!Main.dedServ) //get the height of one frame
    {
        frameh = Main.npcTexture[npc.type].Height / Main.npcFrameCount[npc.type];
    }
    if (npc.velocity.X == 0 && lastvelocityX < 0)
    {
        npc.spriteDirection = -1;
    }
    else if (npc.velocity.X < 0)
    {
        npc.spriteDirection = -1;
        lastvelocityX = npc.velocity.X;
    }
    else
    {
        npc.spriteDirection = 1;
        lastvelocityX = npc.velocity.X;
    }
    npc.frameCounter += 1.0;
    if (npc.frameCounter >= 6.0)
    {
        npc.frame.Y = npc.frame.Y + frameh;
        npc.frameCounter = 0.0;
    }
    if (npc.frame.Y >= frameh * Main.npcFrameCount[npc.type]) //check if at end of frame file, you could substitute the number of the end of a frame section here, ahem
    {
        npc.frame.Y = 0;
    }
}

This is a pretty standard frame animation that will keep it facing the right way. This goes within your NPC Name.cs file. This code will change the frame once every 6 frames of the game, which would be a moderate-speed repeating animation. You can imagine what other crazy things you can do with frames based on this simple template...such as:

  • Animating attacks and stuff separately using npc.ai[] and just moving to the correct frames in order
  • Having monsters that look more beat up as their npc.life goes down by putting checks in here and having those frames set up in your png
  • Many crazy things

-Taien

Classes in Terraria

Chest | Cloud | Collision | Dust | Gore | Item | Lighting | Liquid | Main | NPC | Player | Projectile | Recipe | Sign | Star | WorldGen


angedD

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.