Skip to content
Rijam edited this page Jun 1, 2025 · 3 revisions

There are a few other things we can do with our Town NPC.

Teleport to King or Queen Statue

In vanilla, all Town NPCs can be teleported to a King or Queen Statue. We can easily add this for our Town NPC by overriding the CanGoToStatue() hook.

public override bool CanGoToStatue(bool toKingStatue) {
	// Can teleport to a King Statue
	return toKingStatue;
	// Return `!toKingStatue` for Queen Statues
	// Return `true` for both
}

Returning toKingStatue will allow our Town NPC to go to King Statues. If you want your Town NPC to go to Queen Statues instead, return !toKingStatue. If you want your Town NPC to go to both statues, return true.

If we wanted to, we can also add some special effects when our Town NPC is teleported by overriding OnGoToStatue().

public override void OnGoToStatue(bool toKingStatue) {
	// Display "Woah!" in chat. The message comes from our localization file.
	ChatHelper.BroadcastChatMessage(NetworkText.FromKey("Mods.TownNPCGuide.NPCs.TutorialTownNPC.Dialogue.OnTeleportToStatue"), Color.White);
}

In this example, "Woah!" will be displayed in chat when our Town NPC teleports. We need to use ChatHelper.BroadcastChatMessage() instead of Main.NewText() because OnGoToStatue() runs server side. For creating visual effects, look at Example Person.

Party Hat

By default, our Town NPC can wear party hats during parties. The UsesPartyHat() hook controls whether our Town NPC will wear a hat or not.

public override bool UsesPartyHat() {
	return true;
}

We could set it to false if we don't want our Town NPC to wear a party hat like the Zoologist or Tax Collector. We could also use if statements to change when our Town NPC can wear a hat. You don't need to include this hook if you want your Town NPC to wear a party hat during all parties.

If the Party Hat isn't sitting at the correct height, we can adjust it with a set, NPCID.Sets.HatOffsetY[Type] in SetStaticDefaults(). The measurement is in pixels and remember that negative Y values are up.

NPCID.Sets.HatOffsetY[Type] = 4; // Move the party hat down 4 pixels

If the Party Hat isn't sitting at the correct height between frames, we can also adjust that with a set, NPCID.Sets.NPCFramingGroup[Type] in SetStaticDefaults(). The number we assign to it is which group we want the hat to be animated as. What the framing groups do is move the party hat up or down based on the frames.

  • Group 0: By default, all Town NPCs are in Group 0. This group matches up with the standard walking animations (the same animations the player uses).
  • Group 1: Used by Nurse, Dryad, Party Girl, Steampunker, Mechanic, Stylist, Zoologist. These Town NPCs have only 12 walking frames.
  • Group 2: Used by Angler.
  • Group 3: Used by Truffle.
  • Group 4: Used by Town Cat.
  • Group 5: Used by Town Dog.
  • Group 6: Used by Town Bunny.
  • Group 7: Used by all Town Slimes.
  • Group 8: No offsets. Added by tModLoader.
Frame Group 0 Group 1 Group 2 Group 3 Group 4 Group 5 Group 6 Group 7 Group 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 -2
2 0 0 0 -2 0 0 -2 0
3 -2 -2 -2 0 0 0 -4 -2
4 -2 -2 -2 0 0 0 -4 -4
5 -2 -2 -2 0 0 0 -2 -6
6 0 0 0 0 0 0 0 -4
7 0 0 0 -2 0 0 -2 -2
8 0 0 -2 -2 0 -2 0 0
9 0 -2 -2 -2 0 -2 0 0
10 -2 -2 -2 0 2 -2 2 2
11 -2 -2 -2 0 2 0 4 2
12 -2 0 0 0 4 0 6 4
13 0 0 0 0 6 -2 4 2
14 0 0 0 -2 4 -2 2
15 0 0 0 -2 2 0 0
16 0 0 0 0 2 0 -2
17 0 0 0 0 -2 4 -4
18 0 0 0 0 -4 6 -6
19 0 0 0 0 -6 6 -6
20 0 0 0 0 -4 6 -6
21 0 0 0 0 -2 6 -6
22 0 0 0 -4 4 -6
23 0 0 0 -4 4 -6
24 0 0 0 -6 4 -6
25 0 -6 4 -4
26 -6 4 -2
27 -4 4

The measurement is in pixels and remember that negative Y values are up.

