tModLoader I need help coding an accessory item.

twindtrout9783

Steampunker
I'm trying to make an equipment item that (if possible) doubles the player's current amount of health. I'll add in extra items later that are used to craft it. (It'll be an after-MoonLord item, to keep things balanced.)
I have edited very little code so far, as this is my first time making a mod. My goal is to first make it an equitable item [that goes in the accessory slot], instead of a weapon (which is the default.) Here is my code so far:
C#:
using Terraria.ID;
using Terraria.ModLoader;

namespace playerboosteritems.Items
{
    public class heartofdivinity : ModItem
    {
        public override void SetStaticDefaults()
        {
            DisplayName.SetDefault("Heart of Divinity"); // By default, capitalization in classnames will add spaces to the display name. You can customize the display name here by uncommenting this line.
            Tooltip.SetDefault("Gives you a divine amount of health");
        }

        public override void SetDefaults()
        {
            item.damage = 50;
            item.melee = true;
            item.width = 40;
            item.height = 40;
            item.useTime = 20;
            item.useAnimation = 20;
            item.useStyle = 1;
            item.knockBack = 6;
            item.value = 10000;
            item.rare = 2;
            item.UseSound = SoundID.Item1;
            item.autoReuse = true;
        }

        public override void AddRecipes()
        {
            ModRecipe recipe = new ModRecipe(mod);
            recipe.AddIngredient(ItemID.ManaCrystal, 50);
            recipe.AddTile(TileID.WorkBenches);
            recipe.SetResult(this);
            recipe.AddRecipe();
        }
    }
}
 
I'm trying to make an equipment item that (if possible) doubles the player's current amount of health. I'll add in extra items later that are used to craft it. (It'll be an after-MoonLord item, to keep things balanced.)
I have edited very little code so far, as this is my first time making a mod. My goal is to first make it an equitable item [that goes in the accessory slot], instead of a weapon (which is the default.) Here is my code so far:
C#:
using Terraria.ID;
using Terraria.ModLoader;

namespace playerboosteritems.Items
{
    public class heartofdivinity : ModItem
    {
        public override void SetStaticDefaults()
        {
            DisplayName.SetDefault("Heart of Divinity"); // By default, capitalization in classnames will add spaces to the display name. You can customize the display name here by uncommenting this line.
            Tooltip.SetDefault("Gives you a divine amount of health");
        }

        public override void SetDefaults()
        {
            item.damage = 50;
            item.melee = true;
            item.width = 40;
            item.height = 40;
            item.useTime = 20;
            item.useAnimation = 20;
            item.useStyle = 1;
            item.knockBack = 6;
            item.value = 10000;
            item.rare = 2;
            item.UseSound = SoundID.Item1;
            item.autoReuse = true;
        }

        public override void AddRecipes()
        {
            ModRecipe recipe = new ModRecipe(mod);
            recipe.AddIngredient(ItemID.ManaCrystal, 50);
            recipe.AddTile(TileID.WorkBenches);
            recipe.SetResult(this);
            recipe.AddRecipe();
        }
    }
}
A accessory and a Weapon are rather close, but also a bit different. You can use this code as a example of what to do:
C#:
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;
// if you need content from in your mod, such as items for a crafting recipe, use: using static Terraria.ModLoader.ModContent;

namespace ModName.Items
{
    class Accessory : ModItem
    {
        public override void SetStaticDefaults()
        {
            DisplayName.SetDefault("Accessory");
            Tooltip.SetDefault("I'm a accessory!");
        }

        public override void SetDefaults()
        {
            // These are the only required parameters for a accessory
            item.accessory = true; // Makes this item a worn accessory. IMPORTIANT!
            item.width = 40;
            item.height = 40;
            item.value = Item.sellPrice(0, 0, 0, 0); // The sell price. In (Platnium, Gold, Silver, Copper).
            // you can also set item.value like so: item.value = 10000;
            item.rare = 0; // The rarity of the item.
        }

        public override void UpdateAccessory(Player player, bool hideVisual) // This is used to apply effects to the player when the accessory is put on and off
        {
        }

        public override void AddRecipes()
        {
            ModRecipe recipe = new ModRecipe(mod);
            recipe.AddIngredient(ItemID.ManaCrystal, 50);
            recipe.AddTile(TileID.WorkBenches);
            recipe.SetResult(this);
            recipe.AddRecipe();
        }
    }
}
as for doubling the player health, in the UpdateAccessory() function, this should work
C#:
player.statLifeMax2 += player.statLifeMax * 2
 
A accessory and a Weapon are rather close, but also a bit different. You can use this code as a example of what to do:
C#:
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;
// if you need content from in your mod, such as items for a crafting recipe, use: using static Terraria.ModLoader.ModContent;

namespace ModName.Items
{
    class Accessory : ModItem
    {
        public override void SetStaticDefaults()
        {
            DisplayName.SetDefault("Accessory");
            Tooltip.SetDefault("I'm a accessory!");
        }

