tModLoader Official tModLoader Help Thread

I tried looking into that, but I can't find one specifically made for melees' projectiles. They're all for projectiles such as bullets, magic, summons.
I'm trying to replicate weapon projectile such as the cats from the meowmere, the influx waver etherreal swords, etc.
 
Hewwo, I'm trying to "code" a melee projectile. The melee work, projectile pops up, the only problem is that, the projectile is just /there/.

The projectile phases through enemies, doesn't do much except fly away..
Can anyone show me a script I can reference to?

My current script:

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Terraria;
using Terraria.Graphics.Effects;
using Terraria.Graphics.Shaders;
using Terraria.ID;
using Terraria.ModLoader;

namespace YinBlade.Projectiles
{

public class Lightning : ModProjectile
{
public override void SetStaticDefaults()
{
DisplayName.SetDefault("Lightning");
projectile.width = 200;
projectile.height = 200;
projectile.friendly = true;
projectile.melee = true;
projectile.tileCollide = true;
projectile.penetrate = -1;
projectile.timeLeft = 200;
projectile.light = 20f;
projectile.extraUpdates = 1;
projectile.ignoreWater = true;
}
public override void AI()
{
projectile.rotation = (float)Math.Atan2((double)projectile.velocity.Y, (double)projectile.velocity.X) + 90f;
}
}
}
Take the projectile code and move everything except the DisplayName call to the SetDefaults method:
Code:
public override void SetStaticDefaults()
{
    DisplayName.SetDefault("Lightning");
}
public override void SetDefaults() 
{
    projectile.width = 200;
    projectile.height = 200;
    projectile.friendly = true;
    projectile.melee = true;
    projectile.tileCollide = true;
    projectile.penetrate = 1;
    projectile.timeLeft = 200;
    projectile.light = 20f;
    projectile.extraUpdates = 1;
    projectile.ignoreWater = true;
}
Start off with that and see how it works for you.
 
