tModLoader How to add a second shop to an npc with custom currency?

JoeStorm EL

Skeletron Prime
I wanted to make a new shop in my mod. However, I want it to be a secondary shop where you need a certain custom item in order to purchase it. Something like the Tavernkeeper does. I've looked around how, but can't seem to find anything.
If you have anything to help, thanks in advance.
 
Well, adding a custom currency is relatively straight forward. The relevant bits from the example mod are here:
https://github.com/blushiemagic/tMo...722d14/ExampleMod/ExampleCustomCurrency.cs#L8
https://github.com/blushiemagic/tMo...d7322f311b722d14/ExampleMod/ExampleMod.cs#L73
https://github.com/blushiemagic/tMo...2d14/ExampleMod/NPCs/ExampleGlobalNPC.cs#L146

I've also found a tutorial here:

Now, by "second shop" do you mean you actually want your NPC to have two separate shop inventories? That might be a bit tricky, but I think it should be doable.
 
If your are making a new town NPC, you can do something like this:
Code:
public class ExamplePerson : ModNPC
{
    private bool otherShop;
    
    //... Other town NPC related things ...

    public override void SetChatButtons(ref string button, ref string button2)
    {
        button = Language.GetTextValue("LegacyInterface.28");
        button2 = "Other " + Language.GetTextValue("LegacyInterface.28");
    }
   
    public override void OnChatButtonClicked(bool firstButton, ref bool shop)
    {
        shop = true;
        ((ExamplePerson)mod.GetNPC(Name)).otherShop = !firstButton;
    }
  
    public override void SetupShop(Chest shop, ref int nextSlot)
    {
        if (otherShop)
        {
            //Add items for "Other Shop"
            shop.item[nextSlot++].SetDefaults(ItemID.DirtBlock);
        }
        else
        {
            //Add items for "Shop"
            shop.item[nextSlot++].SetDefaults(ItemID.StoneBlock);
        }
    }
}
I think that should work fine. If some other mod decides to add items to your shop with a GlobalNPC then weird things could happen, but that is not very likely.
A similar approach could be used to add a new shop to an existing vanilla NPC, but that would involve some IL injection as well, because GlobalNPC doesn't have the necessary hooks.
 
I wanted to make a new shop in my mod. However, I want it to be a secondary shop where you need a certain custom item in order to purchase it. Something like the Tavernkeeper does. I've looked around how, but can't seem to find anything.
If you have anything to help, thanks in advance.

Look for an open source mod that has a double shopped NPC.
Then use that as a template, but don't steal their code.
 
Well i'm not sure if I do
Most the ones I know are hiden
So do you know any other closed source mod that has this double shop setup? I haven't come across this idea before, but I'd be interested to see how others have implemented it.
 
Alchemist NPCs (and Alchemist NPCs lite) have that, right? I'm also curious how that looks like though code-wise...
 
Alchemist NPCs (and Alchemist NPCs lite) have that, right? I'm also curious how that looks like though code-wise...
Yeah, it looks like Alchemist NPCs does the same thing I suggested above, just with a static bool field to store which shop to use. The NPCs with the shop selector UI basically just use the second button to make a UIState visible, and then the UI changes the bool settings in the NPC class for the shop type, then sets everything up in Main to display the shop and calls SetupShop. Pretty straight forward.
 
Back
Top Bottom