        public override void SetDefaults()
        {
            // These are the only required parameters for a accessory
            item.accessory = true; // Makes this item a worn accessory. IMPORTIANT!
            item.width = 40;
            item.height = 40;
            item.value = Item.sellPrice(0, 0, 0, 0); // The sell price. In (Platnium, Gold, Silver, Copper).
            // you can also set item.value like so: item.value = 10000;
            item.rare = 0; // The rarity of the item.
        }

        public override void UpdateAccessory(Player player, bool hideVisual) // This is used to apply effects to the player when the accessory is put on and off
        {
        }

        public override void AddRecipes()
        {
            ModRecipe recipe = new ModRecipe(mod);
            recipe.AddIngredient(ItemID.ManaCrystal, 50);
            recipe.AddTile(TileID.WorkBenches);
            recipe.SetResult(this);
            recipe.AddRecipe();
        }
    }
}
as for doubling the player health, in the UpdateAccessory() function, this should work
C#:
player.statLifeMax2 += player.statLifeMax * 2
How would I apply that? Would the code be something like this: (How would I prevent my item from being sold by an npc?)

C#:
 public override void UpdateAccessory(Player player.statLifeMax2 += player.statLifeMax * 2
, bool hideVisual)

Also, how so I apply a texture to my item?

EDIT: And how do I create a material item? (One that will be used for crafting my accessory item.) I want this material item to drop from Moon Lord
 
Last edited:
How would I apply that? Would the code be something like this: (How would I prevent my item from being sold by an npc?)

C#:
 public override void UpdateAccessory(Player player.statLifeMax2 += player.statLifeMax * 2
, bool hideVisual)

Also, how so I apply a texture to my item?

EDIT: And how do I create a material item? (One that will be used for crafting my accessory item.) I want this material item to drop from Moon Lord
You would apply it like so:
C#:
public override void UpdateAccessory(Player player, bool hideVisual)
{
    player.statLifeMax2 += player.statLifeMax * 2;
}
Textures are applied automatically as long as there is a image in the same folder with the same name as the class of the item.
For example, if your item class was called "NewItem", the image would need to be named "NewItem" (see Basic Autoloading on the tmodloader wiki for more info).

To make a material item, you make a new script and derive the class from ModItem (much like how you would a weapon or accessory), but all you need to define it is:
C#:
public override void SetStaticDefaults()
{
    DisplayName.SetDefault("Material Item");
    Tooltip.SetDefault("I'm a material!");
}

public override void SetDefaults()
{
    item.width = 20; // X size of the Item.
    item.height = 20; // Y size of the Item.
    
    item.value = Item.sellPrice(0, 0, 0, 10); // The sell price. In (Platnium, Gold, Silver, Copper).
    // Can also set value like so: item.value = 10000;
    item.rare = 0; // Rarity of the item

    item.maxStack = 999; // Maximun stack size.
}
This item has functionally no use other then to be use for crafting. To add it to a recipe, use this in any addRecipe function:
C#:
recipe.addIngredient(ItemType<>(Namespace.Of.Item.Name));
// Be sure to add 'using static Terraria.ModLoader.ModContent' at the top of the script!

And finally, to make it drop from any vanilla mob, create one last script and make it derive from the GlobalNPC type, then you can make any NPC drop it.
Here's a example of that:
C#:
class MyGlobalNPC : GlobalNPC
{
    public override void NPCLoot(NPC npc)
    {
        if(npc.type == NPCID.MoonLordCore) // Use MoonLordCore for drops, since it's killed last
        {
            item.NewItem(npc.getRect(), ItemType<MaterialItem>(), 5);
        }
        // Addtional if statements here if you would like to add drops to other vanilla npc.
    }
}
 
You would apply it like so:
C#:
public override void UpdateAccessory(Player player, bool hideVisual)
{
    player.statLifeMax2 += player.statLifeMax * 2;
}
Textures are applied automatically as long as there is a image in the same folder with the same name as the class of the item.
For example, if your item class was called "NewItem", the image would need to be named "NewItem" (see Basic Autoloading on the tmodloader wiki for more info).

To make a material item, you make a new script and derive the class from ModItem (much like how you would a weapon or accessory), but all you need to define it is:
C#:
public override void SetStaticDefaults()
{
    DisplayName.SetDefault("Material Item");
    Tooltip.SetDefault("I'm a material!");
}

public override void SetDefaults()
{
    item.width = 20; // X size of the Item.
    item.height = 20; // Y size of the Item.

    item.value = Item.sellPrice(0, 0, 0, 10); // The sell price. In (Platnium, Gold, Silver, Copper).
    // Can also set value like so: item.value = 10000;
    item.rare = 0; // Rarity of the item

    item.maxStack = 999; // Maximun stack size.
}
This item has functionally no use other then to be use for crafting. To add it to a recipe, use this in any addRecipe function:
C#:
recipe.addIngredient(ItemType<>(Namespace.Of.Item.Name));
// Be sure to add 'using static Terraria.ModLoader.ModContent' at the top of the script!

