tModLoader [Tutorial] Projectile Guide and Implementation: tModLoader Edition

I have a slime launcher that can currently only shoot blue slime. How would I make it be only able to shoot either blue slime or green slime, with each one producing a different projectile?
 
So i looked at your simple homing ai but it still doesnt work

This is the code

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

namespace FirstMod.Projectiles
{

public class MjolnirProjectile : ModProjectile
{
public override void SetDefaults()
{
projectile.width = 20;
projectile.height = 28;
projectile.friendly = true;
projectile.melee = true;
projectile.tileCollide = false;
projectile.penetrate = -1;
projectile.timeLeft = 6000;
projectile.light = 0.20f;
projectile.extraUpdates = 1;
projectile.ignoreWater = true;
}
//For all of the NPC slots in Main.npc
//Note, you can replace NPC with other entities such as Projectiles and Players
public void AI()
{
for (int i = 0; i < 200; i++)
{
NPC target = Main.npc;
//If the npc is hostile
if (!target.friendly)
{
//Get the shoot trajectory from the projectile and target
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 the distance between the live targeted npc and the projectile is less than 480 pixels
if (distance < 480f && !target.friendly && target.active)
{
//Divide the factor, 3f, which is the desired velocity
distance = 3f / distance;

//Multiply the distance by a multiplier if you wish the projectile to have go faster
shootToX *= distance * 5;
shootToY *= distance * 5;

//Set the velocities to the shoot values
projectile.velocity.X = shootToX;
projectile.velocity.Y = shootToY;
}
}
}
}
}
}



Can you please tell me whats wrong
Thanks in advance
Kuugeki

Edit: Could you also please tell me how to put particles on it
 
How do I increase a projectile's velocity? I mean overall constant speed, no slowing down/speeding up after a certain time.

Edit: Also, how do I make a projectile grow in size over time?
 
How do I get my projectiles to cycle out? like it shoots a water bolt then the next is an arrow then a water bolt then arrow etc. you know what I mean?

EDIT: NVM I think I found it
 
Uhh I keep getting this error whenever I build and reload
'Terraria.Projectile' does not contain a definition for 'name' and no extension method 'name' accepting a first argument of type 'Terraria.Projectile' could be found (are you missing a using directive or an assembly reference?)
 
This is the code made by my friend to a sword that's sprite I designed.


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

namespace UniverseOfSwordsMod.Items.Weapons
{
public class TrueHorrormageddon : ModItem
{
public override void SetStaticDefaults()
{
Tooltip.SetDefault("'There used to be a graveyard, now it is a crater'");
}

public override void SetDefaults()
{
item.width = 35;
item.height = 35;
item.scale = 2.7F;
item.rare = 10;
item.useStyle = 1;
item.useTime = 15;
item.useAnimation = 15;
item.damage = 600;
item.knockBack = 8.0F;
item.UseSound = SoundID.Item71;
item.shoot = ProjectileID.StarWrath;
item.shootSpeed = 50;
item.value = 1000000;
item.autoReuse = true;
item.melee = true;
}

public override void UseStyle(Player player)
{
player.itemLocation.X -= 1f * player.direction;
player.itemLocation.Y -= 1f * player.direction;
}

public override void AddRecipes()
{
ModRecipe recipe = new ModRecipe(mod);
recipe.AddIngredient(null, "Horrormageddon", 1);
recipe.AddIngredient(null, "PowerOfTheGalactic", 1);
recipe.AddIngredient(null, "GnomBlade", 1);
recipe.AddIngredient(ItemID.BrokenHeroSword, 10);
recipe.AddIngredient(null, "UpgradeMatter", 25);
recipe.AddIngredient(null, "LunarOrb", 3);
recipe.AddIngredient(ItemID.LunarBar, 100);
recipe.AddTile(TileID.DemonAltar);
recipe.SetResult(this);
recipe.AddRecipe();
}

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(position.X, position.Y, speedX, speedY, ProjectileID.Meowmere, damage, knockBack, player.whoAmI);
Projectile.NewProjectile(position.X, position.Y, speedX, speedY, ProjectileID.InfernoFriendlyBlast, damage, knockBack, player.whoAmI);
Projectile.NewProjectile(position.X, position.Y, speedX, speedY, ProjectileID.VortexBeaterRocket, damage, knockBack, player.whoAmI);
Projectile.NewProjectile(position.X, position.Y, speedX, speedY, ProjectileID.DeathSickle, damage, knockBack, player.whoAmI);
return true;
}
}
}

