-
Notifications
You must be signed in to change notification settings - Fork 0
Chat
Rijam edited this page Mar 26, 2024
·
1 revision
We want our Town NPC to say things when we talk to them! For this, we override the GetChat() hook.
public override string GetChat() {
return "Hello world!";
}Now our Town NPC will say "Hello world!" when we speak to them! But, we want them to say other things, too. The best way to do this is to use a weighted random string.
public override string GetChat() {
WeightedRandom<string> chat = new();
chat.Add("Hi there!");
chat.Add("Hello! This message is slightly more common than the other message!", 1.5);
return chat;
}Now, the game will randomly pick one of those two message to display with the second message being 1.5 times more common.
An even better way to type the chat messages is to use localization. This way we can add support for other languages without needing to update our Town NPC's code.
public override string GetChat() {
WeightedRandom<string> chat = new();
// Here we have 4 standard messages that the Town NPC can say
chat.Add(Language.GetTextValue("Mods.TownNPCGuide.NPCs.TutorialTownNPC.Dialogue.StandardDialogue1"));
chat.Add(Language.GetTextValue("Mods.TownNPCGuide.NPCs.TutorialTownNPC.Dialogue.StandardDialogue2"));
chat.Add(Language.GetTextValue("Mods.TownNPCGuide.NPCs.TutorialTownNPC.Dialogue.StandardDialogue3"));
chat.Add(Language.GetTextValue("Mods.TownNPCGuide.NPCs.TutorialTownNPC.Dialogue.StandardDialogue4"));
// If the local player has a Terra Toilet in their inventory.
// We can use Main.LocalPlayer here because GetChat() runs client side. So, the only player who can see the chat is the one chatting with the Town NPC.
if (Main.LocalPlayer.HasItem(ItemID.TerraToilet)) {
// This message is 2 times as common.
chat.Add(Language.GetTextValue("Mods.TownNPCGuide.NPCs.TutorialTownNPC.Dialogue.PlayerHasTerraToilet"), 2);
}
// If the local player is located in the desert.
// As mentioned briefly before, only players know which biome they are in. Since the player is standing right next to the Town NPC to chat, checking the player is not a big deal.
if (Main.LocalPlayer.ZoneDesert) {
// This message is 2 times as rare.
chat.Add(Language.GetTextValue("Mods.TownNPCGuide.NPCs.TutorialTownNPC.Dialogue.DesertDialogue"), 0.5);
}
// We check that an Angler is present in the world. NPC.FindFirstNPC() returns the index of the NPC in Main.npc[]
int angler = NPC.FindFirstNPC(NPCID.Angler);
if (angler >= 0) { // If the Angler is not present, NPC.FindFirstNPC() will return -1.
// We've set up our localization key value pair in such a way that {0} will be replaced with the name of the Angler.
// You can use Main.npc[angler].FullName instead if you want it to say "Adam the Angler" instead of just "Adam".
chat.Add(Language.GetTextValue("Mods.TownNPCGuide.NPCs.TutorialTownNPC.Dialogue.AnglerDialogue", Main.npc[angler].GivenName));
}
// If Moon Lord has been defeated in this world.
if (NPC.downedMoonlord) {
// We've set this message up so that {0} will be replaced the name of the player and {1} will be replaced with the name of the world.
chat.Add(Language.GetTextValue("Mods.TownNPCGuide.NPCs.TutorialTownNPC.Dialogue.DownedMoonLord", Main.LocalPlayer.name, Main.worldName));
}
return chat;
}And in our localization file we have:
Mods: {
TownNPCGuide: {
NPCs: {
TutorialTownNPC: {
# Other keys have been omitted here for this example.
Dialogue: {
StandardDialogue1: Hello!
StandardDialogue2: Reading up on those tutorials I see!
StandardDialogue3: All this coding is confusing...
StandardDialogue4: Have you met my buddy Example Person?
PlayerHasTerraToilet: Ah, a throne fit for a king!
DesertDialogue: Do you have extra water? It sure is hot in the desert.
# {0} and {1} are place holders which will be replaced by other text.
AnglerDialogue: It seems like everyone has a different opinion about {0}.
DownedMoonLord: Congrats {0}! You've defeated Moon Lord in {1}!
ExamplePerson: "{0} is my good friend. If you have any questions about modding, they can sure help you!"
OnTeleportToStatue: Woah!
SecondButtonChat: Yo!
OnRescue: Hey! Thanks for saving me!
}
}
}
}
}Look at the localization guide for more information about localization.
Basic Guides
Intermediate Guides
Advanced Guides
- Preliminaries and What are Town NPCs?
- Spriting
- Create Your Town NPC's Class
- Spawn Condition
- Names
- Chat
- Buttons
- Shop
- Attacking
- Bestiary
- Happiness
- Profile
- Gore
- Misc
- Editing Vanilla NPCs with Global NPC