And finally, to make it drop from any vanilla mob, create one last script and make it derive from the GlobalNPC type, then you can make any NPC drop it.
Here's a example of that:
C#:
class MyGlobalNPC : GlobalNPC
{
    public override void NPCLoot(NPC npc)
    {
        if(npc.type == NPCID.MoonLordCore) // Use MoonLordCore for drops, since it's killed last
        {
            item.NewItem(npc.getRect(), ItemType<MaterialItem>(), 5);
        }
        // Addtional if statements here if you would like to add drops to other vanilla npc.
    }
}
Thank you for all of your assistance. When you say item class, do you mean the name of it in the code? (For example, the item I'm trying to make is called "Heart of Divinity," but it's class name is heartofdivinity, so would i name the image file "heartofdivinity?")
Also, how would I go about animating an item's texture?
 
Last edited:
Thank you for all of your assistance. When you say item class, do you mean the name of it in the code? (For example, the item I'm trying to make is called "Heart of Divinity," but it's class name is heartofdivinity, so would i name the image file "heartofdivinity?")
Also, how would I go about animating an item's texture?
Use the class name. So in this case, heartofdivinity.

To make a animated sprite, add this to your SetStaticDefault of the item
Main.RegisterItemAnimation(item.type, new DrawAnimationVertical(speed, frames));
Where speed is how many ticks each frame is (60 ticks = 1 second) and frames is how many animation frames it has.

On the spriting side, for every frame you want, you add it to the sprite image.
I'll use this as a example:
AnimationGuide.png

The animation starts from the top and goes down a "frame" (your item's height parameter) every certian amount of ticks. So in this example, the items would go from red to yellow to green to blue, then loop back around. The animation HAS TO be vertical.
 
Use the class name. So in this case, heartofdivinity.

To make a animated sprite, add this to your SetStaticDefault of the item
Main.RegisterItemAnimation(item.type, new DrawAnimationVertical(speed, frames));
Where speed is how many ticks each frame is (60 ticks = 1 second) and frames is how many animation frames it has.

On the spriting side, for every frame you want, you add it to the sprite image.
I'll use this as a example:
View attachment 268252
The animation starts from the top and goes down a "frame" (your item's height parameter) every certian amount of ticks. So in this example, the items would go from red to yellow to green to blue, then loop back around. The animation HAS TO be vertical.
Thank you. The spriting part of it reminds me of Minecraft in a way.
Also, I have another item called Star of Divinity, and it's sprite size is 22x24, so would I resize the image t 24x24, or 22x22? 22x22 would take part of the top off of my sprite.
The sprite:
item_03.png
 
Last edited:
Do I add
class MyGlobalNPC : GlobalNPC { public override void NPCLoot(NPC npc) { if(npc.type == NPCID.MoonLordCore) // Use MoonLordCore for drops, since it's killed last { item.NewItem(npc.getRect(), ItemType<MaterialItem>(), 5); } // Addtional if statements here if you would like to add drops to other vanilla npc. } } to the end of my code? (The very bottom, but still inside of the last })
 
Last edited:
I got the following error while trying to build my mod:
Capture.PNG

Here is my code:

C#:
using Terraria.ID;
using Terraria.ModLoader;
using static Terraria.ModLoader.ModContent;

namespace playerboosteritems.Items
{
    class MyGlobalNPC : GlobalNPC
    {
        public override void NPCLoot(NPC npc)
        {
            if (npc.type == NPCID.MoonLordCore) // Use MoonLordCore for drops, since it's killed last
            {
                item.shardofdivinity(npc.getRect(), ItemType<MaterialItem>(), 7);
            }
            {
                item.shardofglory(npc.getRect(), ItemType<MaterialItem>(), 11);
            }
            // Addtional if statements here if you would like to add drops to other vanilla npc.
        }
    }
}
I assume that the error has to do with the following line:
public override void NPCLoot(NPC npc)
 
close, the issue is actually
C#:
{
   item.shardofdivinity(npc.getRect(), ItemType<MaterialItem>(), 7);
}
{
   item.shardofglory(npc.getRect(), ItemType<MaterialItem>(), 11);
}
You can't use "item.nameofitem", you have to use Item.NewItem (notice the capital I in Item)
I'm guessing you want a range from 7 to 11? If so, you do it like this:
C#:
Item.NewItem(npc.getRect(), ItemType<MaterialItem>(), Main.rand.Next(7, 11))
I recommend you read up on this page:
TModLoader Wiki NPC Drops and Loot
 
close, the issue is actually
C#:
{
   item.shardofdivinity(npc.getRect(), ItemType<MaterialItem>(), 7);
}
{
   item.shardofglory(npc.getRect(), ItemType<MaterialItem>(), 11);
}
You can't use "item.nameofitem", you have to use Item.NewItem (notice the capital I in Item)
I'm guessing you want a range from 7 to 11? If so, you do it like this:
C#:
Item.NewItem(npc.getRect(), ItemType<MaterialItem>(), Main.rand.Next(7, 11))
I recommend you read up on this page:
TModLoader Wiki NPC Drops and Loot
Thank you for your help. (Also, I was trying to make one drop less than the other.)
 
Back
Top Bottom