tModLoader [Tutorial] Projectile Guide and Implementation: tModLoader Edition

Before I forget, here's the Projectile Code:
Code:
using System;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;
using Terraria.DataStructures;

namespace JoshuasMod.Projectiles.MourningArc
{
    public class MourningArc : ModProjectile
    {
        public override void SetStaticDefaults()
        {
            DisplayName.SetDefault("Mourning Arc");    //The weapon's name when shown in-game.
        }
      
        public override void SetDefaults()
        {
            projectile.width = 120;
            projectile.height = 120;
            projectile.timeLeft = 15;
            projectile.penetrate = -1;
            projectile.scale = 0.6f;
            projectile.ignoreWater = true;
            projectile.melee = true;
            projectile.friendly = true;
            projectile.tileCollide = false;
        }
      
        public override void AI()
        {
            //The sound code for the projectile.
            projectile.soundDelay--;
            if (projectile.soundDelay <= 0)
            {
                Main.PlaySound(2, (int)projectile.Center.X, (int)projectile.Center.Y, 15);
                projectile.soundDelay = 45;
            }
          
            //How the projectile works.
            Player player = Main.player[projectile.owner];
            if (Main.myPlayer == projectile.owner)
            {
                if (!player.channel || player.noItems || player.CCed)
                {
                    projectile.Kill();
                }
            }
            Lighting.AddLight(projectile.Center, 0f, 1f, 0f); //This is the Projectile's light color (RGB)
          
            projectile.Center = player.MountedCenter;
            projectile.position.X += player.width / 2 * player.direction;
            projectile.spriteDirection = player.direction;
            projectile.rotation += 0.36f * player.direction; //This is the Projectile's Spinning/Rotation Speed
            if (projectile.rotation > MathHelper.TwoPi)
            {
                projectile.rotation -= MathHelper.TwoPi;
            }
        }

        public override bool PreDraw(SpriteBatch spriteBatch, Color lightColor)
        {
            Texture2D texture = Main.projectileTexture[projectile.type];
            spriteBatch.Draw(texture, projectile.Center - Main.screenPosition, null, Color.White, projectile.rotation, new Vector2(texture.Width / 2, texture.Height / 2), 1f, projectile.spriteDirection == 1 ? SpriteEffects.None : SpriteEffects.FlipHorizontally, 0f);
            return false;
        }
      
        public override void OnHitNPC(NPC target, int damage, float knockback, bool crit)
        {
            target.AddBuff(BuffID.CursedInferno, 5 * 60); //Add CursedInferno Buff to the NPC for 5 seconds.
            target.immune[projectile.owner] = 2;
        }
    }
}
I used the "projectile.scale" line in a different Projectile, and that seemed to work for that Projectile. I'm guessing there's a code preventing "projectile.scale" from changing the sprite, because this Projectile is a spinning weapon (Like the Sky Dragon's Fury).
Ah, it's what I thought it'd be. You're manually drawing your projectile without taking into account the scale of the projectile:
Code:
spriteBatch.Draw(texture, projectile.Center - Main.screenPosition, null, Color.White, projectile.rotation, new Vector2(texture.Width / 2, texture.Height / 2), projectile.scale, projectile.spriteDirection == 1 ? SpriteEffects.None : SpriteEffects.FlipHorizontally, 0f);
I put projectile.scale as the drawing scale for your projectile. That should work.
 
Ah, it's what I thought it'd be. You're manually drawing your projectile without taking into account the scale of the projectile:
Code:
spriteBatch.Draw(texture, projectile.Center - Main.screenPosition, null, Color.White, projectile.rotation, new Vector2(texture.Width / 2, texture.Height / 2), projectile.scale, projectile.spriteDirection == 1 ? SpriteEffects.None : SpriteEffects.FlipHorizontally, 0f);
I put projectile.scale as the drawing scale for your projectile. That should work.
It did, thank you! :3
 
1) How do I apply an "After Image" effect on Projectiles? (If it's possible).
2) How do I make a Projectile increase in size?
 
Hey. I am a huge fan of Terraria mods and I started learning C# last summer. I wanted to get into modding for terraria of course. I found this guide and it seems relly helpful. I did everything you've written on your Google Docs-file and I even tried your file. But in both 'mods' I get an error about the MPT.cs and the build.txt.

