• For issues you find with the Switch and Console releases, please follow this link and give as much detail as possible. This is the speediest way to get info to Pipeworks and get a hotfix in the works.
  • 1.4 will bring many changes to the PC version. We strongly advise making plans to back up your worlds and players prior to updating your game. More details here.
  • Begin your search for Journey's End information here and here. Please report bugs and issues for Journey's End here.
  • For issues you find with the Mobile 1.3 update, please follow this link and give as much detail as possible. This is the speediest way to get info to DR Studios to look at your issue. Also, some troubleshooting hints here.

tModLoader Official tModLoader Help Thread

Solo-Ion

Dungeon Spirit
I will try, thank you.

I have another question.

How to make a static projectile position? (No curve movements) I mean, I have the X and Y positions in the same degrees, but when I change either, when I shoot, the direction moves like an arc. I use the "muzzleOffset" code from "ExampleMod > ExampleGun.cs"

I hope you (and readers) understand me.
The behaviour of the projectile after it has been shot is defined by the projectile itself, not the item that shoots it. You'll want to shoot a projectile that has the behaviour that you want.
 

a1b2c41

Official Terrarian
Hey guys i have a question. I believe the reason I can't answer this on my own is because my first projectile was a homing projectile and now i want to make a projectile that is similar to the Ice Blade's projectile, one that fires in a straight line. I feel stupid not being able to do this. Does anyone have a good tutorial or some code they could share?
 

Eldrazi

Eater of Worlds
Hey guys i have a question. I believe the reason I can't answer this on my own is because my first projectile was a homing projectile and now i want to make a projectile that is similar to the Ice Blade's projectile, one that fires in a straight line. I feel stupid not being able to do this. Does anyone have a good tutorial or some code they could share?
So you are able to create a homing projectile, but not a projectile that goes in a straight line...? :p
If you want the projectile to go in a straight line, then do... Nothing! Just leave the aiStyle and all AI methods empty.
 

a1b2c41

Official Terrarian
So you are able to create a homing projectile, but not a projectile that goes in a straight line...? :p
If you want the projectile to go in a straight line, then do... Nothing! Just leave the aiStyle and all AI methods empty.
Hey, i took out almost all the AI and the projectile doesn't home in on enemies but it doesn't move either. I'm not sure if i have to apply a force on it somehow but I can show you my code below in the spoilers. My projectile lights up, emits dust, and steals life on contact but it doesn't move. Can i get a hand please?

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

namespace MTGMod.Projectiles
{

public class FeastAndFamine : ModProjectile
{
int Counter = 0;

public override void SetDefaults()
{
projectile.name = "FeastAndFamine";
projectile.width = 20;
projectile.height = 20;
projectile.alpha = 0;
projectile.friendly = true;
projectile.tileCollide = true;
projectile.ignoreWater = true;
projectile.ranged = true;
projectile.timeLeft = 500;
}

public override void AI()
{
projectile.timeLeft -= 1;

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].heldProj = projectile.whoAmI;
Main.player[projectile.owner].itemTime = Main.player[projectile.owner].itemAnimation;
if (projectile.alpha <= 100)
{
int dust = Dust.NewDust(projectile.position, projectile.width, projectile.height, mod.DustType("FeastAndFamine"));
Main.dust[dust].velocity /= 2f;
}
if (projectile.timeLeft == 0)
{
projectile.active = false;
}
}
public void DealtNPC(Player player, NPC npc, double damage)
{
player.statLife += (int)damage/10;
}
public override void OnHitNPC(NPC target, int damage, float knockback, bool crit)
{
if (Main.rand.Next(2) == 0)
{
projectile.active = false;
}
}
}
}

And the code for the sword which fires it

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

namespace MTGMod.Items
{
public class SwordOfFeastAndFamine : ModItem
{
public override void SetDefaults()
{
item.name = "Sword of Feast and Famine";
item.damage = 45;
item.melee = true;
item.width = 80;
item.height = 80;
item.toolTip = "Why doesn't this have flavor text";
item.useTime = 15;
item.useAnimation = 15;
item.useStyle = 1;
item.knockBack = 3;
item.value = 100000;
item.rare = 4;
item.UseSound = SoundID.Item7;
item.autoReuse = true;
item.shoot = mod.ProjectileType("FeastAndFamine");
}

public override void AddRecipes()
{
ModRecipe recipe = new ModRecipe(mod);
recipe.AddIngredient(null, "GreenManaOrb", 500);
recipe.AddIngredient(null, "BlackManaOrb", 500);
recipe.AddIngredient(null, "TemplateSword", 1);
recipe.AddTile(null, "DarksteelForge");
recipe.SetResult(this);
recipe.AddRecipe();
}
public override void MeleeEffects(Player player, Rectangle hitbox)
{
if (Main.rand.Next(1) == 0)
{
int dust = Dust.NewDust(new Vector2(hitbox.X, hitbox.Y), hitbox.Width, hitbox.Height, mod.DustType("FeastAndFamine"));
}
}
}
}