The idea is for the sword to fire x3 of Star Wrath, Meowmere, Infernal Fork, vortex beater rockets, and Death Sickle projectiles. The problem is, that the sword only fires x3 of Death Sickle projectiles. What should be changed?
 
I need help modding a bow and a custom arrow
So I'm trying to code a bow to use all types of arrows but only shoot my custom arrow.
And I'm also not sure where to start when coding the custom arrow.
Any ideas on how to?

Here's the code for the bow:

using Terraria;
using Terraria.ID;
using Terraria.ModLoader;
namespace TheLegendOfZelda.Items
.Weapons.Ranged.LightBow
{
public class LightBow : ModItem
}
public override void SetStaticDefaults()
{
DisplayName.SetDefault("The Bow of Light");
Tooltip.SetDefault("The legendary Bow of Light");
}
public override void SetDefaults()
{
item.damage = 100;
item.ranged = true;
item.width = 40;
item.height = 40;
item.useTime = 20;
item.useAnimation = 6;
item.useStyle = 5;
item.noMelee = true;
item.autoReuse = true;
item.knockBack = 6;
item.value = 10000;
item.rare = 2;
item.UseSound = SoundID.Item1;
item.autoReuse = true;
item.shoot = mod.ProjectileType("LightArrow");
item.shootSpeed = 15;
item.useAmmo = AmmoID.Arrow;
}
}
public override void AddRecipes()
{
ModRecipe recipe = new ModRecipe(mod);
recipe.AddIngredient(mod,"MasterOre", 20);
recipe.AddIngredient(ItemID.HallowedRepeater);
recipe.AddIngredient(ItemID.Diamond, 25);
recipe.AddIngredient(ItemID.GoldBar, 10);
recipe.AddIngredient(ItemID.PlatinumBar, 10);
recipe.AddTile(TileID.MythrilAnvil);
recipe.SetResult(this);
recipe.AddRecipe();
}
}
}

BTW I'm new at coding c# (or whatever this coding language is). I've only been doing it for about a week now
 
Last edited:
I need help modding a bow and a custom arrow
So I'm trying to code a bow to use all types of arrows but only shoot my custom arrow.
And I'm also not sure where to start when coding the custom arrow.
Any ideas on how to?

Here's the code for the bow:

using Terraria;
using Terraria.ID;
using Terraria.ModLoader;
namespace TheLegendOfZelda.Items
.Weapons.Ranged.LightBow
{
public class LightBow : ModItem
}
public override void SetStaticDefaults()
{
DisplayName.SetDefault("The Bow of Light");
Tooltip.SetDefault("The legendary Bow of Light");
}
public override void SetDefaults()
{
item.damage = 100;
item.ranged = true;
item.width = 40;
item.height = 40;
item.useTime = 20;
item.useAnimation = 6;
item.useStyle = 5;
item.noMelee = true;
item.autoReuse = true;
item.knockBack = 6;
item.value = 10000;
item.rare = 2;
item.UseSound = SoundID.Item1;
item.autoReuse = true;
item.shoot = mod.ProjectileType("LightArrow");
item.shootSpeed = 15;
item.useAmmo = AmmoID.Arrow;
}
}
public override void AddRecipes()
{
ModRecipe recipe = new ModRecipe(mod);
recipe.AddIngredient(mod,"MasterOre", 20);
recipe.AddIngredient(ItemID.HallowedRepeater);
recipe.AddIngredient(ItemID.Diamond, 25);
recipe.AddIngredient(ItemID.GoldBar, 10);
recipe.AddIngredient(ItemID.PlatinumBar, 10);
recipe.AddTile(TileID.MythrilAnvil);
recipe.SetResult(this);
recipe.AddRecipe();
}
}
}

