tModLoader How do I make a true melee weapon that spawns projectiles on hit?

isaac bunny man

Terrarian
I'm currently trying to make a couple of true melee weapons, but I kinda... don't know how to make them do what I want them to do?
C#:
using Microsoft.Xna.Framework;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;

namespace funnymodthing123.Content.Items
{
    // This is a basic item template.
    // Please see tModLoader's ExampleMod for every other example:
    // https://github.com/tModLoader/tModLoader/tree/stable/ExampleMod
    public class NightBlade : ModItem
    {
        // The Display Name and Tooltip of this item can be edited in the 'Localization/en-US_Mods.funnymodthing123.hjson' file.
        public override void SetDefaults()
        {
            Item.damage = 75;
            Item.DamageType = DamageClass.Melee;
            Item.width = 64;
            Item.height = 64;
            Item.useTime = 25;
            Item.useAnimation = 25;
            Item.useStyle = ItemUseStyleID.Swing;
            Item.knockBack = 8;
            Item.value = Item.buyPrice(gold: 8);
            Item.rare = ItemRarityID.Lime;
            Item.UseSound = SoundID.Item1;
            Item.autoReuse = true;
        }

        public override void AddRecipes()
        {
            Recipe recipe1 = CreateRecipe();
            recipe1.AddIngredient(ItemID.FairyQueenMagicItem, 1);
            recipe1.AddIngredient(ItemID.PearlwoodSword, 1);
            recipe1.AddTile(TileID.MythrilAnvil);
            recipe1.Register();
        }
    }
}
Does anyone know how to make my sword spawn Nightglow projectiles on hit?
(oh yeah and "nightblade" is a placeholder name)
 
Another thing: I'm trying to change the recipe to include the Crystal Sword from my mod (an early hardmode weapon) but it always appears with an error

C#:
using funnymodthing123.Content.Items;
using Microsoft.Xna.Framework;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;

namespace funnymodthing123.Content.Items
{
    // This is a basic item template.
    // Please see tModLoader's ExampleMod for every other example:
    // https://github.com/tModLoader/tModLoader/tree/stable/ExampleMod
    public class NightBlade : ModItem
    {
        // The Display Name and Tooltip of this item can be edited in the 'Localization/en-US_Mods.funnymodthing123.hjson' file.
        public override void SetDefaults()
        {
            Item.damage = 75;
            Item.DamageType = DamageClass.Melee;
            Item.width = 64;
            Item.height = 64;
            Item.useTime = 25;
            Item.useAnimation = 25;
            Item.useStyle = ItemUseStyleID.Swing;
            Item.knockBack = 8;
            Item.value = Item.buyPrice(gold: 8);
            Item.rare = ItemRarityID.Lime;
            Item.UseSound = SoundID.Item1;
            Item.autoReuse = true;
        }

        public override void AddRecipes()
        {
            Recipe recipe1 = CreateRecipe();
            recipe1.AddIngredient(ItemID.FairyQueenMagicItem, 1);
            recipe1.AddIngredient(ModContent.ItemType<CrystalSword>, 1);
            recipe1.AddTile(TileID.MythrilAnvil);
            recipe1.Register();
        }
    }
}

(i have a unique way of learning code, where i edit code and copy and paste parts of the code into my own code until i get the hang of it and be able to write my own code)
 
If you want them to spawn only on hit, override OnHitNPC and do your projectile spawning logic there
 
If you want them to spawn only on hit, override OnHitNPC and do your projectile spawning logic there
i tried, it's not working still
C#:
using funnymodthing123.Content.Items;
using Microsoft.Xna.Framework;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;

namespace funnymodthing123.Content.Items
{ 
    // This is a basic item template.
    // Please see tModLoader's ExampleMod for every other example:
    // https://github.com/tModLoader/tModLoader/tree/stable/ExampleMod
    public class NightBlade : ModItem
    {
        // The Display Name and Tooltip of this item can be edited in the 'Localization/en-US_Mods.funnymodthing123.hjson' file.
        public override void SetDefaults()
        {
            Item.damage = 75;
            Item.DamageType = DamageClass.Melee;
            Item.width = 64;
            Item.height = 64;
            Item.useTime = 25;
            Item.useAnimation = 25;
            Item.useStyle = ItemUseStyleID.Swing;
            Item.knockBack = 8;
            Item.value = Item.buyPrice(gold: 8);
            Item.rare = ItemRarityID.Lime;
            Item.UseSound = SoundID.Item1;
            Item.autoReuse = true;
            Item.shoot = ProjectileID.FairyQueenMagicItemShot; // For some reason, all the guns in the vanilla source have this.
            Item.shootSpeed = 6f; // The speed of the projectile (measured in pixels per frame.)
        }


        public override void OnHitNPC(Player player, NPC target, NPC.HitInfo hit, int damageDone)
        {
            public override bool Shoot(Player player, EntitySource_ItemUse_WithAmmo source, Vector2 position, Vector2 velocity, int type, int damage, float knockback)
        {
            const int NumProjectiles = 8; // The number of projectiles that this gun will shoot.

            for (int i = 0; i < NumProjectiles; i++)
            {
                // Rotate the velocity randomly by 30 degrees at max.
                Vector2 newVelocity = velocity.RotatedByRandom(MathHelper.ToRadians(360));

                // Decrease velocity randomly for nicer visuals.
                newVelocity *= 1f - Main.rand.NextFloat(0.3f);

                // Create a projectile.
                Projectile.NewProjectileDirect(source, position, newVelocity, type, damage, knockback, player.whoAmI);
            }

            return false; // Return false because we don't want tModLoader to shoot projectile
        }
        }


        public override void AddRecipes()
        {
            Recipe recipe1 = CreateRecipe();
            recipe1.AddIngredient(ItemID.FairyQueenMagicItem, 1);
            recipe1.AddIngredient(ModContent.ItemType<CrystalSword>, 1);
            recipe1.AddTile(TileID.MythrilAnvil);
            recipe1.Register();
        }
    }
}
is it because i'm putting a function inside another function? idk i don't have any projectile spawning logic
 
Yeah that's wrong, in OnHitNPC you just call Projectile.NewProjectile, not the entire shoot hook.
You can probably just copy the logic you have in Shoot and put it in OnHitNPC
 
Yeah that's wrong, in OnHitNPC you just call Projectile.NewProjectile, not the entire shoot hook.
You can probably just copy the logic you have in Shoot and put it in OnHitNPC
i tried for like 30 minutes i can't do it :guidesad:
C#:
using funnymodthing123.Content.Items;
using Microsoft.Xna.Framework;
using Terraria;
using Terraria.DataStructures;
using Terraria.ID;
using Terraria.ModLoader;

namespace funnymodthing123.Content.Items
{
    // This is a basic item template.
    // Please see tModLoader's ExampleMod for every other example:
    // https://github.com/tModLoader/tModLoader/tree/stable/ExampleMod
    public class NightBlade : ModItem
    {
        // The Display Name and Tooltip of this item can be edited in the 'Localization/en-US_Mods.funnymodthing123.hjson' file.
        public override void SetDefaults()
        {
            Item.damage = 84;
            Item.DamageType = DamageClass.Melee;
            Item.width = 64;
            Item.height = 64;
            Item.useTime = 25;
            Item.useAnimation = 25;
            Item.useStyle = ItemUseStyleID.Swing;
            Item.knockBack = 8;
            Item.value = Item.buyPrice(gold: 8);
            Item.rare = ItemRarityID.Lime;
            Item.UseSound = SoundID.Item1;
            Item.autoReuse = true;
            Item.shootSpeed = 8f; // The speed of the projectile (measured in pixels per frame.)

        }

        public override void OnHitNPC(Player player, NPC target, NPC.HitInfo hit, int damageDone)
        {
            const int velocity = 8;
            const int NumProjectiles = 8; // The number of projectiles that this gun will shoot.

            for (int i = 0; i < NumProjectiles; i++)
            {
                // Rotate the velocity randomly by 30 degrees at max.
                Vector2 newVelocity = velocity.RotatedByRandom(MathHelper.ToRadians(360));

                // Decrease velocity randomly for nicer visuals.
                newVelocity *= 1f - Main.rand.NextFloat(0.3f);

                // Create a projectile.
                Projectile.NewProjectile(Vector2(8, 0), newVelocity, ProjectileID.FairyQueenMagicItemShot, 42, 6, player.whoAmI);
            }

            return false; // Return false because we don't want tModLoader to shoot projectile
            target.AddBuff(BuffID.GelBalloonBuff, 600);
        }


        public override void AddRecipes()
        {
            Recipe recipe1 = CreateRecipe();
            recipe1.AddIngredient(ItemID.FairyQueenMagicItem, 1);
            recipe1.AddIngredient<Content.Items.CrystalSword>(1);
            recipe1.AddIngredient(ItemID.SoulofLight, 25);
            recipe1.AddTile(TileID.MythrilAnvil);
            recipe1.Register();
        }
    }
}
 
Looks like your method parameters are wrong, you're copying old code.
Examples
 
Another thing: I'm trying to change the recipe to include the Crystal Sword from my mod (an early hardmode weapon) but it always appears with an error

C#:
using funnymodthing123.Content.Items;
using Microsoft.Xna.Framework;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;

namespace funnymodthing123.Content.Items
{
    // This is a basic item template.
    // Please see tModLoader's ExampleMod for every other example:
    // https://github.com/tModLoader/tModLoader/tree/stable/ExampleMod
    public class NightBlade : ModItem
    {
        // The Display Name and Tooltip of this item can be edited in the 'Localization/en-US_Mods.funnymodthing123.hjson' file.
        public override void SetDefaults()
        {
            Item.damage = 75;
            Item.DamageType = DamageClass.Melee;
            Item.width = 64;
            Item.height = 64;
            Item.useTime = 25;
            Item.useAnimation = 25;
            Item.useStyle = ItemUseStyleID.Swing;
            Item.knockBack = 8;
            Item.value = Item.buyPrice(gold: 8);
            Item.rare = ItemRarityID.Lime;
            Item.UseSound = SoundID.Item1;
            Item.autoReuse = true;
        }

        public override void AddRecipes()
        {
            Recipe recipe1 = CreateRecipe();
            recipe1.AddIngredient(ItemID.FairyQueenMagicItem, 1);
            recipe1.AddIngredient(ModContent.ItemType<CrystalSword>, 1);
            recipe1.AddTile(TileID.MythrilAnvil);
            recipe1.Register();
        }
    }
}

(i have a unique way of learning code, where i edit code and copy and paste parts of the code into my own code until i get the hang of it and be able to write my own code)

i think i know what the issue with the recipe is, a recipe ingredient added by a mod should be implemented like this:

C#:
.AddIngredient<CrystalSword>()

i have no clue if adding recipe1 to every line is necessary or messing it up, but if it still doesn't work, i'd recommend redoing it so it looks like this instead:

C#:
public override void AddRecipes() {
    CreateRecipe();
        .AddIngredient(ItemID.FairyQueenMagicItem, 1);
        .AddIngredient<CrystalSword>();
        .AddTile(TileID.MythrilAnvil);
        .Register();
}
 
i think i know what the issue with the recipe is, a recipe ingredient added by a mod should be implemented like this:

C#:
.AddIngredient<CrystalSword>()

i have no clue if adding recipe1 to every line is necessary or messing it up, but if it still doesn't work, i'd recommend redoing it so it looks like this instead:

C#:
public override void AddRecipes() {
    CreateRecipe();
        .AddIngredient(ItemID.FairyQueenMagicItem, 1);
        .AddIngredient<CrystalSword>();
        .AddTile(TileID.MythrilAnvil);
        .Register();
}
i already figured it out, but thanks anyway
 
Back
Top Bottom