tModLoader Modding Tutorial 2: Basic Item & Sword

This is Many

Retinazer
Introduction
Hi everyone! This tutorial will be about creating a simple item (material) and a sword.
Perhaps I'm exaggerating a little, starting with 2 objects at once, but for those who read the previous tutorial and, as advised there, learned C#, this won't be difficult.
And one very important anon - we will now make mod for 1.4.4, not for 1.4.3!!!
Let's go!

What are we going to study in this tutorial:
- How to create simple item
- How to create a sword that can poison enemy


How to create simple item
This item will be the material for all subsequent items that we will create.
First draw the sprite. The size of most materials in Terraria doesn't exceed 40x40px.
You can use any raster graphics editor, from Paint.net to Photoshop.
I'm using PikoPixel (only able on macOS) and Piskel.
So, create an item sprite. My will be 22x24px.
IMPORTANT: Default Terraria pixel is 2x2px in every graphics editor.
1689261580845.png

How it looks in game
1689261624995.png

Second, create a .cs file for your material code.
Put it here - ModSources/YourModName/Content/Items/Materials. It's optional but most of mods uses this path.
And then, put this code in it (// are explanations):
C#:
// What libraries we use in the code
using Terraria.ID;
using Terraria.ModLoader;
using Terraria.Localization;
using Terraria.GameContent.Creative;

namespace SteelMod.Content.Items.Materials // Where your code located
{
    public class SteelShard : ModItem // Your item name (SteelShard) and type (ModItem)
    {
        public override void SetStaticDefaults()
        {
            CreativeItemSacrificesCatalog.Instance.SacrificeCountNeededByItemId[Type] = 100; // How many items need for research in Journey Mode
        }

        public override void SetDefaults()
        {
            Item.width = 22; // Width of an item sprite
            Item.height = 24; // Height of an item sprite
            Item.maxStack = 9999; // How many items can be in one inventory slot
            Item.value = 100; // Item sell price in copper coins
            Item.rare = ItemRarityID.Blue; // The color of item's name in game. Check https://terraria.wiki.gg/wiki/Rarity
        }
    }
}
It isn't hard, is it?
Your first item is ready, congrats!

How to create a sword that can poison enemy
Sword is the simpliest weapon you can make, so we'll increase "risk and reward" - we will add poison effect to enemies we hit.
Also, we will emit poison themed dust when the sword is swing for better effect.
For the primary sword we need this code:
C#:
using SteelMod.Content.Items.Materials; // Using our Materials folder
using Terraria;
using Terraria.GameContent.Creative;
using Terraria.ID;
using Terraria.ModLoader;

namespace SteelMod.Content.Items.Weapons.Melee // Where is your code locates
{
    public class SteelSword : ModItem
    {
        public override void SetStaticDefaults()
        {
            CreativeItemSacrificesCatalog.Instance.SacrificeCountNeededByItemId[Type] = 1; // How many items need for research in Journey Mode
        }

        public override void SetDefaults()
        {
            // Visual properties
            Item.width = 40; // Width of an item sprite
            Item.height = 40; // Height of an item sprite
            Item.scale = 1f; // Multiplicator of item size, for example is you set this to 2f our sword will be biger twice. IMPORTANT: If you are using numbers with floating point, write "f" in their end, like 1.5f, 3.14f, 2.1278495f etc.
            Item.rare = ItemRarityID.Blue; // The color of item's name in game. Check https://terraria.wiki.gg/wiki/Rarity

            // Combat properties
            Item.damage = 50; // Item damage
            Item.DamageType = DamageClass.Melee; // What type of damage item is deals, Melee, Ranged, Magic, Summon, Generic (takes bonuses from all damage multipliers), Default (doesn't take bonuses from any damage multipliers)
            // useTime and useAnimation often use the same value, but we'll see examples where they don't use the same values
            Item.useTime = 20; // How long the swing lasts in ticks (60 ticks = 1 second)
            Item.useAnimation = 20; // How long the swing animation lasts in ticks (60 ticks = 1 second)
            Item.knockBack = 6f; // How far the sword punches enemies, 20 is maximal value
            Item.autoReuse = true; // Can the item auto swing by holding the attack button

            // Other properties
            Item.value = 10000; // Item sell price in copper coins
            Item.useStyle = ItemUseStyleID.Swing; // This is how you're holding the weapon, visit https://terraria.wiki.gg/wiki/Use_Style_IDs for list of possible use styles
            Item.UseSound = SoundID.Item1; // What sound is played when using the item, all sounds can be found here - https://terraria.wiki.gg/wiki/Sound_IDs
        }

        // Creating item craft
        public override void AddRecipes()
        {
            Recipe recipe = CreateRecipe();
            recipe.AddIngredient<SteelShard>(7); // We are using custom material for the craft, 7 Steel Shards
            recipe.AddIngredient(ItemID.Wood, 3); // Also, we are using vanilla material to craft, 3 Wood
            recipe.AddTile(TileID.Anvils); // Crafting station we need for craft, WorkBenches, Anvils etc. You can find them here - https://terraria.wiki.gg/wiki/Tile_IDs
            recipe.Register();
        }
    }
}
Oof, sweaty. But this is just a generic sword that swings and deals 50 melee damage.
1689265132611.png

Screenshot 2023-07-13 at 22.19.27.png


But we want to add some visuals (Dust) and combat advantages (Poison)
For Dust you need one more library:
C#:
using Microsoft.Xna.Framework; // Using another one library

And some additional code:
C#:
        public override void MeleeEffects(Player player, Rectangle hitbox)
        {
            if (Main.rand.NextBool(3)) // With 1/3 chance per tick (60 ticks = 1 second)...
            {
                // ...spawning dust
                Dust.NewDust(new Vector2(hitbox.X, hitbox.Y), // Position to spawn
                hitbox.Width, hitbox.Height, // Width and Height
                DustID.Poisoned, // Dust type. Check https://terraria.wiki.gg/wiki/Dust_IDs
                0, 0, // Speed X and Speed Y of dust, it have some randomization
                125); // Dust transparency, 0 - full visibility, 255 - full transparency

            }
        }
Also, we want to add poison effect to this sword, so we need this code:
C#:
// What is happening on hitting living entity
        public override void OnHitNPC(Player player, NPC target, NPC.HitInfo hit, int damageDone)
        {
            if (Main.rand.NextBool(4)) // 1/4 chance, or 25% in other words
            {
                target.AddBuff(BuffID.Poisoned, // Adding Poisoned to target
                    300); // for 5 seconds (60 ticks = 1 second)
            }
        }
Full code of poisoning sword looks like that:
C#:
using Microsoft.Xna.Framework; // Using another one library
using SteelMod.Content.Items.Materials; // Using our Materials folder
using Terraria;
using Terraria.GameContent.Creative;
using Terraria.ID;
using Terraria.ModLoader;

namespace SteelMod.Content.Items.Weapons.Melee // Where is your code locates
{
    public class PoisonedSteelSword : ModItem
    {
        public override void SetStaticDefaults()
        {
            CreativeItemSacrificesCatalog.Instance.SacrificeCountNeededByItemId[Type] = 1; // How many items need for research in Journey Mode
        }

        public override void SetDefaults()
        {
            // Visual properties
            Item.width = 40; // Width of an item sprite
            Item.height = 40; // Height of an item sprite
            Item.scale = 1f; // Multiplicator of item size, for example is you set this to 2f our sword will be biger twice. IMPORTANT: If you are using numbers with floating point, write "f" in their end, like 1.5f, 3.14f, 2.1278495f etc.
            Item.rare = ItemRarityID.Blue; // The color of item's name in game. See https://terraria.wiki.gg/wiki/Rarity

            // Combat properties
            Item.damage = 50; // Item damage
            Item.DamageType = DamageClass.Melee; // What type of damage item is deals, Melee, Ranged, Magic, Summon, Generic (takes bonuses from all damage multipliers), Default (doesn't take bonuses from any damage multipliers)
            // useTime and useAnimation often use the same value, but we'll see examples where they don't use the same values
            Item.useTime = 20; // How long the swing lasts in ticks (60 ticks = 1 second)
            Item.useAnimation = 20; // How long the swing animation lasts in ticks (60 ticks = 1 second)
            Item.knockBack = 6f; // How far the sword punches enemies, 20 is maximal value
            Item.autoReuse = true; // Can the item auto swing by holding the attack button

            // Other properties
            Item.value = 10000; // Item sell price in copper coins
            Item.useStyle = ItemUseStyleID.Swing; // This is how you're holding the weapon, visit https://terraria.wiki.gg/wiki/Use_Style_IDs for list of possible use styles
            Item.UseSound = SoundID.Item1; // What sound is played when using the item, all sounds can be found here - https://terraria.wiki.gg/wiki/Sound_IDs
        }

        public override void MeleeEffects(Player player, Rectangle hitbox)
        {
            if (Main.rand.NextBool(3)) // With 1/3 chance per tick (60 ticks = 1 second)...
            {
                // ...spawning dust
                Dust.NewDust(new Vector2(hitbox.X, hitbox.Y), // Position to spawn
                hitbox.Width, hitbox.Height, // Width and Height
                DustID.Poisoned, // Dust type. Check https://terraria.wiki.gg/wiki/Dust_IDs
                0, 0, // Speed X and Speed Y of dust, it have some randomization
                125); // Dust transparency, 0 - full visibility, 255 - full transparency

            }
        }

        // What is happening on hitting living entity
        public override void OnHitNPC(Player player, NPC target, NPC.HitInfo hit, int damageDone)
        {
            if (Main.rand.NextBool(4)) // 1/4 chance, or 25% in other words
            {
                target.AddBuff(BuffID.Poisoned, // Adding Poisoned to target
                    300); // for 5 seconds (60 ticks = 1 second)
            }
        }

        // Creating item craft
        public override void AddRecipes()
        {
            Recipe recipe = CreateRecipe();
            recipe.AddIngredient<SteelShard>(7); // We are using custom material for the craft, 7 Steel Shards
            recipe.AddIngredient(ItemID.Wood, 3); // Also, we are using vanilla material to craft, 3 Wood
            recipe.AddIngredient(ItemID.JungleSpores, 5); // I've added some Jungle Spores to craft
            recipe.AddTile(TileID.Anvils); // Crafting station we need for craft, WorkBenches, Anvils etc. You can find them here - https://terraria.wiki.gg/wiki/Tile_IDs
            recipe.Register();
        }
    }
}

Yeah, finally, that's done!
Screenshot 2023-07-13 at 22.41.34.png


Ending
Thanks to everyone who read this tutorial! I hope you learned something new. Also, sorry for so long tutorial waiting.
Any questions are allowed! See you on the next tutorial!


Previous Tutorial / Next Tutorial
 
Last edited:
"Velocity X and Velocity Y of the dust, I set to 0 to prevent dust from moving"
It is moving because NewDust and NewDustDirect methods slightly randomize dust stats
 
Last edited:
Hi. I want my blood item to drop out of zombies. And also I want to use items from my mod to create other items. How to do it all? Do I need to use other libraries for recipes? If so, which ones?
 
I want my blood item to drop out of zombies.
C#:
using Terraria.GameContent.ItemDropRules;
using Terraria.ModLoader;
using Terraria.ID;
using Terraria;
using Terraria.DataStructures;

namespace YourModName
{
    public class Loot : GlobalNPC
    {
        public override void ModifyNPCLoot(NPC npc, NPCLoot npcLoot)
        {
            if (npc.type == NPCID.Zombie) { // From what mob it drops
            npcLoot.Add(ItemDropRule.Common (
            ModContent.ItemType<Blood>(), // What it drops
            4, // Chance denominator (1/4, or 25%)
            1, // Minimum dropped
            3 // Maximum dropped
            ));
            }
        }
    }
}

And also I want to use items from my mod to create other items.
Not got, what do you mean?

Sorry for late response
 
I'm trying to build the mod in tModLoader with the code you provided but I'm currently getting this error:

C#:
The type or namespace name 'Recipe' could not be found (are you missing a using directive or an assembly reference?)
 
I'm trying to build the mod in tModLoader with the code you provided but I'm currently getting this error:

C#:
The type or namespace name 'Recipe' could not be found (are you missing a using directive or an assembly reference?)
do you have line using Terraria;?
 
Introduction
Hi everyone! This tutorial will be about creating a simple item (material) and a sword.
Perhaps I'm exaggerating a little, starting with 2 objects at once, but for those who read the previous tutorial and, as advised there, learned C#, this won't be difficult.
And one very important anon - we will now make mod for 1.4.4, not for 1.4.3!!!
Let's go!

What are we going to study in this tutorial:
- How to create simple item
- How to create a sword that can poison enemy


How to create simple item
This item will be the material for all subsequent items that we will create.
First draw the sprite. The size of most materials in Terraria doesn't exceed 40x40px.
You can use any raster graphics editor, from Paint.net to Photoshop.
I'm using PikoPixel (only able on macOS) and Piskel.
So, create an item sprite. My will be 22x24px.
IMPORTANT: Default Terraria pixel is 2x2px in every graphics editor.
View attachment 414236
How it looks in game
View attachment 414237
Second, create a .cs file for your material code.
Put it here - ModSources/YourModName/Content/Items/Materials. It's optional but most of mods uses this way.
And then, put this code in it (// are explanations):​
C#:
// What libraries do we use in the code
using Terraria.ID;
using Terraria.ModLoader;
using Terraria.Localization;
using Terraria.GameContent.Creative;

namespace SteelMod.Content.Items.Materials // Where is your code locates
{
    public class SteelShard : ModItem // Your item name (SteelShard) and type (ModItem)
    {
        public override void SetStaticDefaults()
        {
            CreativeItemSacrificesCatalog.Instance.SacrificeCountNeededByItemId[Type] = 100; // How many items need for research in Journey Mode
        }

        public override void SetDefaults()
        {
            Item.width = 22; // Width of an item sprite
            Item.height = 24; // Height of an item sprite
            Item.maxStack = 9999; // How many items can be in one inventory slot
            Item.value = 100; // Item sell price in copper coins
            Item.rare = ItemRarityID.Blue; // The color of item's name in game. See https://terraria.wiki.gg/wiki/Rarity
        }
    }
}
It isn't hard, is it?
Your first item is ready, congrats!

How to create a sword that can poison enemy
Sword is the simpliest weapon you can make, so we'll increase "risk and reward" - we will add poison effect to enemies we hit.
Also, we will emit poison themed dust when the sword is swing for better effect.
For the primary sword we need this code:
C#:
using SteelMod.Content.Items.Materials; // Using our Materials folder
using Terraria;
using Terraria.GameContent.Creative;
using Terraria.ID;
using Terraria.ModLoader;

namespace SteelMod.Content.Items.Weapons.Melee // Where is your code locates
{
    public class SteelSword : ModItem
    {
        public override void SetStaticDefaults()
        {
            CreativeItemSacrificesCatalog.Instance.SacrificeCountNeededByItemId[Type] = 1; // How many items need for research in Journey Mode
        }

        public override void SetDefaults()
        {
            // Visual properties
            Item.width = 40; // Width of an item sprite
            Item.height = 40; // Height of an item sprite
            Item.scale = 1f; // Multiplicator of item size, for example is you set this to 2f our sword will be biger twice. IMPORTANT: If you are using numbers with floating point, write "f" in their end, like 1.5f, 3.14f, 2.1278495f etc.
            Item.useStyle = ItemUseStyleID.Swing; // This is how you're holding the weapon, visit https://terraria.wiki.gg/wiki/Use_Style_IDs for list of possible use styles
            Item.rare = ItemRarityID.Blue; // The color of item's name in game. See https://terraria.wiki.gg/wiki/Rarity

            // Combat properties
            Item.damage = 50; // Item damage
            Item.DamageType = DamageClass.Melee; // What type of damage item is deals, Melee, Ranged, Magic, Summon, Generic (takes bonuses from all damage multipliers), Default (doesn't take bonuses from any damage multipliers)
            // useTime and useAnimation often use the same value, but we'll see examples where they don't use the same values
            Item.useTime = 20; // How long the swing lasts in ticks (60 ticks = 1 second)
            Item.useAnimation = 20; // How long the swing animation lasts in ticks (60 ticks = 1 second)
            Item.knockBack = 6f; // How far the sword punches enemies, 20 is maximal value
            Item.autoReuse = true; // Can the item auto swing by holding the attack button

            // Other properties
            Item.value = 10000; // Item sell price in copper coins
            Item.useStyle = ItemUseStyleID.Swing; // This is how you're holding the weapon, visit https://terraria.wiki.gg/wiki/Use_Style_IDs for list of possible use styles
            Item.UseSound = SoundID.Item1; // What sound is played when using the item, all sounds can be found here - https://terraria.wiki.gg/wiki/Sound_IDs
        }

        // Creating item craft
        public override void AddRecipes()
        {
            Recipe recipe = CreateRecipe();
            recipe.AddIngredient<SteelShard>(7); // We are using custom material for the craft, 7 Steel Shards
            recipe.AddIngredient(ItemID.Wood, 3); // Also, we are using vanilla material to craft, 3 Wood
            recipe.AddTile(TileID.Anvils); // Crafting station we need for craft, WorkBenches, Anvils etc. You can find them here - https://terraria.wiki.gg/wiki/Tile_IDs
            recipe.Register();
        }
    }
}
Oof, sweaty. But this is just a generic sword that swings and deals 50 melee damage.
View attachment 414260
View attachment 414261

But we want to add some visuals (Dust) and combat advantages (Poison)
For Dust you need a new library:​
C#:
using Microsoft.Xna.Framework; // Using another one library

And some additional code:​
C#:
        public override void MeleeEffects(Player player, Rectangle hitbox)
        {
            if (Main.rand.NextBool(3)) // With 1/3 chance per tick (60 ticks = 1 second)...
            {
                // ...spawning dust
                Dust.NewDust(new Vector2(hitbox.X, hitbox.Y), // Position to spawn
                hitbox.Width, hitbox.Height, // Width and Height
                DustID.Poisoned, // Dust type. Check https://terraria.wiki.gg/wiki/Dust_IDs
                0, 0, // Velocity X and Velocity Y of the dust, I set to 0 to prevent dust from moving
                75); // Dust transparency, 0 - full visibility, 255 - full transparency

            }
        }
Also, we want to add poison effect to this sword, so we need this code:​
C#:
// What is happening on hitting living entity
        public override void OnHitNPC(Player player, NPC target, NPC.HitInfo hit, int damageDone)
        {
            if (Main.rand.NextBool(4)) // 1/4 chance, or 25% in other words
            {
                target.AddBuff(BuffID.Poisoned, // Adding Poisoned to target
                    300); // for 5 seconds (60 ticks = 1 second)
            }
        }
Full code of poisoning sword looks like that:​
C#:
using Microsoft.Xna.Framework; // Using another one library
using SteelMod.Content.Items.Materials; // Using our Materials folder
using Terraria;
using Terraria.GameContent.Creative;
using Terraria.ID;
using Terraria.ModLoader;

namespace SteelMod.Content.Items.Weapons.Melee // Where is your code locates
{
    public class PoisonedSteelSword : ModItem
    {
        public override void SetStaticDefaults()
        {
            CreativeItemSacrificesCatalog.Instance.SacrificeCountNeededByItemId[Type] = 1; // How many items need for research in Journey Mode
        }

        public override void SetDefaults()
        {
            // Visual properties
            Item.width = 40; // Width of an item sprite
            Item.height = 40; // Height of an item sprite
            Item.scale = 1f; // Multiplicator of item size, for example is you set this to 2f our sword will be biger twice. IMPORTANT: If you are using numbers with floating point, write "f" in their end, like 1.5f, 3.14f, 2.1278495f etc.
            Item.useStyle = ItemUseStyleID.Swing; // This is how you're holding the weapon, visit https://terraria.wiki.gg/wiki/Use_Style_IDs for list of possible use styles
            Item.rare = ItemRarityID.Blue; // The color of item's name in game. See https://terraria.wiki.gg/wiki/Rarity

            // Combat properties
            Item.damage = 50; // Item damage
            Item.DamageType = DamageClass.Melee; // What type of damage item is deals, Melee, Ranged, Magic, Summon, Generic (takes bonuses from all damage multipliers), Default (doesn't take bonuses from any damage multipliers)
            // useTime and useAnimation often use the same value, but we'll see examples where they don't use the same values
            Item.useTime = 20; // How long the swing lasts in ticks (60 ticks = 1 second)
            Item.useAnimation = 20; // How long the swing animation lasts in ticks (60 ticks = 1 second)
            Item.knockBack = 6f; // How far the sword punches enemies, 20 is maximal value
            Item.autoReuse = true; // Can the item auto swing by holding the attack button

            // Other properties
            Item.value = 10000; // Item sell price in copper coins
            Item.useStyle = ItemUseStyleID.Swing; // This is how you're holding the weapon, visit https://terraria.wiki.gg/wiki/Use_Style_IDs for list of possible use styles
            Item.UseSound = SoundID.Item1; // What sound is played when using the item, all sounds can be found here - https://terraria.wiki.gg/wiki/Sound_IDs
        }

        public override void MeleeEffects(Player player, Rectangle hitbox)
        {
            if (Main.rand.NextBool(3)) // With 1/3 chance per tick (60 ticks = 1 second)...
            {
                // ...spawning dust
                Dust.NewDust(new Vector2(hitbox.X, hitbox.Y), // Position to spawn
                hitbox.Width, hitbox.Height, // Width and Height
                DustID.Poisoned, // Dust type. Check https://terraria.wiki.gg/wiki/Dust_IDs
                0, 0, // Velocity X and Velocity Y of the dust, I set to 0 to prevent dust from moving
                75); // Dust transparency, 0 - full visibility, 255 - full transparency

            }
        }

        // What is happening on hitting living entity
        public override void OnHitNPC(Player player, NPC target, NPC.HitInfo hit, int damageDone)
        {
            if (Main.rand.NextBool(4)) // 1/4 chance, or 25% in other words
            {
                target.AddBuff(BuffID.Poisoned, // Adding Poisoned to target
                    300); // for 5 seconds (60 ticks = 1 second)
            }
        }

        // Creating item craft
        public override void AddRecipes()
        {
            Recipe recipe = CreateRecipe();
            recipe.AddIngredient<SteelShard>(7); // We are using custom material for the craft, 7 Steel Shards
            recipe.AddIngredient(ItemID.Wood, 3); // Also, we are using vanilla material to craft, 3 Wood
            recipe.AddIngredient(ItemID.JungleSpores, 5); // I've added some Jungle Spores to craft
            recipe.AddTile(TileID.Anvils); // Crafting station we need for craft, WorkBenches, Anvils etc. You can find them here - https://terraria.wiki.gg/wiki/Tile_IDs
            recipe.Register();
        }
    }
}

Yeah, finally, that's done!
View attachment 414266

Ending
Thanks to everyone who read this tutorial! I hope you learned something new. Also, sorry for so long tutorial waiting.
Any questions are allowed! See you on the next tutorial!


Previous Tutorial / Next Tutorial (coming soon!)
Yooo is there a mobil android download for tmodloader
 
Yooo is there a mobil android download for tmodloader
No, only thing remotely close to it is tlpro, which is payed and only really has audio and textures. due to androids restrictions, its hard for said mods to be implemented, that being said, there is no yes or no if we get a tmodloader for mobile, bt ost likly no until android lightens their restrictions.
 
I did not. Adding that fixed it thanks! Also is there a modding community Discord that can answer C# questions? I may have a few more questions in the future and I don't want to flood this thread.
yeah, tModLoader have discord, but you also can DM me in discord if you have questions (many_is_always_good)
 
I seem to have done something wrong... in the C# file for my item, it has 23 errors, 12 of which are this: Predefined type 'System.Int32' is not defined or imported (CS0518)
 
I don't exactly know how to make a .CS fine sense I'm on windows 10, also do I need to use something like a website to make the .CS file?
 
I don't exactly know how to make a .CS fine sense I'm on windows 10, also do I need to use something like a website to make the .CS file?
I am just copying files from the first one, so all my mod is grown from one file. also idk how to work on windows 10 cuz I am on mac
 
Back
Top Bottom