Skip to content

Buttons

Rijam edited this page Mar 26, 2024 · 1 revision

We've made it so our Town NPC has dialogue, but let's give them some buttons for us to click. For this, we override the SetChatButtons() hook.

public override void SetChatButtons(ref string button, ref string button2) {
	button = Language.GetTextValue("LegacyInterface.28"); // This will automatically be translated to say "Shop".
}

There, we go! Our Town NPC now has a "Shop" button. But, if you try to click on it, you'll notice that nothing happens. To give functionally to our buttons, we need to override the OnChatButtonClicked() hook.

public override void OnChatButtonClicked(bool firstButton, ref string shop) {
	if (firstButton) {
		// We set `shop` to a string that corresponds to our shop name.
		// See the shop section for more info.
		shop = ShopName;
	}
}

Now when we click the Shop button, the shop actually opens.

You can also set a second button here. Some Town NPCs like the Painter, Dye Trader, Tavernkeep, and Party Girl have a second chat button. You can use if statements to change what any of the buttons are, too. The Dryad's Status button changes to Purify when holding a Joja Cola, for example. Example Person has some more examples on setting the chat buttons.

public override void SetChatButtons(ref string button, ref string button2) {
	button = Language.GetTextValue("LegacyInterface.28"); // This will automatically be translated to say "Shop".
	if (Main.dayTime) { // The second button will only appear during the day time.
		button2 = Language.GetTextValue("Mods.TownNPCGuide.NPCs.TutorialTownNPC.UI.SecondButton");
	}
}

public override void OnChatButtonClicked(bool firstButton, ref string shop) {
	if (firstButton) {
		// We set `shop` to a string that corresponds to our shop name.
		// See the shop section for more info.
		shop = ShopName;
	}
	if (!firstButton) {
		// Do something else here.
	}
}


Clone this wiki locally