GaBCtvZ.jpg

NZs35OX.jpg

can anyone help me?
 
Hey. I am a huge fan of Terraria mods and I started learning C# last summer. I wanted to get into modding for terraria of course. I found this guide and it seems relly helpful. I did everything you've written on your Google Docs-file and I even tried your file. But in both 'mods' I get an error about the MPT.cs and the build.txt.

GaBCtvZ.jpg

NZs35OX.jpg

can anyone help me?
show your build.txt
 
Hello I am quite new to coding and especially Terraria coding. I am trying to find how to do a spinning throwing weapon, I couldn't find any tutorials for it so I thought this would a reliable source to ask. So please how do you make a throwing weapon spin?
(Extra Question) With my throwing weapon I used a tutorial but the tutorial was for a spear and my weapon was more like a endless shuriken and they had a "Dust" effect when the spear collided with a tile or enemy but it was in the shape of a spear but I would like more of a explosion like dust effect but as I said I am quite a Newbie ( I did a bit of coding in Unity(Only Beginner Stuff). Sorry for disturbing you.:D
 
Hey, just looking for some help. I'm making a sword projectile, and I want it to make a noise upon despawning once its time limit is up. How would I go about doing this?
Sorry if this was answered elsewhere already, I didn't have much time too look :/
 
How do I make a weapon shoot 2 different projectiles? For example, I want ExampleSpear to shoot ExampleSpearProjectile and ExampleFireball. How would one go about this?
 
I just want to say, I was able to use that orbit code, but I had to change

Player p = Main.player[projectile.owner];
to
Player player = Main.player[projectile.owner];

because it wasn't recognizing "player" in

projectile.position.X = player.Center.X - (int)(Math.Cos(rad) * dist) - projectile.width/2;
projectile.position.Y = player.Center.Y - (int)(Math.Sin(rad) * dist) - projectile.height/2;

I don't know if that's been brought up, but, oddly enough, Example Spear had code that made it make sense to me, and the fix worked like a charm for my noob self. Cheers!
 
I'm not sure if this qualifies as a double post, but I guess I'll find out the hard way. Using the code for a projectile orbiting the owning player, how would I get the projectile to always point away from player.Center? I've been hammering away at this for hours, but I'm not savvy enough, I guess.

Edit: last item, too... getting the rotation to reverse when I change direction.

These are the last two hurdles to getting a collection of weapons done. I've hammered out a lot of it using Example Mod, Google, these forums and help from the Discord channel, but these have me stumped. Probably just because I don't know the syntax well enough, I know. :(
 
Last edited:
Just have the shoot value of the item as the ID number or use TerrariaID like this...

Code:
item.shoot = 405; //Literal integer ID of Flairon Bubble
item.shoot = ProjectileID.FlaironBubble; //Must have "using Terraria.ID;" at the top
remove item.shoot = ProjectileID.FlaironBubble; or it wont work anymore
 
I wanted to make a sword that fires a projectile which I did but the sword fires one projectile every tick causing this to be very OP. How can I make it fires the projectiles more slowly.
 
I wanted to make a sword that fires a projectile which I did but the sword fires one projectile every tick causing this to be very OP. How can I make it fires the projectiles more slowly.
Does the sword have a use time of 1? If so, it registers a swing every frame and therefore shoots a projectile every frame. Here's some working code:
using System;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;
// if I put a * in the note it means you can change the value and still have a working sword, ** means you can change it but shouldn't for a sword
namespace Mod.Items.Weapons // namespace, change Mod to whatever your mod folder name is, it has to be in the weapons folder which has to be in the items folder
{
public class Sword : ModItem // change sword to whatever the sword sprite is called
{
public override void SetStaticDefaults()
{
Tooltip.SetDefault("This is a sword that shoots projectiles'"); // tooltip, optional*
DisplayName.SetDefault("Example Projectile Sword"); // name, optional, defaults to the class (with spaces before capitals other than the first)*
}
public override void SetDefaults()
{
item.damage = 222; // damage*
item.melee = true; // it's a melee item, switch melee to ranged for ranged damage, magic for magic, etc. **
item.width = 34; // width of the sprite*
item.height = 40; // height of the sprite*
item.useTime = 15; // how many frames between it registering a swing (terraria is 60 fps so this swings 4 times per second and therefore shoots 4 times per second)*
item.useAnimation = 15; // how many frames does it take to visibly swing it*
item.useStyle = 1; // Use styles: 1 = sword, 3 = shortsword, 5 = staff/gun/bow, not sure about the others cuz I forgot**
item.knockBack = 3; //knockback*
item.value = Item.buyPrice(0, 75, 0, 0); // 5 times the sell price, in brackets it's (platinum coins, gold coins, silver coins, copper coins)*
item.rare = 9; // the rarity of the item*
item.autoReuse = true; // auto-swings the sword*
item.shoot = mod.ProjectileType("Beam"); // what projectile it shoots*
item.shootSpeed = 20f; // velocity of the projectile*
item.UseSound = SoundID.Item1; // use sound when you use it, this one is Sword**
item.useTurn = true; // can you turn when you use it?*
}

public override void AddRecipes() // you can delete this code to get rid of a recipe if you want it to be a drop
{
ModRecipe recipe = new ModRecipe(mod); // adding a recipe
recipe.AddIngredient(1570); // ingredient, this one is broken hero's sword*
recipe.AddTile(TileID.MythrilAnvil); // crafting station*
recipe.SetResult(this); // code to make the recipe work
recipe.AddRecipe(); // what he said ^
} // end of deleteable code
}
}
 
Does the sword have a use time of 1? If so, it registers a swing every frame and therefore shoots a projectile every frame. Here's some working code:
using System;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;
// if I put a * in the note it means you can change the value and still have a working sword, ** means you can change it but shouldn't for a sword
namespace Mod.Items.Weapons // namespace, change Mod to whatever your mod folder name is, it has to be in the weapons folder which has to be in the items folder
{
public class Sword : ModItem // change sword to whatever the sword sprite is called
{
public override void SetStaticDefaults()
{
Tooltip.SetDefault("This is a sword that shoots projectiles'"); // tooltip, optional*
DisplayName.SetDefault("Example Projectile Sword"); // name, optional, defaults to the class (with spaces before capitals other than the first)*
}
public override void SetDefaults()
{
item.damage = 222; // damage*
item.melee = true; // it's a melee item, switch melee to ranged for ranged damage, magic for magic, etc. **
item.width = 34; // width of the sprite*
item.height = 40; // height of the sprite*
item.useTime = 15; // how many frames between it registering a swing (terraria is 60 fps so this swings 4 times per second and therefore shoots 4 times per second)*
item.useAnimation = 15; // how many frames does it take to visibly swing it*
item.useStyle = 1; // Use styles: 1 = sword, 3 = shortsword, 5 = staff/gun/bow, not sure about the others cuz I forgot**
item.knockBack = 3; //knockback*
item.value = Item.buyPrice(0, 75, 0, 0); // 5 times the sell price, in brackets it's (platinum coins, gold coins, silver coins, copper coins)*
item.rare = 9; // the rarity of the item*
item.autoReuse = true; // auto-swings the sword*
item.shoot = mod.ProjectileType("Beam"); // what projectile it shoots*
item.shootSpeed = 20f; // velocity of the projectile*
item.UseSound = SoundID.Item1; // use sound when you use it, this one is Sword**
item.useTurn = true; // can you turn when you use it?*
}

public override void AddRecipes() // you can delete this code to get rid of a recipe if you want it to be a drop
{
ModRecipe recipe = new ModRecipe(mod); // adding a recipe
recipe.AddIngredient(1570); // ingredient, this one is broken hero's sword*
recipe.AddTile(TileID.MythrilAnvil); // crafting station*
recipe.SetResult(this); // code to make the recipe work
recipe.AddRecipe(); // what he said ^
} // end of deleteable code
}
}
THANK YOU
 
I made a modded sword which shoots a projectile (meowmere) but whenever I swing, the projectile just falls to the ground. It happened to all of my weapons that use projectiles. What do I need to add to the code so it will actually SHOOT the projectile?
[doublepost=1514137712,1514137253][/doublepost]Oh nevermind! I accidentally deleted the shoot speed.
If it's not that then I don't know what to do.
 
I'm trying to have my laser shoot from the sky straight down on the mouses location but I can't figure it out and I can't find it on the doc.
 
Back
Top Bottom