• 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

Hallam_9K

Spazmatism
Help, how do I make my ore to generate only in the Underground Jungle?
Also, how do I make it shine when I use a spelunker potion?
Also, also, how do I make the ore show up on the mini-map?

Code:
using System.IO;
using System.Collections.Generic;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;
using Terraria.World.Generation;
using Microsoft.Xna.Framework;
using Terraria.GameContent.Generation;

namespace ModOfRandomness
{
    public class ModWord : ModWorld
    {
        private const int saveVersion = 0;
        public override void ModifyWorldGenTasks(List<GenPass> tasks, ref float totalWeight)
        {
            int ShiniesIndex = tasks.FindIndex(genpass => genpass.Name.Equals("Shinies"));
            if (ShiniesIndex == -1)
            {
                return;
            }
            tasks.Insert(ShiniesIndex + 1, new PassLegacy("Custom Mod Ores", delegate (GenerationProgress progress)
            {
                progress.Message = "Custom Mod Ores";
                                                                                                                                                                                                                                         //Put your custom tile block name
                for (int k = 0; k < (int)((double)(Main.maxTilesX * Main.maxTilesY) * 6E-05); k++)                                                                                                                                      //      |
                {                                                                                                                                                                                                                      //       |
                    WorldGen.TileRunner(WorldGen.genRand.Next(0, Main.maxTilesX), WorldGen.genRand.Next((int)WorldGen.worldSurfaceLow, Main.maxTilesY), (double)WorldGen.genRand.Next(3, 6), WorldGen.genRand.Next(2, 6), mod.TileType("NoobiteFossilBlock"), false, 0f, 0f, false, true);
                }
            }));
        }

    }
}

Code:
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;

namespace ModOfRandomness.Items.Placeable
{
    public class NoobiteFossil : ModItem
    {
        public override void SetDefaults()
        {
            item.name = "Noobite Fossil";
            item.width = 16;
            item.height = 16;
            item.maxStack = 999;
            AddTooltip("The remains of an extinct race.");
            item.useTurn = true;
            item.autoReuse = true;
            item.useAnimation = 15;
            item.useTime = 10;
            item.useStyle = 1;
            item.consumable = true;
            item.createTile = mod.TileType("NoobiteFossilBlock"); //put your CustomBlock Tile name
        }
    }
}

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

namespace ModOfRandomness.Tiles
{
    public class NoobiteFossilBlock : ModTile
    {
        public override void SetDefaults()
        {
            Main.tileSolid[Type] = true;
            Main.tileMergeDirt[Type] = true;
            drop = mod.ItemType("NoobiteFossil");   //put your CustomBlock name
            minPick = 60;
            mineResist = 3f;
        }
    }
}
 

Marcin JOT.PE

Terrarian
Hey everyone i try to make npc like Deadly Sphere and i found code from terraria can someone explain of help how to get this to work

Code:
float num4 = 1f;
                bool flag2 = false;
                if (velocity.X != vector2_2.X)
                {
                    velocity.X = vector2_2.X * -num4;
                    flag2 = true;
                }
                if (velocity.Y != vector2_2.Y || velocity.Y == 0.0)
                {
                    velocity.Y = (float)(vector2_2.Y * -num4 * 0.5);
                    flag2 = true;
                }
                if (flag2)
                {
                    float num5 = vector2_2.Length() / velocity.Length();
                    if (num5 == 0.0)
                        num5 = 1f;
                    Projectile projectile = this;
                    Vector2 vector2_3 = projectile.velocity / num5;
                    projectile.velocity = vector2_3;
                    if (ai[0] == 7.0 && velocity.Y < -0.1)
                        velocity.Y += 0.1f;
                    if (ai[0] >= 6.0 && ai[0] < 9.0)
                        Collision.HitTiles(position, vector2_2, width, height);
               
            }
 

VampireBagel

Terrarian
hey every one, right now im making a torch that works under water, but when its placed all non solid tiles nearby go invisible ( non soild blocks being other torches, work benches, tall grass, tomb stones) this is my current code, if any one knows whats causing this please let me know

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Terraria;
using Terraria.DataStructures;
using Terraria.Enums;
using Terraria.ID;
using Terraria.ModLoader;
using Terraria.ObjectData;

namespace Traces.tiles
{
public class SealedTorch : ModTile
{
public override void SetDefaults()
{
Main.tileLighted[Type] = true;
Main.tileFrameImportant[Type] = true;
Main.tileSolid[Type] = false;
Main.tileNoAttach[Type] = true;
Main.tileNoFail[Type] = true;
Main.tileWaterDeath[Type] = false;
TileObjectData.newTile.CopyFrom(TileObjectData.StyleTorch);
TileObjectData.newTile.AnchorBottom = new AnchorData(AnchorType.SolidTile | AnchorType.SolidSide, TileObjectData.newTile.Width, 0);
TileObjectData.newAlternate.CopyFrom(TileObjectData.StyleTorch);
TileObjectData.newAlternate.AnchorLeft = new AnchorData(AnchorType.SolidTile | AnchorType.SolidSide | AnchorType.Tree | AnchorType.AlternateTile, TileObjectData.newTile.Height, 0);
TileObjectData.newAlternate.AnchorAlternateTiles = new int[]{ 124 };
TileObjectData.addAlternate(1);
TileObjectData.newAlternate.CopyFrom(TileObjectData.StyleTorch);
TileObjectData.newAlternate.AnchorRight = new AnchorData(AnchorType.SolidTile | AnchorType.SolidSide | AnchorType.Tree | AnchorType.AlternateTile, TileObjectData.newTile.Height, 0);
TileObjectData.newAlternate.AnchorAlternateTiles = new int[]{ 124 };
TileObjectData.addAlternate(2);
TileObjectData.newAlternate.CopyFrom(TileObjectData.StyleTorch);
TileObjectData.newAlternate.AnchorWall = true;
TileObjectData.addAlternate(0);
TileObjectData.addTile(Type);
AddToArray(ref TileID.Sets.RoomNeeds.CountsAsTorch);
AddMapEntry(new Color(200, 200, 200), "Torch");
dustType = mod.DustType("Sparkle");
drop = mod.ItemType("SealedTorch");
disableSmartCursor = true;
adjTiles = new int[]{ TileID.Torches };
torch = true;
}

public override void NumDust(int i, int j, bool fail, ref int num)
{
num = Main.rand.Next(1, 3);
}

public override void ModifyLight(int i, int j, ref float r, ref float g, ref float b)
{
Tile tile = Main.tile[i, j];
if (tile.frameX < 66)
{
//r = 0.9f;
g = 4f;
b = 1f;
}
}

public override void SetDrawPositions(int i, int j, ref int width, ref int offsetY, ref int height)
{
offsetY = 0;
if (WorldGen.SolidTile(i, j - 1))
{
offsetY = 2;
if (WorldGen.SolidTile(i - 1, j + 1) || WorldGen.SolidTile(i + 1, j + 1))
{
offsetY = 4;
}
}
}

public override void PostDraw(int i, int j, SpriteBatch spriteBatch)
{
ulong randSeed = Main.TileFrameSeed ^ (ulong)((long)j << 32 | (long)((ulong)i));
Color color = new Color(100, 100, 100, 0);
int frameX = Main.tile[i, j].frameX;
int frameY = Main.tile[i, j].frameY;
int width = 20;
int offsetY = 0;
int height = 20;
if (WorldGen.SolidTile(i, j - 1))
{
offsetY = 2;
if (WorldGen.SolidTile(i - 1, j + 1) || WorldGen.SolidTile(i + 1, j + 1))
{
offsetY = 4;
}
}
Vector2 zero = new Vector2(Main.offScreenRange, Main.offScreenRange);
if (Main.drawToScreen)
{
zero = Vector2.Zero;
}
for (int k = 0; k < 7; k++)
{
float x = (float)Utils.RandomInt(ref randSeed, -10, 11) * 0.15f;
float y = (float)Utils.RandomInt(ref randSeed, -10, 1) * 0.35f;
Main.spriteBatch.Draw(mod.GetTexture("Tiles/SealedTorch_Flame"), new Vector2((float)(i * 16 - (int)Main.screenPosition.X) - (width - 16f) / 2f + x, (float)(j * 16 - (int)Main.screenPosition.Y + offsetY) + y) + zero, new Rectangle(frameX, frameY, width, height), color, 0f, default(Vector2), 1f, SpriteEffects.None, 0f);
}
}
}
}
 

AaroSA

Cultist
Trying to play modded multiplayer with a friend, but the world crashes soon after he joins. The mods we are using are Prefixes for enemies and Thorium. All are updated to their newest version. This is the error message I receive, translated from Finnish to English:

The given key was not present in the dictionary.
in location System.Collections.Generic.Dictionary`2.get_Item(TKey key)
in location Terraria.GameInput.KeyConfiguration.CopyKeyState(TriggersSet oldSet, TriggersSet newSet, String newKey)
in location Terraria.GameInput.PlayerInput.KeyboardInput()
in location Terraria.GameInput.PlayerInput.UpdateInput()
in location Terraria.Main.DoUpdate(GameTime gameTime)
in location Terraria.Main.Update(GameTime gameTime)
 

Will A

Terrarian
Snippet: 'How does the velocity system work?'



Guide: How to use and the uses of the Terraria.ID namespace

Terraria.ID is a really nice namespace that contains virtually all vanilla ID's for you to use. You need an item id? Sure! You need a projectile id? Sure! All you need to do is add the following to the top of your file:
Code:
using Terraria.ID;
Next, you can use all of these:
Code:
AchievementHelperID
AnimationID
BuffID
ChainID
Colors
DustID
ExtrasID
GlowMaskID
GoreID
InvasionID
ItemID
MessageID
MountID
NPCID
PlayerTextureID
PlayerVariantID
ProjectileID
SetFactory
StatusID
TileEntityID
TileID
WallID
For example, to get the ID of a Cutlass: (672)
Code:
ItemID.Cutlass
To get the ID of a Demon Scythe projectile: (45)
Code:
ProjectileID.DemonScythe
If you've set up your mod environment using an IDE, such as MVS, (you can use my tutorial) then IntelliSense will help you a lot like this:
View attachment 100565
IntelliSense will show you what you can access, as well as what it is and does etc.

Wow thank you for helping me and everyone else with this. I have one question though. I'm trying to create a mod which includes a new bar. I've been messing around with the ExamplePlatform and ExampleBlock "Tile" and "Placeable" codes, yet I haven't figured out how to get the bar to place on the ground like other bars. I've mostly gotten it down, but I can't answer two problems:

1. The bars can be placed horizontally without any vertical support. (Normal bars will break if they're placed above another bar and the bar below it breaks.)

2. The bars can't shine and emit a dust when not broken. (The ExampleBlock emits a dust when someone breaks it.)

Could you help me please?

Thanks.
 

Hallam_9K

Spazmatism
How do you make the player get a custom debuff when hit by a projectile and when hit by an NPC?
And, if possible, how do I make it so the player gets the debuff just by holding an item?
 

FoxyFullmoon

Terrarian
I'm having a problem where my bow says it doesn't have any ammo. I've tried setting useAmmo to ProjectileID.WoodenArrowFriendly and to 1, but neither work. What am I doing wrong?

Here's my code.

Code:
public class BowPrism : ModItem
    {
        public override void SetDefaults()
        {
            item.name = "Prism Bow";
            item.damage = 25;
            item.ranged = true;
            item.width = 16;
            item.height = 32;
            item.useTime = 20;
            item.useAnimation = 20;
            item.useStyle = 5;
            item.noMelee = true;
            item.knockBack = 1;
            item.value = 3000;
            item.UseSound = SoundID.Item5;
            item.autoReuse = true;
            item.shootSpeed = 10;
            item.shoot = 1;
            item.useAmmo = ProjectileID.WoodenArrowFriendly;
            item.crit = 50;
        }

        public override void AddRecipes()
        {
            ModRecipe recipe = new ModRecipe(mod);
            recipe.AddIngredient(ItemID.Ruby, 2);
            recipe.AddIngredient(ItemID.Amber, 2);
            recipe.AddIngredient(ItemID.Topaz, 2);
            recipe.AddIngredient(ItemID.Emerald, 2);
            recipe.AddIngredient(ItemID.Sapphire, 2);
            recipe.AddIngredient(ItemID.Amethyst, 2);
            recipe.AddIngredient(ItemID.Diamond, 2);
            recipe.AddIngredient(ItemID.IronBar, 2);
            recipe.AddTile(TileID.Anvils);
            recipe.SetResult(this);
            recipe.AddRecipe();
        }
    }
 

Ivysaur

Official Terrarian
I'm having a problem where my bow says it doesn't have any ammo. I've tried setting useAmmo to ProjectileID.WoodenArrowFriendly and to 1, but neither work. What am I doing wrong?

Here's my code.

Code:
public class BowPrism : ModItem
    {
        public override void SetDefaults()
        {
            item.name = "Prism Bow";
            item.damage = 25;
            item.ranged = true;
            item.width = 16;
            item.height = 32;
            item.useTime = 20;
            item.useAnimation = 20;
            item.useStyle = 5;
            item.noMelee = true;
            item.knockBack = 1;
            item.value = 3000;
            item.UseSound = SoundID.Item5;
            item.autoReuse = true;
            item.shootSpeed = 10;
            item.shoot = 1;
            item.useAmmo = ProjectileID.WoodenArrowFriendly;
            item.crit = 50;
        }

        public override void AddRecipes()
        {
            ModRecipe recipe = new ModRecipe(mod);
            recipe.AddIngredient(ItemID.Ruby, 2);
            recipe.AddIngredient(ItemID.Amber, 2);
            recipe.AddIngredient(ItemID.Topaz, 2);
            recipe.AddIngredient(ItemID.Emerald, 2);
            recipe.AddIngredient(ItemID.Sapphire, 2);
            recipe.AddIngredient(ItemID.Amethyst, 2);
            recipe.AddIngredient(ItemID.Diamond, 2);
            recipe.AddIngredient(ItemID.IronBar, 2);
            recipe.AddTile(TileID.Anvils);
            recipe.SetResult(this);
            recipe.AddRecipe();
        }
    }
You need to use AmmoID.Arrow
Download the example mod, simple problems like these can be avoided by looking at the code of the example mod.
 

FoxyFullmoon

Terrarian
You need to use AmmoID.Arrow
Download the example mod, simple problems like these can be avoided by looking at the code of the example mod.
Thanks, I actually just figured it out by looking at Example Mod. I was already using it as an example, but I somehow just missed it the 20 times that I read it over, as I was also looking at an online guide.
 

The bipod knife

Official Terrarian
How might one go about making an NPC attempt to always stay above the player's location?

I learned the hard way not to set the ai style to 11.
 

Kazzymodus

Moderator
Staff member
Moderator
How might one go about making an NPC attempt to always stay above the player's location?

I learned the hard way not to set the ai style to 11.
Basically, you want to check where the npc is compared to the player: if it's too far to the left, give it some velocity to the right, and vice versa.

Here's a script I use for one of my bosses. It's very basic, but I believe most NPCs in the game follow this format.
Code:
                float horMoveSpeed = 0.07f;
                float vertMoveSpeed = 0.15f;
                float maxMoveSpeed = 10f;

                // Going up.

                if (npc.Center.Y > Main.player[npc.target].Center.Y - 200)
                {
                    if (npc.velocity.Y > 0)
                        npc.velocity.Y *= 0.8f;

                    npc.velocity.Y -= vertMoveSpeed;

                    if (npc.velocity.Y < -maxMoveSpeed)
                        npc.velocity.Y = -maxMoveSpeed;
                }

                // Going down.

                if (npc.Center.Y < Main.player[npc.target].Center.Y - 200)
                {
                    if (npc.velocity.Y < 0)
                        npc.velocity.Y *= 0.8f;

                    npc.velocity.Y += vertMoveSpeed;

                    if (npc.velocity.Y > maxMoveSpeed)
                        npc.velocity.Y = maxMoveSpeed;
                }

                // Going left.

                if (npc.Center.X > Main.player[npc.target].Center.X + 200)
                {
                    if (npc.velocity.X > 0)
                        npc.velocity.X *= 0.98f;

                    npc.velocity.X -= horMoveSpeed;

                    if (npc.velocity.X < -maxMoveSpeed)
                        npc.velocity.X = -maxMoveSpeed;
                }

                // Going right.

                if (npc.Center.X < Main.player[npc.target].Center.X + 200)
                {
                    if (npc.velocity.X < 0)
                        npc.velocity.X *= 0.98f;

                    npc.velocity.X += horMoveSpeed;

                    if (npc.velocity.X > maxMoveSpeed)
                        npc.velocity.X = maxMoveSpeed;
                }

                // When velocities are too low, set them to zero (prevents jittery movement).

                if (npc.velocity.X > -0.05f && npc.velocity.X < 0.05f)
                    npc.velocity.X = 0;
                if (npc.velocity.Y > -0.05f && npc.velocity.Y < 0.05f)
                    npc.velocity.Y = 0;
As you can see, the boss will attempt to stay 200 pixels above the player and 200 pixels to the right of it. If you want the boss to stay exactly above the player, get rid of the + 200 in the horizontal (X) movement.

Yes, I hardcoded the values, shoot me.

If you want to fix your NPC to the player, you can simply do this instead:
Code:
npc.position = Main.player[npc.target].position;
npc.position.y -= 200;
 

FoxyFullmoon

Terrarian
I've been learning a lot today and I've almost completed my bow and arrow set. However, something that I'm clueless on and I couldn't find on google or in the Example Mod: I have the pierce value set to 2 on my projectile. However, this makes it bounce once before dying. I have it set to kill on tile collision, so what would the code be to negate that bounce?

Edit: I figured it out on my own, though it took me way too long.

I changed

if (projectile.penetrate <= 0)
{
projectile.Kill();
}

to

if (projectile.penetrate <= 2)
{
projectile.Kill();
}
 
Last edited:

Will A

Terrarian
Hey! Pretty basic question, but how does one make dusts spawn from wings whilst flying?
Hey I'm pretty sure I know this. I haven't messed around with wings, but I have with dust. This is from the ExamplePickaxe:

public override void MeleeEffects(Player player, Rectangle hitbox)
{
if (Main.rand.Next(10) == 0)
{
int dust = Dust.NewDust(new Vector2(hitbox.X, hitbox.Y), hitbox.Width, hitbox.Height, mod.DustType("DustNameHere"));
}
}

Hope I helped!
 

FoxyFullmoon

Terrarian
Hey I'm pretty sure I know this. I haven't messed around with wings, but I have with dust. This is from the ExamplePickaxe:

public override void MeleeEffects(Player player, Rectangle hitbox)
{
if (Main.rand.Next(10) == 0)
{
int dust = Dust.NewDust(new Vector2(hitbox.X, hitbox.Y), hitbox.Width, hitbox.Height, mod.DustType("DustNameHere"));
}
}

Hope I helped!
I actually needed to know this. Do you know how to put the dust on an arrow trail?

Edit: I figured it out. Here's my code:

Code:
 public override void AI ()
        {
            if (Main.rand.Next(1) == 0)
            {
                int dust = Dust.NewDust(projectile.position, projectile.width, projectile.height, mod.DustType("DustPrism"));
                Main.dust[dust].velocity /= 1f;
            }
        }
 
Last edited:

PyroPoro

Terrarian
Hi im new to modding. I know how to make weapons/npcs (not very gud at it though lul)but does anyone know if it is possible to make a custom animation and use it on a custom weapon, like a spin on a sword or something fancy. If anyone knows plz teach me senpai
 

Jofairden

Duke Fishron
tModLoader
How do you make the player get a custom debuff when hit by a projectile and when hit by an NPC?
And, if possible, how do I make it so the player gets the debuff just by holding an item?
You should probably use the OnHitPlayer or ModifyHitPlayer hook to do this, for items you'll probably use the HoldItem hook.
Hey! Pretty basic question, but how does one make dusts spawn from wings whilst flying?
You can do this by calling Dust.NewDust in the WingUpdate hook from ModItem.
Hi im new to modding. I know how to make weapons/npcs (not very gud at it though lul)but does anyone know if it is possible to make a custom animation and use it on a custom weapon, like a spin on a sword or something fancy. If anyone knows plz teach me senpai
As you said you're not very good at this yet, I'd recommend looking into other things first. What you're asking is possible, heck I even fancied around it, but it's rather difficult compared to a lot of other things. You'll be messing around with custom AI and drawing code.
 

Hallam_9K

Spazmatism
You should probably use the OnHitPlayer or ModifyHitPlayer hook to do this, for items you'll probably use the HoldItem hook.

You can do this by calling Dust.NewDust in the WingUpdate hook from ModItem.

As you said you're not very good at this yet, I'd recommend looking into other things first. What you're asking is possible, heck I even fancied around it, but it's rather difficult compared to a lot of other things. You'll be messing around with custom AI and drawing code.
Thanks, I also want to know how to make a custom block glow when the player uses a spelunker potion.
Also, the block doesn't show up on the mini-map.
 
Top Bottom