BTW I'm new at coding c# (or whatever this coding language is). I've only been doing it for about a week now
Like this, but without the check for the basic bullet since that's what you want: https://github.com/blushiemagic/tModLoader/blob/master/ExampleMod/Items/Weapons/ExampleGun.cs#L54
 
How do I make my weapon shoot 4-6 projectiles (Such as the TerraBeam Proj.) in an equal spread from the sword, while also shooting projectiles (Such as the StarWrathProj.) from the top of the screen? I got the StarWrath part down, just don't know how to implement/add the TerraBeam part in.
Code:
        public override bool Shoot(Player player, ref Vector2 position, ref float speedX, ref float speedY, ref int type, ref int damage, ref float knockBack)
        {
            int numberProjectiles = 4 + Main.rand.Next(1);
            for (int index = 0; index < numberProjectiles; ++index)
            {
                //This shoot function is almost identical to Star Wrath's, except with 4 projectiles instead of 3.
                Vector2 vector2_1 = new Vector2((float)((double)player.position.X + (double)player.width * 0.5 + (double)(Main.rand.Next(100) * -player.direction) + ((double)Main.mouseX + (double)Main.screenPosition.X - (double)player.position.X)), (float)((double)player.position.Y + (double)player.height * 0.5 - 600.0));   //This defines the projectile width, direction and position.
                vector2_1.X = (float)(((double)vector2_1.X + (double)player.Center.X) / 2.0) + (float)Main.rand.Next(-100, 100);
                vector2_1.Y -= (float)(100 * index);
                float num12 = (float)Main.mouseX + Main.screenPosition.X - vector2_1.X;
                float num13 = (float)Main.mouseY + Main.screenPosition.Y - vector2_1.Y;
                if ((double)num13 < 0.0) num13 *= -1f;
                if ((double)num13 < 20.0) num13 = 20f;
                float num14 = (float)Math.Sqrt((double)num12 * (double)num12 + (double)num13 * (double)num13);
                float num15 = item.shootSpeed / num14;
                float num16 = num12 * num15;
                float num17 = num13 * num15;
                float SpeedX = num16 + (float)Main.rand.Next(-12, 10) * 0.160f;  //This defines the projectile X position speed and randomness.
                float SpeedY = num17 + (float)Main.rand.Next(-12, 10) * 0.160f;  //This defines the projectile Y position speed and randomness.
                Projectile.NewProjectile(vector2_1.X, vector2_1.Y, SpeedX * 0.90f, SpeedY * 0.90f, ProjectileID.StarWrath, damage * 1, knockBack, Main.myPlayer, 0.0f, (float)Main.rand.Next(5)); //The "StarWrath" projectile's velocity is the same as the original projectile's speed.
            }
            return false;
        }

Edit 2: Nevermind, figured out how to shoot 2 different projectiles from different locations (Weapon and Top of Screen at least), yet still stuck on shooting 1 of the 3 different projectiles, each with a 33% chance of occurring.
 
Last edited:
Hey! I kinda need some help. I am trying to make a gun that shoots golden keys, but i kinda am stuck with the programming.

gun:

Code:
using System;
using Microsoft.Xna.Framework;

using Terraria;
using Terraria.ID; //We are borrowing properties from
using Terraria.ModLoader;