Here is a visual representation of all of the groups **Click to Expand**
The width and height of the frames here are the "standard" width and height. They may or may not line up correctly if you've increase the height of your frames. Note about Group 6: The Town Bunny has a `HatOffsetY` of 4, so it is moved down in game than what is shown here.

FramingGroup0 FramingGroup1 FramingGroup2 FramingGroup3 FramingGroup4 FramingGroup5 FramingGroup6 FramingGroup7

For even more control, ModNPC.PartyHatPosition(ref Vector2 position, ref SpriteEffects spriteEffects) or GlobalNPC.PartyHatPosition(NPC npc, ref Vector2 position, ref SpriteEffects spriteEffects) can be used to adjust the position of the party hat and direction it is drawn for each frame.

Here is an example of using ModNPC.PartyHatPosition **Click to Expand**
public override void PartyHatPosition(ref Vector2 position, ref SpriteEffects spriteEffects) {
	spriteEffects = npc.spriteDirection == -1 ? SpriteEffects.FlipHorizontally : SpriteEffects.None; // Flip the party hat.
	
	position.X += 4 * npc.spriteDirection; // Move the party hat right or left at all times.

	int frame = NPC.frame.Y / NPC.frame.Height; // Get the current frame of the NPC.
	switch (frame) {
		case 12:
			position.Y += 2; // Move the party hat down 2 pixels when on frame 12
			break;
		case 18: // Sitting frame
			// Don't adjust the position any more
			break;
		default:
			position.Y += 4 // If not frame 12 or 18, move the party hat down 4 pixels.
			break;
	}
}

Drop an item on death

If you want your Town NPC to drop an item on death, the standard ModifyNPCLoot() hook will work. Here is an example where an item only drops if the Town NPC has a certain name:

public override void ModifyNPCLoot(NPCLoot npcLoot) {
	// Our Town NPC will drop an item only if it is named a specific name.
	// See the Basic NPC Drops and Loot guide for more information about NPC loot.
	LeadingConditionRule specificName = new LeadingConditionRule(new Conditions.NamedNPC("blushiemagic"));
	specificName.OnSuccess(ItemDropRule.Common(ModContent.ItemType<TutorialItem>()));
	npcLoot.Add(specificName);
}

Census Support

Census - Town NPC Checklist is a popular mod that will tell the player how to get a Town NPC. Adding support for our Town NPC is extremely simple. All we have to do is add a line in our Localization file:

Mods: {
	TownNPCGuide: {
		NPCs: {
			TutorialTownNPC: {
				# Other keys have been omitted here for this example.
				
				# Here is where we write our Town NPC's spawn condition.
				Census.SpawnCondition: Rescue on the surface. Lives in the Hallow.
			}
		}
	}
}

That's it. As of 1.4.4, a Mod Call to Census is no longer required so adding support for Census is super easy.

In fact, you probably don't even need to create this key yourself. Enabling Census will automatically create a key for your Town NPC in your mod. You can then edit the value to say how to get the Town NPC.

Other

Setting Main.npcChatText to a string will replace whatever chat dialogue with the new string. In the Chat Buttons section earlier, we could change the chat dialogue when the player clicks the second button.

public override void OnChatButtonClicked(bool firstButton, ref bool shop) {
	if (firstButton) {
		shop = true;
	}
	if (!firstButton) {
		Main.npcChatText = Language.GetTextValue("Mods.TownNPCGuide.NPCs.TutorialTownNPC.Dialogue.SecondButtonChat");
	}
}

The CanChat() determines whether the Town NPC can be talked to. If NPC.townNPC is true, then this will be set to true by default. You could override this and return false if you do not want your Town NPC to be talked to.

NPC.homeless in SetDefaults() is a property that will make our Town NPC homeless by default. ModNPC.TownNPCStayingHomeless will make it so the Town NPC will not appear in the housing menu and will always be homeless.

NPCID.Sets.AllowDoorInteraction[Type] in SetStaticDefaults determines whether our Town NPC can open doors. By default they can, but we can set this to false to stop them from opening doors. This only works for Town NPCs with an aiStyle of 7 (NPCAIStyleID.Passive).

NPC.IsShimmerVariant is a getter that will tell you if the Town NPC is in its Shimmer variant. NPC.townNpcVariationIndex is also used to determine if the NPC is in its Shimmer varient. An index of 1 will always be "Shimmered", even if that variable is set manually and not by dunking the Town NPC in Shimmer.

NPCID.Sets.IsTownChild[] can be set to true to tell the game the Town NPC is a child like the Angler and Princess. Their death messages will say they left, and they will vanish in a puff of smoke.



Clone this wiki locally