How to Create a Sword

In Terraria, a Sword is a very common item that exists, so this tutorial is going to cover how to create a sword. We will start with creating a basic sword and then modifying our sword to make it much better.

Requirements:

In order to follow this tutorial you will need the following items / knowledge.

  • Basic C#
  • C# Text Editor (Visual Studio, Notepad++)
  • tModLoader (Version 0.10 or Later)

Part 1: Creating the Sword

Step 1: Create the Weapon Class. You will want to place this in your items folder to be more organised. In this tutorial, I have created the class inside the Items/Weapons folder.

Step 2: Add the following “using” statements at the top of your document.

using Terraria;
using Terraria.ModLoader;
using Terraria.ID;

Step 3: Extend the Mod class with “ModItem” it is also recommended to make the class a public class.

public class CustomSword : ModItem {
 
}

Step 4: Override the “SetStaticDefaults” method (This is a public method)

public override void SetStaticDefaults() {
 
}

Step 5: In order to add a display name, you will want to add the
“DisplayName.SetDefault(“Item Name”);”
(Optional) If you wish to also add a Tooltip, you will want to add
“Tooltip.SetDefault(“Your Tooltip Here”);”

DisplayName.SetDefault("Custom Sword");
Tooltip.SetDefault("This is a custom sword!");

Our Code So Far:

using Terraria;
using Terraria.ModLoader;
using Terraria.ID;
 
namespace YourModName.Items.Weapons {
	public class CustomSword : ModItem {
		public override void SetStaticDefaults() {
			DisplayName.SetDefaults("Custom Swords");
			Tooltip.SetDefaults("This is a custom sword.");
		}
	}
}

Step 6: Next we want to override the SetDefaults method. (This is a public method)

public override void SetDefaults() {
 
}

Step 7: There are a lot of options that you can add in the “SetDefaults” section, but we are going to cover the ones that are required for a sword.

The first options you want to set are the defaults that are for most items. These are: Width, Height, Use Time, Use Animation, Use Style, Value and Rarity.

Width and Height are the hitbox for the item. These take an integer.
UseTime and UseAnimation are the time it takes before you can use the item again / the time it takes for the animation to start over. For the most part, it is better to keep these values the same. These take an integer.
UseStyle is the style of the item. There are a number of styles that you can use ranging from swinging, stabbing, shooting, drinking, etc. This takes an integer.
Value is how much the item is brought for. This value can be set out as an integer where each number is sorted into the value. For example 45641 will be 4 Gold, 56 Silver, 41 Copper. You can use Item.buyPrice(0, 4, 56, 51) to have it work the same way.
Rare is how rare the item is. This is mainly used to help work out when in the game it is received, you can use the following link for help: https://terraria.gamepedia.com/Rarity

These options will be set out like so:

item.width = 12;
item.height = 12;
item.useTime = 20;
item.useAnimation = 20;
item.buyPrice = Item.buyPrice(0, 0, 2, 0);
// item.buyPrice = Item.buyPrice(silver: 2); (Easier option)
item.rare = 3;

So, those are the basics for an item, but now we need to add the options for weapons as well as a bit more code that can be used for items.

Firstly we have Auto Reuse which is where if you hold down the mouse button then the item will continue to swing. This is a boolean, if this is true you just hold down the mouse button. If it is false you have to press the mouse button for each swing.
Next we have UseSound. This is the sound the item makes when it is used. There are a list you can get from the SoundID list.
In order to decide what type of weapon this is going to be we need to choose between Melee, Ranged, Thrown, Magic and a few other ones. For this we just want to set Melee to true.
To decide what the damage is going to be, we set an integer to represent the damage. This is the base value that is used and not what the damage will ALWAYS be.
We can also add knockback to the weapon so when an enemy is hit, they will be knocked back. This value is a float.

item.autoReuse = true;
item.UseSound = SoundID.Item1;
item.melee = true;
item.damage = 20;
item.knockBack = 2.5f;

With these set we have a basic weapon created. But let’s take a look at the code so far.

using Terraria;
using Terraria.ModLoader;
using Terraria.ID;
 
namespace YourModName.Items.Weapons {
	public class CustomSword : ModItem {
		public override void SetStaticDefaults() {
			DisplayName.SetDefaults("Custom Swords");
			Tooltip.SetDefaults("This is a custom sword.");
		}
 
		public override void SetDefaults() {
			item.width = 12;
			item.height = 12;
			item.useTime = 20;
			item.useAnimation = 20;
			item.buyPrice = Item.buyPrice(0, 0, 2, 0);
			// item.buyPrice = Item.buyPrice(silver: 2); (Easier option)
			item.rare = 3;
			item.autoReuse = true;
			item.UseSound = SoundID.Item1;
			item.melee = true;
			item.damage = 20;
			item.knockBack = 2.5f;
		}
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *