tModLoader [Tutorial] TModLoader: Projectile Help

I am unable to make it so I can both consume ammo and use a custom projectile. Anytime I write in "item.useAmmo = AmmoID.Bullet;" it will just shoot a bullet instead of the projectile I set for it to shoot.
 
Hi AwesomePerson159 how do I add the homing AI hook to my projectile?
when I used it more than 99 errors appeared.
 
I get 11 errors while using the
public override bool Shoot(Player player, ref Microsoft.Xna.Framework.Vector2 position, ref float speedX, ref float speedY, ref int type, ref int damage, ref float knockBack)

{
}
hook:
C:\Users\goodw\OneDrive\Документы\My Games\Terraria\ModLoader\Mod Sources\stupidopweapon\Items\BFG69000.cs(40,29) : error CS0246: The type or namespace name 'Player' could not be found (are you missing a using directive or an assembly reference?)
C:\Users\goodw\OneDrive\Документы\My Games\Terraria\ModLoader\Mod Sources\stupidopweapon\Items\BFG69000.cs(40,23) : error CS0115: 'BFG69000.Shoot(Player, ref Vector2, ref float, ref float, ref int, ref int, ref float)': no suitable method found to override
C:\Users\goodw\OneDrive\Документы\My Games\Terraria\ModLoader\Mod Sources\stupidopweapon\Items\BFG69000.cs(29,3) : error CS0103: The name 'projectile' does not exist in the current context
C:\Users\goodw\OneDrive\Документы\My Games\Terraria\ModLoader\Mod Sources\stupidopweapon\Items\BFG69000.cs(30,3) : error CS0103: The name 'projectile' does not exist in the current context
C:\Users\goodw\OneDrive\Документы\My Games\Terraria\ModLoader\Mod Sources\stupidopweapon\Items\BFG69000.cs(43,29) : error CS0103: The name 'MathHelper' does not exist in the current context
C:\Users\goodw\OneDrive\Документы\My Games\Terraria\ModLoader\Mod Sources\stupidopweapon\Items\BFG69000.cs(44,24) : error CS0103: The name 'Vector2' does not exist in the current context
C:\Users\goodw\OneDrive\Документы\My Games\Terraria\ModLoader\Mod Sources\stupidopweapon\Items\BFG69000.cs(44,46) : error CS0246: The type or namespace name 'Vector2' could not be found (are you missing a using directive or an assembly reference?)
C:\Users\goodw\OneDrive\Документы\My Games\Terraria\ModLoader\Mod Sources\stupidopweapon\Items\BFG69000.cs(47,16) : error CS0246: The type or namespace name 'Vector2' could not be found (are you missing a using directive or an assembly reference?)
C:\Users\goodw\OneDrive\Документы\My Games\Terraria\ModLoader\Mod Sources\stupidopweapon\Items\BFG69000.cs(47,45) : error CS0246: The type or namespace name 'Vector2' could not be found (are you missing a using directive or an assembly reference?)
C:\Users\goodw\OneDrive\Документы\My Games\Terraria\ModLoader\Mod Sources\stupidopweapon\Items\BFG69000.cs(47,79) : error CS0103: The name 'MathHelper' does not exist in the current context
C:\Users\goodw\OneDrive\Документы\My Games\Terraria\ModLoader\Mod Sources\stupidopweapon\Items\BFG69000.cs(48,16) : error CS0103: The name 'Projectile' does not exist in the current context
And heres my code:
Code:
using Terraria.ID;
using Terraria.ModLoader;

namespace stupidopweapon.Items
{
    public class BFG69000 : ModItem
    {
        public override void SetStaticDefaults()
        {
            // DisplayName.SetDefault(BFG 69000); // 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("stupid op");
        }

        public override void SetDefaults()
        {
            item.damage = 69420;
            item.melee = true;
            item.width = 40;
            item.height = 40;
            item.useTime = 1;
            item.useAnimation = 1;
            item.useStyle = 1;
            item.knockBack = 69;
            item.value = 10000;
            item.rare = 2;
            item.UseSound = SoundID.Item1;
            item.autoReuse = true;
            item.shoot = ProjectileID.NightBeam;
            projectile.aiStyle = 0;
            projectile.friendly  = true;
        }

        public override void AddRecipes()
        {
            ModRecipe recipe = new ModRecipe(mod);
            recipe.AddIngredient(ItemID.DirtBlock, 1);
            recipe.SetResult(this);
            recipe.AddRecipe();
        }
        public override bool Shoot(Player player, ref Microsoft.Xna.Framework.Vector2 position, ref float speedX, ref float speedY, ref int type, ref int damage, ref float knockBack)
        {
            float numberProjectiles = 50; // 3 shots
            float rotation = MathHelper.ToRadians(45);//Shoots them in a 45 degree radius. (This is technically 90 degrees because it's 45 degrees up from your cursor and 45 degrees down)
            position += Vector2.Normalize(new Vector2(speedX, speedY)) * 45f; //45 should equal whatever number you had on the previous line
            for (int i = 0; i < numberProjectiles; i++)
            {
                Vector2 perturbedSpeed = new Vector2(speedX, speedY).RotatedBy(MathHelper.Lerp(-rotation, rotation, i / (numberProjectiles - 1))) * .2f; // Vector for spread. Watch out for dividing by 0 if there is only 1 projectile.
                Projectile.NewProjectile(position.X, position.Y, perturbedSpeed.X, perturbedSpeed.Y, type, damage, knockBack, player.whoAmI); //Creates a new projectile with our new vector for spread.
            }
            return false; //makes sure it doesn't shoot the projectile again after this
        }
    }
}
45 degree radius. (This is technically 90 degrees because it's 45 degrees up from your cursor and 45 degrees down)
            position += Vector2.Normalize(new Vector2(speedX, speedY)) * 45f; //45 should equal whatever number you had on the previous line
            for (int i = 0; i < numberProjectiles; i++)
            {
                Vector2 perturbedSpeed = new Vector2(speedX, speedY).RotatedBy(MathHelper.Lerp(-rotation, rotation, i / (numberProjectiles - 1))) * .2f; // Vector for spread. Watch out for dividing by 0 if there is only 1 projectile.
                Projectile.NewProjectile(position.X, position.Y, perturbedSpeed.X, perturbedSpeed.Y, type, damage, knockBack, player.whoAmI); //Creates a new projectile with our new vector for spread.
            }
            return false; //makes sure it doesn't shoot the projectile again after this
        }
    }
}
[CODE]
 
How would i apply the Script that rains the projectiles from the sky to a boss?
 
using System;
using Microsoft.Xna.Framework;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;

namespace ShieldMod.Items
{
public class FieryGreatShield : ModItem
{
public override void SetStaticDefaults()
{
// DisplayName.SetDefault("TheScythe"); // 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("This is a basic modded shield.");
}
public override void SetDefaults()
{

item.damage = 30;
item.melee = true;
item.width = 38;
item.height = 38;
item.scale = 1.1f;
item.maxStack = 1;
item.useTime = 10;
item.useAnimation = 30;
item.knockBack = 4f;
item.noMelee = true;
item.noUseGraphic = true;
item.useTurn = true;
item.useStyle = 5;
item.value = Item.sellPrice(0, 1, 0, 0);
item.rare = 3;
item.shoot = mod.ProjectileType("FieryGreatShieldproj"); //put your Spear projectile name
item.shootSpeed = 5f;

}
public override bool Shoot(Player player, ref Vector2 position, ref float speedX, ref float speedY, ref int type, ref int damage, ref float knockBack)
{
Projectile.NewProjectile = mod.ProjectileType("FlameThrowerproj");
}


public override void AddRecipes() //How to craft this item
{
ModRecipe recipe = new ModRecipe(mod);
recipe.AddIngredient(ItemID.DirtBlock, 1); //you need 1 DirtBlock
recipe.AddTile(TileID.WorkBenches); //at work bench
recipe.SetResult(this);
recipe.AddRecipe();
}
}
}





cannot assign to NewProjectile because it is a methodgroup

Help
 
@Codename_Ps your error is fairly simple.
Just add the following lines at the top of your file(Also its not needed to use the additional code from the multiple prjectile shooting to make multiple projectiles rain from the sky):
using Microsoft.Xna.Framework;
using Terraria;
using AurumMod.Items;
using Terraria.Utilities;
using static Terraria.ModLoader.ModContent;
@Darlak simply add item.shoot = your projectile; in the setdefaults hook
Feel free to message/@ me if more problems occur.
 
Last edited:
I need help with a yoyo shooting projectiles when an enemy is close, the yoyo just stays there in midair when i let go of it. It also shoots way too many projectiles and i would like to know how to make it shoot slower.
The code:
Code:
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;

namespace EgamingsCryptolicMod.Projectiles
{
    public class CryptolicYoyoProjectile : ModProjectile
    {
        public override void SetStaticDefaults()
        {
            ProjectileID.Sets.YoyosLifeTimeMultiplier[projectile.type] = -1f;
            ProjectileID.Sets.YoyosMaximumRange[projectile.type] = 500f;
            ProjectileID.Sets.YoyosTopSpeed[projectile.type] = 20f;
        }

        public override void SetDefaults()
        {
            projectile.extraUpdates = 0;
            projectile.width = 16;
            projectile.height = 16;
            // aiStyle 99 is used for all yoyos, and is Extremely suggested, as yoyo are extremely difficult without them
            projectile.aiStyle = 99;
            projectile.friendly = true;
            projectile.penetrate = -1;
            projectile.melee = true;
        }

        public override void PostAI()
        {
            if (Main.rand.NextBool())
            {
                Dust dust = Dust.NewDustDirect(projectile.position, projectile.width, projectile.height, 61);
                dust.noGravity = true;
                dust.scale = 1.6f;
            }
        }
        public override void OnHitNPC(NPC target, int damage, float knockback, bool crit)
        {
            Player p = Main.player[projectile.owner];
            int healingAmount = damage / 1000; //decrease the value 30 to increase heal, increase value to decrease. Or you can just replace damage/x with a set value to heal, instead of making it based on damage.
            p.statLife += healingAmount;
            p.HealEffect(healingAmount, true);
        }
        public override void AI()
        {
            for (int i = 0; i < 200; i++)
            {
                NPC target = Main.npc[i];
                float shootToX = target.position.X + (float)target.width * 0.5f - projectile.Center.X;
                float shootToY = target.position.Y - projectile.Center.Y;
                float distance = (float)System.Math.Sqrt((double)(shootToX * shootToX + shootToY * shootToY));
                if (distance < 100f && !target.friendly && target.active)
                {
                    if (projectile.ai[0] > 10f)
                    {
                        distance = 3f / distance;
                        shootToX *= distance * 5;
                        shootToY *= distance * 5;
                        int proj = Projectile.NewProjectile(projectile.Center.X, projectile.Center.Y, shootToX, shootToY, mod.ProjectileType("CryptolicBlast"), projectile.damage, projectile.knockBack, Main.myPlayer, 0f, 0f); //mod.ProjectileType("Laser") is the projectile it shoots, change it to what you like
                        Main.projectile[proj].timeLeft = 300;
                        Main.projectile[proj].netUpdate = true;
                        projectile.netUpdate = true;
                        Main.PlaySound(1, (int)projectile.position.X, (int)projectile.position.Y, 12);
                        projectile.ai[0] = -1f;
                    }

                }
                projectile.ai[0] += 1f;
            }
        }
    }
}
 

Attachments

  • 1281930_screenshots_20210522145353_1.jpg
    1281930_screenshots_20210522145353_1.jpg
    345.7 KB · Views: 201
Say that I wanted to create a throwing weapon, a bomb, that when it explodes, would rain down a few projectiles to it's position. Does anyone know how to do this?

using Terraria;
using Terraria.ID;
using Terraria.ModLoader;
using NexusMod.Projectiles;

namespace NexusMod.Items.Weapons.Assassin
{
internal class CometDecimator : ModItem
{
public override void SetStaticDefaults()
{
base.SetStaticDefaults();
DisplayName.SetDefault("Comet Decimator");
ItemID.Sets.ItemsThatCountAsBombsForDemolitionistToSpawn[item.type] = true;
}

public override void SetDefaults()
{
item.useStyle = ItemUseStyleID.SwingThrow;
item.shootSpeed = 12f;
item.shoot = ModContent.ProjectileType<Projectiles.CometDecimator>();
item.width = 32;
item.height = 32;
item.maxStack = 1;
item.consumable = true;
item.UseSound = SoundID.Item1;
item.useAnimation = 40;
item.useTime = 40;
item.noUseGraphic = true;
item.noMelee = true;
item.value = Item.sellPrice(0, 0, 20, 0);
item.rare = ItemRarityID.Red;
}

public override void AddRecipes()
{
ModRecipe recipe = new ModRecipe(mod);
recipe.AddIngredient(ModContent.ItemType<CometFragment>());
recipe.AddTile(TileID.LunarCraftingStation);
recipe.SetResult(this);
recipe.AddRecipe();
}
}
}

using Microsoft.Xna.Framework;
using System;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;
using NexusMod.Items;

namespace NexusMod.Projectiles
{
// to investigate: Projectile.Damage, (8843)
internal class CometDecimator : ModProjectile
{
public override void SetDefaults() {
// while the sprite is actually bigger than 15x15, we use 15x15 since it lets the projectile clip into tiles as it bounces. It looks better.
projectile.width = 15;
projectile.height = 15;
projectile.friendly = true;
projectile.penetrate = -1;

projectile.timeLeft = 300;

drawOffsetX = 5;
drawOriginOffsetY = 5;
}

public override void ModifyHitNPC(NPC target, ref int damage, ref float knockback, ref bool crit, ref int hitDirection) {
// Vanilla explosions do less damage to Eater of Worlds in expert mode, so we will too.
if (Main.expertMode) {
if (target.type >= NPCID.EaterofWorldsHead && target.type <= NPCID.EaterofWorldsTail) {
damage /= 5;
}
}
}


public override void AI() {
if (projectile.owner == Main.myPlayer && projectile.timeLeft <= 3) {
projectile.tileCollide = false;
// Set to transparent. This projectile technically lives as transparent for about 3 frames
projectile.alpha = 255;
// change the hitbox size, centered about the original projectile center. This makes the projectile damage enemies during the explosion.
projectile.position = projectile.Center;
//projectile.position.X = projectile.position.X + (float)(projectile.width / 2);
//projectile.position.Y = projectile.position.Y + (float)(projectile.height / 2);
projectile.width = 250;
projectile.height = 250;
projectile.Center = projectile.position;
//projectile.position.X = projectile.position.X - (float)(projectile.width / 2);
//projectile.position.Y = projectile.position.Y - (float)(projectile.height / 2);
projectile.damage = 250;
projectile.knockBack = 10f;
}
else {
// Smoke and fuse dust spawn.
if (Main.rand.NextBool()) {
int dustIndex = Dust.NewDust(new Vector2(projectile.position.X, projectile.position.Y), projectile.width, projectile.height, 31, 0f, 0f, 100, default(Color), 1f);
Main.dust[dustIndex].scale = 0.1f + (float)Main.rand.Next(5) * 0.1f;
Main.dust[dustIndex].fadeIn = 1.5f + (float)Main.rand.Next(5) * 0.1f;
Main.dust[dustIndex].noGravity = true;
Main.dust[dustIndex].position = projectile.Center + new Vector2(0f, (float)(-(float)projectile.height / 2)).RotatedBy((double)projectile.rotation, default(Vector2)) * 1.1f;
dustIndex = Dust.NewDust(new Vector2(projectile.position.X, projectile.position.Y), projectile.width, projectile.height, 6, 0f, 0f, 100, default(Color), 1f);
Main.dust[dustIndex].scale = 1f + (float)Main.rand.Next(5) * 0.1f;
Main.dust[dustIndex].noGravity = true;
Main.dust[dustIndex].position = projectile.Center + new Vector2(0f, (float)(-(float)projectile.height / 2 - 6)).RotatedBy((double)projectile.rotation, default(Vector2)) * 1.1f;
}
}
projectile.ai[0] += 1f;
if (projectile.ai[0] > 5f) {
projectile.ai[0] = 10f;
// Roll speed dampening.
if (projectile.velocity.Y == 0f && projectile.velocity.X != 0f) {
projectile.velocity.X = projectile.velocity.X * 0.97f;
//if (projectile.type == 29 || projectile.type == 470 || projectile.type == 637)
{
projectile.velocity.X = projectile.velocity.X * 0.99f;
}
if ((double)projectile.velocity.X > -0.01 && (double)projectile.velocity.X < 0.01) {
projectile.velocity.X = 0f;
projectile.netUpdate = true;
}
}
projectile.velocity.Y = projectile.velocity.Y + 0.2f;
}
// Rotation increased by velocity.X
projectile.rotation += projectile.velocity.X * 0.1f;
return;
}

}
}

However, the popup below appears when I attempt to build and reload the mod. Any ideas why this is happening?View attachment 336490
image_2021-08-03_184449.png
 
Hi, I tried your adapted chlorophyte bullet snippet, but it did nothing, the projectile acts like a normal bullet and does not home in. Why is that?
 
I am trying to make a projectile that appears above an enemy, tracks their movement and then falls downwards. How would I do that?
 
Is there a way of making it so when you equip an accessory and hit a enemy it spawns a projectile ?
 
The hook doesn't work
it gives me an error saying theres no method found to overide
 
Back
Top Bottom