tModLoader How to create a bow?

HummeL777

Steampunker
Last night, while creating a gun, I had a request to make a bow. But in ExampleMod I did not have a layout of a bow or arrows. Could you tell me how to remake the code?

Code of rifle:
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;

namespace GlobalOffensive.Items.Weapons.Range
{
public class Rifle : ModItem
{
public override void SetDefaults()
{
item.name = "Hunter rifle";
item.damage = 25;
item.ranged = true;
item.width = 45;
item.height = 9;
item.toolTip = "This is a modded gun.";
item.useTime = 30;
item.useAnimation = 30;
item.useStyle = 5;
item.noMelee = true; //so the item's animation doesn't do damage
item.knockBack = 4;
item.value = 10000;
item.rare = 2;
item.UseSound = SoundID.Item11;
item.autoReuse = true;
item.shoot = mod.ProjectileType("CopperBullet");
item.shootSpeed = 6f;
item.useAmmo = mod.ItemType("CopperBullet");
item.scale = 1.5f;
}

public override void AddRecipes()
{
ModRecipe recipe = new ModRecipe(mod);
recipe.AddIngredient(ItemID.DynastyWood, 5);
recipe.AddIngredient(ItemID.IronBar, 10);
recipe.AddIngredient(ItemID.Leather, 3);
recipe.AddIngredient(ItemID.GrayPressurePlate, 1);
recipe.AddTile(TileID.Anvils);
recipe.SetResult(this);
recipe.AddRecipe();
}

public override Vector2? HoldoutOffset()
{
return new Vector2(-8, 0);
}
}
}



Code of Bullets(Item):


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

namespace GlobalOffensive.Items.Weapons.Range.Ammo
{
public class CopperBullet : ModItem
{
public override void SetDefaults()
{
item.name = "Hunter Bullet";
item.damage = 6;
item.ranged = true;
item.width = 1;
item.height = 1;
item.maxStack = 999;
item.toolTip = "This is a modded bullet ammo.";
item.consumable = true; //You need to set the item consumable so that the ammo would automatically consumed
item.knockBack = 1.5f;
item.value = 10;
item.rare = 2;
item.shoot = mod.ProjectileType("CopperBullet"); //The projectile shoot when your weapon using this ammo
item.shootSpeed = 12f; //The speed of the projectile
item.ammo = item.type; //The ammo class this ammo belongs to.
}

public override void AddRecipes()
{
ModRecipe recipe = new ModRecipe(mod);
recipe.AddIngredient(ItemID.DirtBlock, 1);
recipe.AddTile(TileID.Anvils);
recipe.SetResult(this, 50);
recipe.AddRecipe();
}
}
}


Code of Bullets(Projectile):


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

