tAPI [Tutorial] Projectile Guide and Implementation

If you wanna find the code to deal extra damage to enemies just download the Projectile Ai Styles File Sin Costan has and look for the steak. They deal extra damage 2 vampires so U can copy that code. Replace the id of the vampire and add your own Ai/vanilla Ai
 
If you wanna find the code to deal extra damage to enemies just download the Projectile Ai Styles File Sin Costan has and look for the steak. They deal extra damage 2 vampires so U can copy that code. Replace the id of the vampire and add your own Ai/vanilla Ai
After checking the AI Style list myself, the AI itself does not contain the code necessary for the damage multiplier, rather, it is in Damage function for the Projectile. It is, however basically set up the same as the code Berberborscing has set up.
 
Well I have been having some problems with my spear if you have been following along. Well guess what, I got it to work!:happy: Turns out it was namespace error. Total derp move on my part. I have one problem though, the starts offset. It starts out at the tip and as far out it goes as the middle. I need it to start at the middle and reach the handle at its peak. I just don't know how. Here's the code.
Code:
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using TAPI;
using Terraria;

namespace MaxsMod.Projectiles
{
    public class CopperLanceProj : ModProjectile
    {
        public override void AI()
        {
            projectile.light = 0.9f;
            int DustID = Dust.NewDust(new Vector2(projectile.position.X, projectile.position.Y + 2f), projectile.width, projectile.height, 24, projectile.velocity.X * 0.2f, projectile.velocity.Y * 0.2f, 100, default(Color), 0.5f);
            Main.player[projectile.owner].direction = projectile.direction; //Make's the owner face the direction the spear
            Main.player[projectile.owner].heldProj = projectile.whoAmI;
            Main.player[projectile.owner].itemTime = Main.player[projectile.owner].itemAnimation;
            projectile.position.X = Main.player[projectile.owner].position.X + (float)(Main.player[projectile.owner].width / 2) - (float)(projectile.width / 2);
            projectile.position.Y = Main.player[projectile.owner].position.Y + (float)(Main.player[projectile.owner].height / 2) - (float)(projectile.height / 2);
            projectile.position += projectile.velocity * projectile.ai[0];
            if (projectile.ai[0] == 0f)
            {
                projectile.ai[0] = 3f;
                projectile.netUpdate = true;
            }
            if (Main.player[projectile.owner].itemAnimation < Main.player[projectile.owner].itemAnimationMax / 3)
            {
                projectile.ai[0] -= 1.1f; //How far back it goes
                if (projectile.localAI[0] == 0f && Main.myPlayer == projectile.owner)
                {
                    projectile.localAI[0] = 1f;
                    if (Collision.CanHit(Main.player[projectile.owner].position, Main.player[projectile.owner].width, Main.player[projectile.owner].height, new Vector2(projectile.center().X + projectile.velocity.X * projectile.ai[0], projectile.center().Y + projectile.velocity.Y * projectile.ai[0]), projectile.width, projectile.height)) //The if statement to make a projectile after the spear point reaaches it's max distance; you can remove the if statements and the things it contains to make it a normal spear
                    {
                        Projectile.NewProjectile(projectile.center().X + projectile.velocity.X , projectile.center().Y + projectile.velocity.Y , projectile.velocity.X * 1.5f, projectile.velocity.Y * 1.5f, "COFP:MiniAncientSpear", projectile.damage , projectile.knockBack * 0.85f, projectile.owner, 0f, 0f); //Spawning the projectile
                    }
                }
            }
            else
            {
                projectile.ai[0] += 0.75f; //How Far It Goes
            }
            if (Main.player[projectile.owner].itemAnimation == 0)
            {
                projectile.Kill(); //Kills projectile after it is done
            }
            projectile.rotation = (float)Math.Atan2((double)projectile.velocity.Y, (double)projectile.velocity.X) + 2.355f; //Rotates the spear based on where you shoot
            if (projectile.spriteDirection == -1)
            {
                projectile.rotation -= 1.57f;
            }
        }
    }
}
All I need is a origin point readjust.
 
Last edited:
Wow this is really nice!

Now time to go make the gravity gun from Half Life...

MWHAHAHAHAHHA

EDIT: How do I change the colour of vanilla projectiles? Like changing the heat ray projectile to blue?
 
Last edited:
Wow this is really nice!

Now time to go make the gravity gun from Half Life...

MWHAHAHAHAHHA

EDIT: How do I change the colour of vanilla projectiles? Like changing the heat ray projectile to blue?
I think you have to create a whole new projectile :)
 
I think you have to create a whole new projectile :)
NOOO! Oh well... how would I make a projectile like the heat ray? Thanks!
 
NOOO! Oh well... how would I make a projectile like the heat ray? Thanks!
Isnt heat ray like projectiles listed on this tutorial?!?!
 
I tried building your code today, it tells me to add brackets and so I did but then this error appear,
Code:
Validating Jsons...
Compiling code...
GlassShard.cs  (33,24)
  'Terraria.Main' does not contain a definition for 'NPC'
         foreach (NPC N in Main.NPC)
                                ^
Failed to build TestMod.
Any clue?
Haven't tried the Projectile Ai Styles File yet, just having a glimpse of it already made me go @.@ So many questions I wanted to ask but feel uncomfortable in doing so. Why is it so hard to place furniture on tables properly!? etc etc. I really wish there's more guide like this, can't find the things that I wanted online.
 
I tried building your code today, it tells me to add brackets and so I did but then this error appear,
Code:
Validating Jsons...
Compiling code...
GlassShard.cs  (33,24)
  'Terraria.Main' does not contain a definition for 'NPC'
         foreach (NPC N in Main.NPC)
                                ^
Failed to build TestMod.
Any clue?
Haven't tried the Projectile Ai Styles File yet, just having a glimpse of it already made me go @.@ So many questions I wanted to ask but feel uncomfortable in doing so. Why is it so hard to place furniture on tables properly!? etc etc. I really wish there's more guide like this, can't find the things that I wanted online.
The "NPC" part of "Main.NPC" should be all lowercase instead. so it would be "Main.npc".
 
The "NPC" part of "Main.NPC" should be all lowercase instead. so it would be "Main.npc".
How would I make Vilethorn Like weapons?
 
how far out do I make a spear go in relation to the sprite size. Like _f for a sprite 22 by 22. I don't know what the number should be.
 
I have the very distinct feeling that this question has a very obvious answer, but how do you modify a projectile's velocity?
 
I have the very distinct feeling that this question has a very obvious answer, but how do you modify a projectile's velocity?
You would modify the projectile's velocity within the projectile's cs file (I guess you would already know that...). It all depends on how you want to modify the projectile... Do you want it to go faster as time goes? Do you want it to go slower as the projectile goes? Well here's an example of how you would do either of those.

Multiplying velocity
Code:
public override void AI()
{
       projectile.velocity *= 2;
}

Dividing Velocity
Code:
public override void AI()
{
       projectile.velocity /= 2;
}

Or if you want to have some sort of gravity effect...

Gravitizing Projectile
Code:
public override void AI()
{
       projectile.velocity.Y += 0.1f; //Positive goes downwards, and negative goes upwards, just how Terraria handles Y coordinates.
}
[CODE]

Of course there are other ways you can manipulate projectile's velocity through AI, but these are the most simple ones.

how far out do I make a spear go in relation to the sprite size. Like _f for a sprite 22 by 22. I don't know what the number should be.

Generally I just guess and check, I wouldn't really know of a general formula for spear relation, but I might check that out later.

How would I make Vilethorn Like weapons?

Generally, it has to do with using the projectile ai functions and using the Projectile.NewProjectile() function.
 
Last edited:
You would modify the projectile's velocity within the projectile's cs file (I guess you would already know that...). It all depends on how you want to modify the projectile... Do you want it to go faster as time goes? Do you want it to go slower as the projectile goes? Well here's an example of how you would do either of those.

Multiplying velocity
Code:
public override void AI()
{
       projectile.velocity *= 2;
}

Dividing Velocity
Code:
public override void AI()
{
       projectile.velocity /= 2;
}

Or if you want to have some sort of gravity effect...

Gravitizing Projectile
Code:
public override void AI()
{
       projectile.velocity.Y += 0.1f; //Positive goes downwards, and negative goes upwards, just how Terraria handles Y coordinates.
}
[CODE]

Of course there are other ways you can manipulate projectile's velocity through AI, but these are the most simple ones.
You misunderstand. I want the projectile to have a constant velocity, but I want that velocity to be faster. (This is the kind of thing that I expected to have a .json attribute, but I can't find any that correspond to velocity.)
 
You misunderstand. I want the projectile to have a constant velocity, but I want that velocity to be faster. (This is the kind of thing that I expected to have a .json attribute, but I can't find any that correspond to velocity.)
What do you mean by constant and faster?
 
What do you mean by constant and faster?
Right now, my projectile travels X speed at all times. I, instead, want it to travel X + 2 speed at all times.

By constant, I mean that I want it to travel at the same speed through the air at all times.

By faster, I mean that when I fire it, it's moving slower than I want it to move at all times.
 
Right now, my projectile travels X speed at all times. I, instead, want it to travel X + 2 speed at all times.

By constant, I mean that I want it to travel at the same speed through the air at all times.

By faster, I mean that when I fire it, it's moving slower than I want it to move at all times.
Never mind, I was being a scrub, and I figured out how to change it. There was, in fact, a very obvious answer - I had to change "shootSpeed" in the ammo's .json.
 
Looking for some help with creating a projectile sword.
I checked all the .json files on jsonlint and no errors were found.
Essentially, the problem is that the sword itself does damage, but it does not shoot a projectile and there is no projectile damage.
I'm aiming for a Terra Blade style projectile that simply shoots a sword in a straight line that does not require mana or ammo.

Code for ModInfo:
Code:
{
   "displayName": "Weapons",
   "author": "Someone",
   "info": "Testing certain equipment",
   "version": "0.1.0",
   "internalName": "Mod",
   "includePDB": true
}

Code for weapon:
Code:
{
   "displayName": "Swordzee",
   "texture": "Items/Swordzee",
   "size": [60, 60],
   "maxStack": 1,
   "value": [0, 0, 0, 2],
   "rare": 9,
   "tooltip": "Creating a projectile sword",
   "useStyle": 1,
   "useAnimation": 10,
   "useTime": 10,
   "damage": 100,
   "knockback": 9,
   "crit": 25,
   "useSound": 1,
   "autoReuse": true,
   "useTurn": true,
   "melee": true,
   "range": true,
   "shoot": "Mod:SwordzeeProj",
   "shootSpeed": 8,
   "recipes": [{
     "items":{ "Dirt Block": 1 },
     "tiles":[ "Work Bench"],
     "creates": 1
   }]
}

Code for projectile:
Code:
{
  "displayName": "SwordzeeBeam",
  "size": [28,84],
  "scale": 1,
  "aiStyle": 0,
  "timeLeft": 3600,
  "friendly": true,
  "hostile": false,
  "tileCollide": false,
  "penetrate": -1,
  "ranged": true
}
 
Last edited:
Looking for some help with creating a projectile sword.
I checked all the .json files on jsonlint and no errors were found.
Essentially, the problem is that the sword itself does damage, but it does not shoot a projectile and there is no projectile damage.
I'm aiming for a Terra Blade style projectile that simply shoots a sword in a straight line that does not require mana or ammo.

Code for ModInfo:
Code:
{
   "displayName": "Weapons",
   "author": "Someone",
   "info": "Testing certain equipment",
   "version": "0.1.0",
   "internalName": "Mod",
   "includePDB": true
}

Code for weapon:
Code:
{
   "displayName": "Swordzee",
   "texture": "Items/Swordzee",
   "size": [60, 60],
   "maxStack": 1,
   "value": [0, 0, 0, 2],
   "rare": 9,
   "tooltip": "Creating a projectile sword",
   "useStyle": 1,
   "useAnimation": 10,
   "useTime": 10,
   "damage": 100,
   "knockback": 9,
   "crit": 25,
   "useSound": 1,
   "autoReuse": true,
   "useTurn": true,
   "melee": true,
   "range": true,
   "shoot": "Mod:SwordzeeProj",
   "shootSpeed": 8,
   "recipes": [{
     "items":{ "Dirt Block": 1 },
     "tiles":[ "Work Bench"],
     "creates": 1
   }]
}

Code for projectile:
Code:
{
  "displayName": "SwordzeeBeam",
  "size": [28,84],
  "scale": 1,
  "aiStyle": 0,
  "timeLeft": 3600,
  "friendly": true,
  "hostile": false,
  "tileCollide": false,
  "penetrate": -1,
  "ranged": true
}

Images of file locations:

Well, for starters, you don't necessarily need the "texture" code anymore, as long as all the files have the same name, tAPI compiles them without the need for the "texture" code. I think you were trying to use "ranged" but wrote "range" instead. Other than those minor things, it looks like it should work. Do you think you can send me your source folder through conversation? I want to check this myself.
 
It still doesn't shoot a projectile.
I originally thought the problem was because the projectile lacked the weapon attributes like damage, but I'm fairly certain it copies it from the sword itself.

*Edit*
It works now, the folder being named "Projectile" instead of "Projectiles" was the issue.
 
Last edited:
Back
Top Bottom