It did end up helping, it lead me to the code in the terraria game files to find the moveSpeed variable (or whatever you call it here, I'm too used to Python) :p So thanks!

I do have a string of questions now though.. The first one being, how would you create an item with damage reduction? The mod itself is for adding a whole load of shields into the game, and I've currently got them adding defense, but I do realise it's much better to have damage reduction instead. Plus, this would end up helping with finding a way to make knockback apply to the player for a mechanic I want, as it'd factor in the damage you take which I could find from a damage reduction calculation.

To make an item that gives you damage reduction, you can just do player.endurance(it's a float). As for manually doing damage reduction, well the algorithm for calculating damage reduction due to defense is damage/2 rounded up (eg 100 def 2 dmg = 1 dmg reduction). That's what I've managed to find, anyway.
Hope you do find a way, and that that helps :)
 
Is there such thing as setting a different damage value for your projectile vs the sword's damage value? (Projectile refers to projectiles of melee)
 
Anthing is possible (exept liquids :( ) with tmodloader, if you know how, but I don't know how. (then after I begin to wonder why I posted this)
 
I tried "projectile.damage = 100;" didn't work,
I did find something on the internet, but it resulted in some errors that I'm clueless about.. the line of code is:
virtual void Terraria.ModLoader.ModProjectile.ModifyHitNPC ( ... ref int 100, ... )
 
I tried "projectile.damage = 100;" didn't work,
I did find something on the internet, but it resulted in some errors that I'm clueless about.. the line of code is:
virtual void Terraria.ModLoader.ModProjectile.ModifyHitNPC ( ... ref int 100, ... )
Easiest way is to override the Shoot method on your ModItem:
Code:
public override void SetDefaults()
{
    // Blabla
}

public override bool Shoot(Player player, ref Vector2 position, ref float speedX, ref float speedY, ref int type, ref int damage, ref float knockBack)
{
    damage = 100;
    return true;
}
 
I am trying to make a vilethorn style weapon using the vanilla vilethorn projectile. Problem is i don't know how to combine the vilethorn with its end to create that kind of projectile that you see with it. I also want to make it so that it does the projectile every 5th swing since it is an autoreuse weapon. If someone could please help me out so far i have this as my code.
Code:
using Terraria;
using Terraria.DataStructures;
using Terraria.ID;
using Terraria.ModLoader;
using Microsoft.Xna.Framework;
namespace TrepidityMod.Items.Weapons.Prehardmode.Shortswords
{
    public class VilethornStabber : ModItem
    {
        public override void SetStaticDefaults()
        {
            DisplayName.SetDefault("Vilethorn Stabber");
        }
        public override void SetDefaults()
        {
            item.damage = 15;
            item.melee = true;
            item.width = 32;
            item.height = 32;
            item.useTime = 15;
            item.useAnimation = 15;
            item.useStyle = 3;
            item.knockBack = 3;
            item.value = 100;
            item.rare = 2;
            item.UseSound = SoundID.Item1;
            item.autoReuse = true;
            item.shoot = ProjectileID.VilethornTip;
        }

        public override void AddRecipes()
        {
            ModRecipe recipe = new ModRecipe(mod);
            recipe.AddIngredient(ItemID.DirtBlock, 1);
            recipe.AddTile(TileID.WorkBenches);
            recipe.SetResult(this);
            recipe.AddRecipe();
        }
    }
}
 
Last edited:
I am trying to make a vilethorn style weapon using the vanilla vilethorn projectile. Problem is i don't know how to combine the vilethorn with its end to create that kind of projectile that you see with it. I also want to make it so that it does the projectile every 5th swing since it is an autoreuse weapon. If someone could please help me out so far i have this as my code.
Code:
using Terraria;
using Terraria.DataStructures;
using Terraria.ID;
using Terraria.ModLoader;
using Microsoft.Xna.Framework;
namespace TrepidityMod.Items.Weapons.Prehardmode.Shortswords
{
    public class VilethornStabber : ModItem
    {
        public override void SetStaticDefaults()
        {
            DisplayName.SetDefault("Vilethorn Stabber");
        }
        public override void SetDefaults()
        {
            item.damage = 15;
            item.melee = true;
            item.width = 32;
            item.height = 32;
            item.useTime = 15;
            item.useAnimation = 15;
            item.useStyle = 3;
            item.knockBack = 3;
            item.value = 100;
            item.rare = 2;
            item.UseSound = SoundID.Item1;
            item.autoReuse = true;
            item.shoot = ProjectileID.VilethornTip;
        }

        public override void AddRecipes()
        {
            ModRecipe recipe = new ModRecipe(mod);
            recipe.AddIngredient(ItemID.DirtBlock, 1);
            recipe.AddTile(TileID.WorkBenches);
            recipe.SetResult(this);
            recipe.AddRecipe();
        }
    }
}
For the item.shoot set you'll want to use ProjectileID.VilethornBase instead of VilethornTip.
As for the shooting, here is a rudamentary example of how you could do something like that:

Code:
int swingAmount = 0;
public override bool Shoot(Player player, ref Vector2 position, ref float speedX, ref float speedY, ref int type, ref int damage, ref float knockBack)
{
    if(swingAmount++ < 5) return false;

    swingAmount = 0;
    return true;
}
This code can easily be improved on, but I hope you get the general idea.
 
Hello, I am trying to make a modded sword, and I'm running into two issues.
1. The sword's sprite doesn't show up when swinging.
2. No matter what I set the item.width and item.height values to, the sword always has a ridiculously long range (It can hit things offscreen)

Here is the code: (I've just started so there's not a lot)

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

namespace NinjagoMod.Items
{
public class SwordOfFire : ModItem
{
public override void SetStaticDefaults()
{
DisplayName.SetDefault("Sword of Fire");
Tooltip.SetDefault("Forged with gold from the Golden Peaks");
}
public override void SetDefaults()
{
item.damage = 25;
item.melee = true;
item.width = 40;
item.height = 40;
item.useTime = 22;
item.useAnimation = 22;
item.useStyle = 1;
item.knockBack = 6;
item.value = 50000;
item.rare = 2;
item.UseSound = SoundID.Item1;
item.autoReuse = true;
item.noUseGraphic = false;

}

public override void AddRecipes()
{
ModRecipe recipe = new ModRecipe(mod);
recipe.AddIngredient(ItemID.GoldBar, 15);
recipe.AddIngredient(ItemID.Diamond, 1);
recipe.AddIngredient(ItemID.Ruby, 5);
recipe.AddTile(TileID.Anvils);
recipe.SetResult(this);
recipe.AddRecipe();

recipe = new ModRecipe(mod);
recipe.AddIngredient(ItemID.PlatinumBar, 15);
recipe.AddIngredient(ItemID.Diamond, 1);
recipe.AddIngredient(ItemID.Ruby, 5);
recipe.AddTile(TileID.Anvils);
recipe.SetResult(this);
recipe.AddRecipe();
}




public override void OnHitNPC(Player player, NPC target, int damage, float knockBack, bool crit)
{
target.AddBuff(BuffID.OnFire, 300);
}





}
}
Set item.nousegraphic to true
 
Hey, how would I go about making a boomerang pierce when it goes forwards, instead of just piercing when it goes backwards?

The only way I've gotten it to work is by either setting the projectile to basically be the Paladin's Hammer with projectile.type or if you hit with just the tip of the throw, with some snippets of the source code.

Code:
using System;
using Microsoft.Xna.Framework;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;

namespace Subweapons.Items.Cross {
public class WoodenCross : ModItem
{
    public override void SetDefaults()
    {
   
        item.knockBack = 4;     
        item.damage = 10;     
        item.useAnimation = 21;
        item.useTime = 21;
        item.useStyle = 1;     
        item.UseSound = SoundID.Item1; 
        item.width = 34;
        item.height = 34;
        item.maxStack = 999;
        item.shoot = mod.ProjectileType("WoodenCross");
            item.consumable = true;
            item.noUseGraphic = true;
            item.noMelee = true;
            item.autoReuse = false;
            item.thrown = true; 
        item.shootSpeed = 5.9f;     
            item.value = Item.sellPrice(copper: 10);
        item.rare = 0;
    }

    public override void AddRecipes()
    {
        ModRecipe recipe = new ModRecipe(mod);
        recipe.AddIngredient(ItemID.DirtBlock, 1);             
        recipe.SetResult(this, 50);
        recipe.AddRecipe();
    }
}}
Code:
using Microsoft.Xna.Framework;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;

namespace Subweapons.Projectiles.Cross
{
    public class WoodenCross : ModProjectile
    {
        public override void SetStaticDefaults()
        {
            DisplayName.SetDefault("WoodenCross");
        }

        public override void SetDefaults()
        {
            projectile.CloneDefaults(ProjectileID.WoodenBoomerang);

            projectile.friendly = true;
            projectile.thrown = true; 
            projectile.tileCollide = false; 
     
            projectile.penetrate = -1;
            projectile.width = 22;
            projectile.height = 22;
            projectile.MaxUpdates = 2;          
            aiType = ProjectileID.WoodenBoomerang;
        }
     
        public override void AI()
        {
            if ((double) projectile.ai[1] >= 27.5)
            {
              projectile.ai[0] = 1f;
              projectile.ai[1] = 0.0f;
              projectile.netUpdate = true;
            }
        }
    }
}
WoodenCross.png

And there's the item's image itself, if you want to test it in-game.
 
Hey, how would I go about making a boomerang pierce when it goes forwards, instead of just piercing when it goes backwards?

The only way I've gotten it to work is by either setting the projectile to basically be the Paladin's Hammer with projectile.type or if you hit with just the tip of the throw, with some snippets of the source code.

Code:
using System;
using Microsoft.Xna.Framework;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;

namespace Subweapons.Items.Cross {
public class WoodenCross : ModItem
{
    public override void SetDefaults()
    {

        item.knockBack = 4;  
        item.damage = 10;  
        item.useAnimation = 21;
        item.useTime = 21;
        item.useStyle = 1;  
        item.UseSound = SoundID.Item1;
        item.width = 34;
        item.height = 34;
        item.maxStack = 999;
        item.shoot = mod.ProjectileType("WoodenCross");
            item.consumable = true;
            item.noUseGraphic = true;
            item.noMelee = true;
            item.autoReuse = false;
            item.thrown = true;
        item.shootSpeed = 5.9f;  
            item.value = Item.sellPrice(copper: 10);
        item.rare = 0;
    }

    public override void AddRecipes()
    {
        ModRecipe recipe = new ModRecipe(mod);
        recipe.AddIngredient(ItemID.DirtBlock, 1);          
        recipe.SetResult(this, 50);
        recipe.AddRecipe();
    }
}}
Code:
using Microsoft.Xna.Framework;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;

namespace Subweapons.Projectiles.Cross
{
    public class WoodenCross : ModProjectile
    {
        public override void SetStaticDefaults()
        {
            DisplayName.SetDefault("WoodenCross");
        }

        public override void SetDefaults()
        {
            projectile.CloneDefaults(ProjectileID.WoodenBoomerang);

            projectile.friendly = true;
            projectile.thrown = true;
            projectile.tileCollide = false;
  
            projectile.penetrate = -1;
            projectile.width = 22;
            projectile.height = 22;
            projectile.MaxUpdates = 2;       
            aiType = ProjectileID.WoodenBoomerang;
        }
  
        public override void AI()
        {
            if ((double) projectile.ai[1] >= 27.5)
            {
              projectile.ai[0] = 1f;
              projectile.ai[1] = 0.0f;
              projectile.netUpdate = true;
            }
        }
    }
}
View attachment 210150
And there's the item's image itself, if you want to test it in-game.
Not sure, but a possible fix:
Code:
public override void PostAI()
{
    projectile.penetrate = -1;
}
I'll see if I can find anything else that might help you and adjust this post correspondingly.

EDIT:
Nevermind, that was stupid. I misunderstood the question. Best thing you can do is copy the functionality of the boomerang (instead of using vanilla AI) and make your changes there.
 
Hello guys I would like to know how to add OnHitNPC on accessory. I was already reading this forum and I was looking on google but I still can't figure out how to do that. I have ModPlayer class, Buff Class and ItemClass. When I hit NPC in game nothing happen. Am I missing something ?

Player Class
Code:
public class MyPlayer : ModPlayer
    {    
        public bool NaughtyRomiPresentEQ;
        public override void ResetEffects()
        {
            this.NaughtyRomiPresentEQ = false;         
        }
        public override void OnHitNPC(Item item, NPC target, int damage, float knockback, bool crit)
        {
            if (this.NaughtyRomiPresentEQ == true)
               player.AddBuff(mod.BuffType("BarrierBuff"), 1000);        
        }
    }


Item Class

Code:
public class NaughtyRomiPresent : ModItem
    {
        public override void SetStaticDefaults()
        {
            Tooltip.SetDefault("");
        }
        public override void SetDefaults()
        {         
            item.width = 10;
            item.height = 14;
            item.value = 10;
            item.rare = 3;
            item.accessory = true;         
        }
        public override void UpdateAccessory(Player player, bool hideVisual)
        {
            player.GetModPlayer<MyPlayer>(mod).NaughtyRomiPresentEQ = true;
        }
        public override void AddRecipes()
        {
            ModRecipe recipe = new ModRecipe(mod);
            recipe.AddIngredient(ItemID.DirtBlock, 1);
            recipe.AddTile(TileID.LunarCraftingStation);
            recipe.SetResult(this);
            recipe.AddRecipe();
        }
    }

Buff Class
Code:
public class BarrierBuff : ModBuff
    {
        public override void SetDefaults()
        {
            DisplayName.SetDefault("Barrier");
            Description.SetDefault("");
            Main.buffNoTimeDisplay[Type] = true;
        }
        public override void Update(Player player, ref int buffIndex)
        {
            player.statDefense += 500;   //example
        }
    }

Thanks for your time :)
 
Hello guys I would like to know how to add OnHitNPC on accessory. I was already reading this forum and I was looking on google but I still can't figure out how to do that. I have ModPlayer class, Buff Class and ItemClass. When I hit NPC in game nothing happen. Am I missing something ?

Player Class
Code:
public class MyPlayer : ModPlayer
    {   
        public bool NaughtyRomiPresentEQ;
        public override void ResetEffects()
        {
            this.NaughtyRomiPresentEQ = false;        
        }
        public override void OnHitNPC(Item item, NPC target, int damage, float knockback, bool crit)
        {
            if (this.NaughtyRomiPresentEQ == true)
               player.AddBuff(mod.BuffType("BarrierBuff"), 1000);       
        }
    }


Item Class

Code:
public class NaughtyRomiPresent : ModItem
    {
        public override void SetStaticDefaults()
        {
            Tooltip.SetDefault("");
        }
        public override void SetDefaults()
        {        
            item.width = 10;
            item.height = 14;
            item.value = 10;
            item.rare = 3;
            item.accessory = true;        
        }
        public override void UpdateAccessory(Player player, bool hideVisual)
        {
            player.GetModPlayer<MyPlayer>(mod).NaughtyRomiPresentEQ = true;
        }
        public override void AddRecipes()
        {
            ModRecipe recipe = new ModRecipe(mod);
            recipe.AddIngredient(ItemID.DirtBlock, 1);
            recipe.AddTile(TileID.LunarCraftingStation);
            recipe.SetResult(this);
            recipe.AddRecipe();
        }
    }

Buff Class
Code:
public class BarrierBuff : ModBuff
    {
        public override void SetDefaults()
        {
            DisplayName.SetDefault("Barrier");
            Description.SetDefault("");
            Main.buffNoTimeDisplay[Type] = true;
        }
        public override void Update(Player player, ref int buffIndex)
        {
            player.statDefense += 500;   //example
        }
    }

Thanks for your time :)
That looks right, maybe try to identify where it is messing up. You can do Main.NewText("Hi"); in Update or UpdateAccessory to make sure the code is running. You can also player.AddBuff with a vanilla Buff to see if that works. If you can identify what is messing up, that'll help.

As always, the best place for help is the discord chat: discord.me/tModLoader
 
That looks right, maybe try to identify where it is messing up. You can do Main.NewText("Hi"); in Update or UpdateAccessory to make sure the code is running. You can also player.AddBuff with a vanilla Buff to see if that works. If you can identify what is messing up, that'll help.

As always, the best place for help is the discord chat: discord.me/tModLoader

Well .... I am using my mage/ranger character for that and now I accidentally hit NPC with pickaxe and it is working ... only with melee weapons is there anything I can switch it for all kind of weapons ? It wont work with magic/ranged weapons or even with projectiles which are releasing from melee weapons. My bad that I didnt check this before but I didnt expect that it is gona be locked just for melee weapons.
 
Back
Top Bottom