namespace TiborsMod.Items
{
    public class akey47 : ModItem
    {
        public override void SetDefaults()
        {
            item.name = "A-key 47"; //Its display name
            item.damage = 20; //The damage
            item.ranged = true; //Whether or not it is a ranged weapon
            item.width = 60; //Item width
            item.height = 60; //Item height
            item.maxStack = 1; //How many of this item you can stack
            item.toolTip = "Unlocked and loaded."; //The item’s tooltip
            item.useTime = 10; //How long it takes for the item to be used | 1
            item.useAnimation = 39; //How long the animation of the item takes | 2
            item.knockBack = 7f; //How much knockback the item produces
            item.useSound = 12; //The soundeffect played when used
            item.noMelee = true; //Whether the weapon should do melee damage or not
            item.useStyle = 5; //How the weapon is held, 5 is the gun hold style
            item.value = 1; //How much the item is worth
            item.rare = 1; //The rarity of the item
            item.shoot = mod.ProjectileType("keyproj"); //What the item shoots, retains an int value | 3
            item.shootSpeed = 8f; //How fast the projectile fires
            item.useAmmo = AmmoID.; //The item ammo ID that it will use
            item.autoReuse = true; //Whether it automatically uses the item again after its done being used/animated
        }
        public override void AddRecipes() //Adding recipes
        {
            ModRecipe recipe = new ModRecipe(mod); //Creating a new recipe to be added to
            recipe.AddIngredient(ItemID.DirtBlock); //Setting the ingredient to the item as a Dirt Block
            recipe.SetResult(this); //Set the result of the recipe to this item (this refers to the class itself)
            recipe.AddRecipe(); //Add this recipe
        }

        public class akey47 : GlobalItem
        {
            public override void SetDefaults(Item item)
    {
        if (item.type == ItemID.GoldenKey)
        {
            item.ammo = ItemID.GoldenKey;
            item.shoot = mod.ProjectileType("keyproj");
        }
        // and so on, for SilkRope and WebRope
    }
        }

    }

}

projectile


Code:
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using Terraria;
using Terraria.ModLoader;

namespace TiborsMod.Projectiles //We need this to basically indicate the folder where it is to be read from, so you the texture will load correctly
{
    public class keyproj : ModProjectile
    {
        public override void SetDefaults()
        {
            projectile.name = "keyproj"; //Name of the projectile, only shows this if you get killed by it
            projectile.width = 36; //Set the hitbox width
            projectile.height = 36; //Set the hitbox height
            projectile.timeLeft = 300; //The amount of time the projectile is alive for
            projectile.penetrate = 1; //Tells the game how many enemies it can hit before being destroyed
            projectile.friendly = true; //Tells the game whether it is friendly to players/friendly npcs or not
            projectile.hostile = false; //Tells the game whether it is hostile to players or not
            projectile.tileCollide = true; //Tells the game whether or not it can collide with a tile
            projectile.ignoreWater = true; //Tells the game whether or not projectile will be affected by water
            projectile.ranged = true; //Tells the game whether it is a ranged projectile or not
            projectile.aiStyle = 0; //How the projectile works, this is no AI, it just goes a straight path
        }
    }
}
 
How do I make it so that a custom projectile spawns upon another custom projectile's death? And is there a way to change the overall velocity of the projectile?
 
How do I make it so that a custom projectile spawns upon another custom projectile's death? And is there a way to change the overall velocity of the projectile?
It is something like:

public override void Kill()
{
(New projectile code)
}
[doublepost=1521401513,1521401397][/doublepost]I want to know how to make a weapon spawn a projectile that is a set distance away from the player, as well as 90 degrees from the degrees relation of the player and mouse pointer
 
It is something like:

public override void Kill()
{
(New projectile code)
}
[doublepost=1521401513,1521401397][/doublepost]I want to know how to make a weapon spawn a projectile that is a set distance away from the player, as well as 90 degrees from the degrees relation of the player and mouse pointer
Thanks for the reply, but I figured it out before you typed this. :)
Also, I don't the answer to your question, since I'm not very experienced, sorry. ^^;
Btw, how does one create a Shadowflame Hex Doll-like projectile?
 
Thanks for the reply, but I figured it out before you typed this. :)
Also, I don't the answer to your question, since I'm not very experienced, sorry. ^^;
Btw, how does one create a Shadowflame Hex Doll-like projectile?
Lol, i figured that out myself too.

Anyway, I've never tried this, but there is a projectile id (and subsequently ai type) for the shadowflame hex doll. You can probably use that on a custom projectile and make the weapon shoot it at a random angle.
 
Back
Top Bottom