I'm not sure if its something in the sword code i should change or the projectile code. Anyway I just want it to fly a straight line towards the direction of the cursor(but go past the cursor). Also i'm not sure if this matters but my sprite is mirrored so i can use rotation to change the direction it points but im not sure how i would tell it to point the direction it's going.
Sorry if this seems like a lot but for some reason I can't find any good tutorials and the ones I can find are outdated, for tconfig.
 

Eldrazi

Eater of Worlds
Hey, i took out almost all the AI and the projectile doesn't home in on enemies but it doesn't move either. I'm not sure if i have to apply a force on it somehow but I can show you my code below in the spoilers. My projectile lights up, emits dust, and steals life on contact but it doesn't move. Can i get a hand please?

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

namespace MTGMod.Projectiles
{

public class FeastAndFamine : ModProjectile
{
int Counter = 0;

public override void SetDefaults()
{
projectile.name = "FeastAndFamine";
projectile.width = 20;
projectile.height = 20;
projectile.alpha = 0;
projectile.friendly = true;
projectile.tileCollide = true;
projectile.ignoreWater = true;
projectile.ranged = true;
projectile.timeLeft = 500;
}

public override void AI()
{
projectile.timeLeft -= 1;

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].heldProj = projectile.whoAmI;
Main.player[projectile.owner].itemTime = Main.player[projectile.owner].itemAnimation;
if (projectile.alpha <= 100)
{
int dust = Dust.NewDust(projectile.position, projectile.width, projectile.height, mod.DustType("FeastAndFamine"));
Main.dust[dust].velocity /= 2f;
}
if (projectile.timeLeft == 0)
{
projectile.active = false;
}
}
public void DealtNPC(Player player, NPC npc, double damage)
{
player.statLife += (int)damage/10;
}
public override void OnHitNPC(NPC target, int damage, float knockback, bool crit)
{
if (Main.rand.Next(2) == 0)
{
projectile.active = false;
}
}
}
}

And the code for the sword which fires it

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

namespace MTGMod.Items
{
public class SwordOfFeastAndFamine : ModItem
{
public override void SetDefaults()
{
item.name = "Sword of Feast and Famine";
item.damage = 45;
item.melee = true;
item.width = 80;
item.height = 80;
item.toolTip = "Why doesn't this have flavor text";
item.useTime = 15;
item.useAnimation = 15;
item.useStyle = 1;
item.knockBack = 3;
item.value = 100000;
item.rare = 4;
item.UseSound = SoundID.Item7;
item.autoReuse = true;
item.shoot = mod.ProjectileType("FeastAndFamine");
}

public override void AddRecipes()
{
ModRecipe recipe = new ModRecipe(mod);
recipe.AddIngredient(null, "GreenManaOrb", 500);
recipe.AddIngredient(null, "BlackManaOrb", 500);
recipe.AddIngredient(null, "TemplateSword", 1);
recipe.AddTile(null, "DarksteelForge");
recipe.SetResult(this);
recipe.AddRecipe();
}
public override void MeleeEffects(Player player, Rectangle hitbox)
{
if (Main.rand.Next(1) == 0)
{
int dust = Dust.NewDust(new Vector2(hitbox.X, hitbox.Y), hitbox.Width, hitbox.Height, mod.DustType("FeastAndFamine"));
}
}
}
}

I'm not sure if its something in the sword code i should change or the projectile code. Anyway I just want it to fly a straight line towards the direction of the cursor(but go past the cursor). Also i'm not sure if this matters but my sprite is mirrored so i can use rotation to change the direction it points but im not sure how i would tell it to point the direction it's going.
Sorry if this seems like a lot but for some reason I can't find any good tutorials and the ones I can find are outdated, for tconfig.
On your sword you have to set item.shootSpeed otherwise the projectile won't have an initial speed.
Try some values to see what fits your taste ( item.shootSpeed = 5; is rather slow, whereas item.shootSpeed = 14; can be seen as rather fast ).
 

G4M3R57

Terrarian
The behaviour of the projectile after it has been shot is defined by the projectile itself, not the item that shoots it. You'll want to shoot a projectile that has the behaviour that you want.
Man, I know that, I need something like a piece of code.
But thank you for responding!
 

DeAuTh1511

Terrarian
Is there a guide to browsing the vanilla source code? I am using ILSpy but even finding simple things is nearly impossible for me. For example, the North Pole is a spear weapon that shoots a projectile, so should produce a spear weapon "projectile" that penetrates the ground etc. and several snowflake projectiles over its path, both of which should be calculated, and the accompanying sprites. Yet the only code I can find describes none of these actions happening, merely setting variables (damage, speed, etc.) in SetDefaults2() in the Item class.

Basically, I want to know where to locate the code behind the variables that makes the magic happen so I can more easily implement classes that use similar code to vanilla items. If someone could give me code locations for one item in particular, such as for the "North Star" spear or any item/weapon with unique effects, that would be great.
 

Eldrazi

Eater of Worlds
Is there a guide to browsing the vanilla source code? I am using ILSpy but even finding simple things is nearly impossible for me. For example, the North Pole is a spear weapon that shoots a projectile, so should produce a spear weapon "projectile" that penetrates the ground etc. and several snowflake projectiles over its path, both of which should be calculated, and the accompanying sprites. Yet the only code I can find describes none of these actions happening, merely setting variables (damage, speed, etc.) in SetDefaults2() in the Item class.

Basically, I want to know where to locate the code behind the variables that makes the magic happen so I can more easily implement classes that use similar code to vanilla items. If someone could give me code locations for one item in particular, such as for the "North Star" spear or any item/weapon with unique effects, that would be great.
The problem with this is that the code for a lot of functionality is located over several files.
In your case: the item file contains the information (damage, name, etc), the Projectile.cs file contains the AI for the spear projectile and the item is used in the Player.cs file (where you can find hoe it's shot).
 
Last edited:

DeAuTh1511

Terrarian
The problem with this is that the code for a lot of functionality is located over several files.
In your case: the item file contains the information (damage, name, etc), the Projectile.cs file contains the AI for the spear projectile and the item is used in the Player.cs file (where you can find hoe it's shot).
Thank you for the help! It took some digging but I found exactly what I was looking for split painfully across several files.

Would you be able to help me with this question too? I see references to "base" (as in "base.Center.X") in Terraria.player and it seems to be integral to many Magic and Ranged weapon projectile algorithms - only problem is it cannot be accessed through a mod item/weapon ("'ModItem' does not contain a definition for 'Center') and I have no idea how to figure out what it does in order to replace it with valid code. Is there a way to find out what this 'base' refers to and also if I come across something similar in the future?
 

Eldrazi

Eater of Worlds
Thank you for the help! It took some digging but I found exactly what I was looking for split painfully across several files.

Would you be able to help me with this question too? I see references to "base" (as in "base.Center.X") in Terraria.player and it seems to be integral to many Magic and Ranged weapon projectile algorithms - only problem is it cannot be accessed through a mod item/weapon ("'ModItem' does not contain a definition for 'Center') and I have no idea how to figure out what it does in order to replace it with valid code. Is there a way to find out what this 'base' refers to and also if I come across something similar in the future?
(I'm on mobile, so forgive any errors)

This has to do with classes deriving from one another. Deriving can basically be seen as making one class a 'child' of another one, inheriting all data from that class but also able to add additional functionality.
In the case of the vanilla Player class, the base refers to the Entity class (as you can see where the class is declared public class Player : Entity).
This Entity class basically contains some variables that are used die every type of Entity (players, NPCs, projectiles and items). These are things like position, center, Hitbox, etc.
When you look at vanilla code, you often see two pointers: this and base. this refers to the current class (Player) and base to the patent class (Entity). So when you have a ModItem called 'MyItem' (for example) this refers to MyItem whereas base refers to ModItem (class MyItem : ModItem).
So when you encounter instances of base pointers, you can almost always replace them with the type of entity you're working with. (projectile on ModProjectiles, item on ModItems etc).

I hope I was clear enough with my explanation!
 

Koala

Terrarian
Are tmodloader have bug about camera mode? Because, I use tmodloader today and take pic on my build but it was pure black even though there are torch everywhere. Then the second time the game crash! Please help me :(. Note: If it is a bug I will not put the crash log
 

a1b2c41

Official Terrarian
Hey, anyone know a line of code to make a projectile heal the player 10% of the damage done (like vampire knives)?
 

Eldrazi

Eater of Worlds
Hey, anyone know a line of code to make a projectile heal the player 10% of the damage done (like vampire knives)?
On your projectile override the OnHitNPC method and then add damage/10 to Main.player[projectile.owner].statLife.
 

DeAuTh1511

Terrarian
(I'm on mobile, so forgive any errors)

This has to do with classes deriving from one another. Deriving can basically be seen as making one class a 'child' of another one, inheriting all data from that class but also able to add additional functionality.
In the case of the vanilla Player class, the base refers to the Entity class (as you can see where the class is declared public class Player : Entity).
This Entity class basically contains some variables that are used die every type of Entity (players, NPCs, projectiles and items). These are things like position, center, Hitbox, etc.
When you look at vanilla code, you often see two pointers: this and base. this refers to the current class (Player) and base to the patent class (Entity). So when you have a ModItem called 'MyItem' (for example) this refers to MyItem whereas base refers to ModItem (class MyItem : ModItem).
So when you encounter instances of base pointers, you can almost always replace them with the type of entity you're working with. (projectile on ModProjectiles, item on ModItems etc).

I hope I was clear enough with my explanation!
Thank you, this makes perfect sense!
 
Last edited:

trinomials

Terrarian
yes
[doublepost=1486825398,1486824974][/doublepost]This is my code
Code:
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Terraria;
using Terraria.ModLoader;
using Terraria.ID;

namespace test.Projectiles
{
    class BetterTerraBeam : ModProjectile
    {
        public override void SetDefaults()
        {
            projectile.name = "Terrabeam of Death";
            aiType = ProjectileID.TerraBeam;
            projectile.width = 10;       //projectile width
            projectile.height = 18;  //projectile height
            projectile.friendly = true;      //make that the projectile will not damage you
            projectile.melee = true;         //
            projectile.tileCollide = true;   //make that the projectile will be destroed if it hits the terrain
            projectile.penetrate = 2;      //how many npc will penetrate
            projectile.timeLeft = 200;   //how many time this projectile has before disepire
            projectile.light = 0.75f;    // projectile light
            projectile.extraUpdates = 1;
            projectile.ignoreWater = true;

        }



        public override void OnHitNPC(NPC target, int damage, float knockback, bool crit)
        {
            target.AddBuff(BuffID.OnFire, 5 * 60);
        }
        public override void AI(){
             projectile.rotation = (float)Math.Atan2((double)projectile.velocity.Y, (double)projectile.velocity.X) + 1.57f; 
              int DustID = Dust.NewDust(new Vector2(projectile.position.X, projectile.position.Y + 2f), projectile.width + 4, projectile.height + 4, 36, projectile.velocity.X * 0.2f, projectile.velocity.Y * 0.2f, 120, default(Color), 0.75f); //Spawns dust
            Main.dust[DustID].noGravity = true;
            Lighting.AddLight(this.position,1,1,1);
        }           
    }
}
[doublepost=1486825459][/doublepost]i am getting the error message
" error CS1061: 'test.Projectiles.BetterTerraBeam' does not contain a definition for 'position' and no extension method 'position' accepting a first argument of type 'test.Projectiles.BetterTerraBeam' could be found (are you missing a using directive or an assembly reference?)"
 

dtim

Terrarian
how do I make a debuff that negates regen and decreases stats by 5%
You'll probably need to use a ModPlayer and set a bool variable in the debuff's Update method. Then check for that bool in several ModPlayer methods. Don't forget to reset the bool variable in ResetEffects or it won't ever wear off.
For the regen, see ModPlayer.UpdateBadLifeRegen
For stats, that's a bit more vague because there are a bunch of them. Probably the easiest thing to do is put some of the code ModPlayer.PostUpdate
Change the main stats like max health and mana there with
Code:
player.statLifeMax2 = (int)(player.statLifeMax2 * 0.95f);
player.statManaMax2 = (int)(player.statManaMax2 * 0.95f);
player.statDefense = (int)(player.statDefense * 0.95f);
There are a ton of other things like mana regen, movement speed, various crit chances, etc. You'll need to figure out what you want to alter.
And finally use ModPlayer.GetWeaponDamage to lower the player's damage by 5% rather than try to lower all the various damage stats.
[doublepost=1486826454,1486826115][/doublepost]
yes
[doublepost=1486825398,1486824974][/doublepost]This is my code
Code:
<snip>
            Lighting.AddLight(this.position,1,1,1);
        }         
    }
}
[doublepost=1486825459][/doublepost]i am getting the error message
" error CS1061: 'test.Projectiles.BetterTerraBeam' does not contain a definition for 'position' and no extension method 'position' accepting a first argument of type 'test.Projectiles.BetterTerraBeam' could be found (are you missing a using directive or an assembly reference?)"
You need "projectile.position" instead of "this.position". To change the color, the three arguments after the position indicate red, green, and blue in a scale from 0 to 1. So all 1's will be a white light. If you set the first two to 0.5f then it'll be a light blue.
 
Top Bottom