Surfpup's tConfig Mod Wiki
Advertisement
noframe

Under Construction
This page has been marked as under construction, which means information may change rapidly as it updated, or that the page is awaiting updates.


Difficulty: noframe
Time: 15-30 Minutes


Hero of Lumelia

There is an example NPC included in the Examples mod. This tutorial has been updated to make use of this example. The sprite used was made by Omnir.

This guide will show you how to create a Town NPC

Custom Town NPCs can have custom dialogue, shop items, spawn requirements, and a name.

Make Your Mod Folder[]

First, you must create a mod pack folder. All NPC-related files will go in a folder called "NPC" - see this page for more details about mod packs. You may also download the examples mod and use that as a reference.

Create the INI file[]

Decide upon the name of your NPC - in this example, it is called 'Hero of Lumelia' - as with everything else, the name of the INI file should be the name of your NPC, so it is 'Hero of Lumelia.ini'

Contents of our file:

[Stats]
frameCount=16
animationType=28
aiStyle=7
height=40
width=20
damage=0
defense=22
lifeMax=450
scale=1
soundHit=1
soundKilled=1
type=-1
knockBackResist=.3
townNPC=True
friendly=False


Some notes:

  • 'frameCount' is the number of frames that are in your .png sprite image.
  • 'aiStyle' affects how the NPC will behave
  • 'animationType' determines the internal code used to handle animations. Very often you'll want to make a sprite that is based off of an existing one, and use the same animation code that is already built into the game, instead of going through the process of making new animation code.
  • 'townNPC' must be set to True for the game to handle it like a Town NPC.
  • 'friendly' determines whether or not the NPC can hurt the player. In this case, I set friendly to False, and damage to zero, so that it doesn't hurt the player, and I can kill the NPC easily - for testing purposes of course. This is how bunnies work, I believe.
  • 'type' must be set to -1 if you want it to use a custom image. It affects many other things handled in the code as well.

::: NOTE :::

When changing names, remember all files need the same name.

E.g:

Villager1.ini

Villager1.png

Villager1.cs

::: NOTE END :::

Required Images[]

You need to have a sprite for the NPC (called "Hero of Lumelia.png"), as well as a sprite for the NPC's head (called "Hero of Lumelia Head.png"). The head image is required for the housing interface, and looks like this:

Hero of Lumelia Head

  • Note- you don't actually have to frame your sprite, just leave it like the banner you see above on the right.

Writing Methods For Chat, Shop Items, & More[]

This code goes in a file called: Hero of Lumelia.cs

TownSpawn()[]

The first thing we need to add is a method that will determine what conditions are needed for the NPC to spawn. Even if you have no requirements, this method is required for it to spawn at all. Here's what we have in our example:

public static bool TownSpawn() {
	if(Main.hardMode)
          return true;
	else 
          return false;
}

This NPC spawns if the world is in hard mode. If the method returns true, the NPC will spawn.

SetName()[]

Next, we'll add the SetName() method. It is highly recommended that you define this, unless you want your NPC to show up as "Hero of Lumelia the Hero of Lumelia"

public static string SetName() {
	if(Main.rand.Next(2)==0)
	  return "Arthur";
        else
	  return "Lancelot";
}

Simply return a string for the name of your NPC. The idea is that when your NPC dies, when a new one spawns, it has a new name. So we use a bit of randomness and choose from a set of names.

However, using the above code will only allow for a maximum of 2 names to be chosen.

public static string SetName() {

	int result = Main.rand.Next(4);
	string name = "";
	
	if (result == 0) 
        { 
          name = "InsertNameHere"; 
        }
	else if (result == 1) 
        { 
          name = "InsertNameHere1"; 
        }
	else if (result == 2) 
        { 
          name = "InsertNameHere2"; 
        }
	else if (result == 3)
        { 
          name = "InsertNameHere3"; 
        }

	return name;
	
}

Using this code we can set it so when an NPC spawns there is more than 2 names that the code can randomly pick from. As long as each name has a different result number and the Main.rand.Next (...) has the total amount of names in the brackets.

Chat()[]

Now we'll look at the Chat method.

public static string Chat() {
	if(Main.rand.Next(2)==0)
	  return "I eat Corruptors for breakfast.";
        else
	  return "The pixies around here have a serious temper.";
}

This just returns a string of dialogue text to display when the player clicks on the NPC. You can check various conditions to make the NPC say things at certain times of the day, or to make fun of how the player only has 6 hearts... There are a lot of things you could do. The simplest example involves just randomly choosing between a few lines of text.

Just like with the names, using the above line of code you can only have up to 2 lines of chat.

public static string Chat() {

	int result = Main.rand.Next(3);
	string text = "";

	if (result == 0) 
        { 
          text = "ChatLine"; 
        }
	else if (result == 1) 
        { 
          text = "ChatLine1"; 
        }
	else  if (result == 2)
        { 
          text = "ChatLine2"; 
        }
	
	return text;

}

But using a very similar code to the name part, we can now have more lines of code. All that is changed is where it said 'name' before they have now become 'text'. Using this you can have an NPC with a large and varied amount of chatlines!

SetupShop()[]

Finally, this is our example's code for setting shop items:

public static void SetupShop(Chest chest) {
	int index=0;

	chest.item[index].SetDefaults("Flame Beam");
	index++;
	chest.item[index].SetDefaults("Muramasa");
	index++;
	chest.item[index].SetDefaults("Red Hero's Shirt");
	index++;
	chest.item[index].SetDefaults("Red Hero's Pants");
	index++;
	chest.item[index].SetDefaults("Red Hero's Hat");
	index++;
}

It takes a Chest as a parameter, and all you need to do is add items to the chest. The SetDefaults() method sets the item to be what you want it to be.

However, say you want to add an excalibur that costs 15 silver (for some odd reason). You just have to change the value variable of the item: (Remember, PPGGSSCC)

chest.item[index].SetDefaults("Excalibur");
chest.item[index].value = 1500;
index++;

Wrapping Up[]

This tutorial barely scratches the surface of what is possible. I hope someone may take the time to add some more advanced concepts to this tutorial at some point.

Link: NPC - Examples

Advertisement