namespace GlobalOffensive.Projectile
{
public class CopperBullet : ModProjectile
{
public override void SetDefaults()
{
projectile.name = "CopperBullet";
projectile.width = 8;
projectile.height = 8;
projectile.aiStyle = 1;
projectile.friendly = true;
projectile.hostile = false;
projectile.ranged = true;
projectile.penetrate = 1;
projectile.timeLeft = 600;
projectile.alpha = 255;
projectile.light = 0f;
projectile.ignoreWater = true;
projectile.tileCollide = true;
projectile.extraUpdates = 1;
ProjectileID.Sets.TrailCacheLength[projectile.type] = 5;
ProjectileID.Sets.TrailingMode[projectile.type] = 0;
aiType = ProjectileID.Bullet;
}

public override bool OnTileCollide(Vector2 oldVelocity)
{
//If collide with tile, reduce the penetrate.
//So the projectile can reflect at most 5 times
projectile.penetrate--;
if (projectile.penetrate <= 0)
{
projectile.Kill();
}
else
{
if (projectile.velocity.X != oldVelocity.X)
{
projectile.velocity.X = -oldVelocity.X;
}
if (projectile.velocity.Y != oldVelocity.Y)
{
projectile.velocity.Y = -oldVelocity.Y;
}
Main.PlaySound(SoundID.Item10, projectile.position);
}
return false;
}

public override bool PreDraw(SpriteBatch spriteBatch, Color lightColor)
{
Vector2 drawOrigin = new Vector2(Main.projectileTexture[projectile.type].Width * 0.5f, projectile.height * 0.5f);
for (int k = 0; k < projectile.oldPos.Length; k++)
{
Vector2 drawPos = projectile.oldPos[k] - Main.screenPosition + drawOrigin + new Vector2(0f, projectile.gfxOffY);
Color color = projectile.GetAlpha(lightColor) * ((float)(projectile.oldPos.Length - k) / (float)projectile.oldPos.Length);
spriteBatch.Draw(Main.projectileTexture[projectile.type], drawPos, null, color, projectile.rotation, drawOrigin, projectile.scale, SpriteEffects.None, 0f);
}
return true;
}
}
 
Additional questions:
1. How do you make the code displayed properly
2.How to add an effect to the bullet
3. What are aiStyle there is
 
Basically, there's no difference between a gun and a bow apart from the sound it makes and the sprite used. Here's a code from a simple bow as an example:
Code:
using Microsoft.Xna.Framework;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;

namespace dropyourweapon.Items
{
  public class Goblin_Bow : ModItem
  {
  public override void SetStaticDefaults()
  {
  DisplayName.SetDefault("Goblin Bow");
  Tooltip.SetDefault("A strong and sturdy bow.");
  }

  public override void SetDefaults()
  {
  item.damage = 22;
  item.ranged = true;
  item.width = 12;
  item.height = 38;
  item.maxStack = 1;
  item.useTime = 28;
  item.useAnimation = 28;
  item.useStyle = 5;
  item.knockBack = 2;
  item.value = 12000;
  item.rare = 2;
  item.UseSound = SoundID.Item5;
  item.noMelee = true;
  item.shoot = 1;
  item.useAmmo = AmmoID.Arrow;
  item.shootSpeed = 10f;
  item.autoReuse = false;
  }
  }
}
As you can see, it has the same use style and everything, and it shoots arrows, naturally. It could shoot anything else, though.

As for the arrows, they have the same aiStyle, but set the aiType to "aiType = ProjectileID.WoodenArrowFriendly;" to make them behave like arrows. Simple.
All other effects and additions work the same. Another example, just for reference:
Code:
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;

namespace BossUpcycling.Projectiles
{
  public class eaterarrow : ModProjectile
  {
  public override void SetStaticDefaults()
  {
  DisplayName.SetDefault("Eater's Arrow");
  }

  public override void SetDefaults()
  {
  projectile.width = 14;
  projectile.height = 32;
  projectile.aiStyle = 1;
  projectile.friendly = true;  //Can the projectile deal damage to enemies?
  projectile.hostile = false;  //Can the projectile deal damage to the player?
  projectile.ranged = true;
  projectile.penetrate = 2;
  projectile.timeLeft = 600;
  projectile.ignoreWater = false;
  projectile.tileCollide = true;
  aiType = ProjectileID.WoodenArrowFriendly;
  }

  public override void AI()
  {
  if (projectile.owner == Main.myPlayer && Main.rand.Next(4) == 0)
  {
  Dust.NewDust(projectile.position + projectile.velocity, projectile.width, projectile.height, 14, projectile.oldVelocity.X * 0.1f, projectile.oldVelocity.Y * 0.1f);
  }
  }


  public override void Kill(int timeLeft)
  {
  Main.PlaySound(0, (int)projectile.position.X, (int)projectile.position.Y, 1, 1f, 0f);
  for (int k = 0; k < 5; k++)
  {
  Dust.NewDust(projectile.position + projectile.velocity, projectile.width, projectile.height, 0, projectile.oldVelocity.X * 0.1f, projectile.oldVelocity.Y * 0.1f);
  }
  }
  }
}

As for your 3 questions:
1) To display code in the forum, use [ code ]Your Code[ /code ] (without the spaces, of course)
2) It depends on the effect. If you want to add a debuff, you'll need something like this:
Code:
  public override void OnHitNPC(NPC target, int damage, float knockback, bool crit)
  {
  target.AddBuff(BuffID.Slimed, 150, false);
  }
3) There's a list for Vanilla Projectile IDs and Vanilla Projectile AIs.
 
Last edited:
Yes, now the projectile behaves like an arrow. But there is one minus it has ceased to be displayed ... And this happens every time I change aiStyle or aiType.
 
What did you change it to? Does it vanish after firing or is it just invisible?
If you just want it to be like an arrow, you should keep projectile.aiStyle at 1 and aiType at ProjectileID.WoodenArrowFriendly.
 
Yes, I have these values. But after the shot the projectile is invisible, I can learn its trajectory only by the particles that are flying out of it.
Code:
            projectile.aiStyle = 1;
            aiType = ProjectileID.WoodenArrowFriendly;
These are the lines
 
How do I make it fire 2 arrows at a time? I'm making a Shroomite repeater that fires 2 and i need help lol
Here's my code:
using Terraria.ID;
using Terraria.ModLoader;

namespace willsweaponpack.Items
{
public class ShroomiteRepeater : ModItem
{
public override void SetStaticDefaults()
{
DisplayName.SetDefault("ShroomiteRepeater");
Tooltip.SetDefault("This is a modded bow.");
}
public override void SetDefaults()
{
item.damage = 70;
item.ranged = true;
item.width = 54;
item.height = 24;
item.maxStack = 1;
item.useTime = 14;
item.useAnimation = 28;
item.useStyle = 5;
item.knockBack = 4;
item.value = 15000;
item.rare = 4;
item.UseSound = SoundID.Item5;
item.noMelee = true;
item.shoot = 1;
item.useAmmo = AmmoID.Arrow;
item.shootSpeed = 5f;
item.autoReuse = true;
}

public override void AddRecipes()
{
ModRecipe recipe = new ModRecipe(mod);
recipe.AddIngredient(ItemID.ShroomiteBar, 10);
recipe.AddTile(TileID.WorkBenches);
recipe.SetResult(this);
recipe.AddRecipe();
}
}
}
 
1605363232810.png
I request help
Code:
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;

namespace dropyourweapon.Items
{
public class FirstBow : ModItem
{
public override void SetStaticDefaults()
{
DisplayName.SetDefault("Toxic_WolfYt67's cheat");
Tooltip.SetDefault("Overpowered bow");
}

public override void SetDefaults()
{
item.damage = 99999999999999;
item.ranged = true;
item.width = 12;
item.height = 38;
item.maxStack = 1;
item.useTime = 10;
item.useAnimation = 28;
item.useStyle = 5;
item.knockBack = 9999999;
item.value = 12000;
item.rare = 2;
item.UseSound = SoundID.Item5;
item.noMelee = true;
item.shoot = 100;
item.useAmmo = AmmoID.Arrow;
item.shootSpeed = 99999f;
item.autoReuse = true;
}
}
}
 
View attachment 298282 I request help
Code:
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;

namespace dropyourweapon.Items
{
public class FirstBow : ModItem
{
public override void SetStaticDefaults()
{
DisplayName.SetDefault("Toxic_WolfYt67's cheat");
Tooltip.SetDefault("Overpowered bow");
}

public override void SetDefaults()
{
item.damage = 99999999999999;
item.ranged = true;
item.width = 12;
item.height = 38;
item.maxStack = 1;
item.useTime = 10;
item.useAnimation = 28;
item.useStyle = 5;
item.knockBack = 9999999;
item.value = 12000;
item.rare = 2;
item.UseSound = SoundID.Item5;
item.noMelee = true;
item.shoot = 100;
item.useAmmo = AmmoID.Arrow;
item.shootSpeed = 99999f;
item.autoReuse = true;
}
}
}

is the "f" next to "99999" needed? also i need you should calm down your numbers, set it to 5 9s or something.
 
Yeah, if your numbers should not be over the integer limit. And I wouldn't be surprised if that level of knockback crashed the game even if C# didn't reject it.
 
Back
Top Bottom