tModLoader I require help with my weapon code!

Waulk Stylie

Terrarian
I am currently creating a weapon for my mod. The weapon is supposed to give the user, friends, and npcs who walks into its aura a buff. However, I only managed to get the weapon to work when I damage the user or npcs. I would like to not damage the player when i want to activate the buff. Down below is the code, any help would be wonderful.

_________________________________________________________________________________________________________________________________________________________
Weapon Projectile
_________________________________________________________________________________________________________________________________________________________
using Terraria;
using Terraria.ModLoader;
using Terraria.ID;

namespace SupportClass.Content.Items.Weapon.SBS
{
internal class SteelButterflyStaffAura : ModProjectile
{


public override void SetDefaults()
{
Projectile.width = 144;
Projectile.height = 144;

Projectile.friendly = false;
Projectile.hostile = true;
Projectile.tileCollide = false;
Projectile.ignoreWater = true;

Projectile.DamageType = DamageClass.Magic;

Projectile.aiStyle = -1;
Projectile.penetrate = -1;
}

public override void AI()
{
Projectile.ai[0]++;
if (Projectile.ai[0] >= 180)
{
Projectile.Kill();
}
Lighting.AddLight(Projectile.Center, 1f, 1f, 0f);
}


public override void OnHitNPC(NPC target, int damage, float knockback, bool crit)
{

target.AddBuff(BuffID.Ironskin, 120 * 60);
}


}
}
_________________________________________________________________________________________________________________________________________________________
Weapon
_________________________________________________________________________________________________________________________________________________________
using Terraria;
using Terraria.ModLoader;
using Terraria.ID;
using Terraria.GameContent.Creative;

namespace SupportClass.Content.Items.Weapon.SBS
{
internal class SteelButterflyStaff : ModItem
{
private int timeToAdd;

public override void SetStaticDefaults()
{
DisplayName.SetDefault("Steel Butterfly Staff");
Tooltip.SetDefault("Surrounds the user and allies in a field of Steel Butterflies, increasing Defense by 4");
CreativeItemSacrificesCatalog.Instance.SacrificeCountNeededByItemId[Type] = 5;
}
public override void SetDefaults()
{
Item.width = 42;
Item.height = 74;

Item.useStyle = ItemUseStyleID.Swing;
Item.useTime = 50;
Item.useAnimation = 50;
Item.autoReuse = true;

Item.DamageType = DamageClass.Magic;
Item.noMelee = true;
Item.mana = 0;
Item.damage = 0;
Item.knockBack = 0;
Item.crit = 0;

Item.value = Item.buyPrice(silver: 3);
Item.maxStack = 1;

Item.UseSound = SoundID.Item43;

Item.shoot = ModContent.ProjectileType<SteelButterflyStaffAura>();
Item.shootSpeed = 0;
Item.useAmmo = AmmoID.FallenStar;

}

public override void AddRecipes()
{
Recipe SBS = CreateRecipe();
SBS.AddRecipeGroup("Wood", 10);
SBS.AddRecipeGroup("IronBar", 10);

SBS.Register();



}
}
}
 
You could add the buff to the player when you simply swing the weapon.
Alternatively, in the projectile you could loop through Main.maxPlayers and if a player is active and close enough to the center of the projectile, add the buff.
 
You could add the buff to the player when you simply swing the weapon.
Alternatively, in the projectile you could loop through Main.maxPlayers and if a player is active and close enough to the center of the projectile, add the buff.
And how should I do the coding for Main.maxPlayers?
 
Just use a for loop, where i < Main.maxPlayers

Then check if Main.player[i].active && collision with projectile etc, and apply the buff to that player.
 
Just use a for loop, where i < Main.maxPlayers

Then check if Main.player[i].active && collision with projectile etc, and apply the buff to that player.
Could you put that into exactly what it would look like, like what each line of code should say. If that isn't too much to ask?
 
There's an example of it in ExampleMod here
Small Issue, There is one error for one of the target settings. The code has been altered to only this one error. The target that is producing an error is in the line -if (ForcedTArgetPosition is Vector2 target)-. The error says "a local or perimeter named Target cannot be declared in this scope because that name is used in an enclosing local scope to define a local or perimeter"
Code:
 public override void OnHitNPC(NPC target, int damage, float knockback, bool crit)
        {
            target.AddBuff(BuffID.Ironskin, 120 * 60);
            for (int i = 0; i < Main.maxPlayers; i++)
            {
                Rectangle areaCheck;

                Player player = Main.player[i];

                if (ForcedTargetPosition is Vector2 target)
                    areaCheck = new Rectangle((int)target.X - maxDistance, (int)target.Y - maxDistance, maxDistance * 2, maxDistance * 2);
                else if (player.active && !player.dead && !player.ghost)
                    areaCheck = new Rectangle((int)player.position.X - maxDistance, (int)player.position.Y - maxDistance, maxDistance * 2, maxDistance * 2);
                else
                    continue;  // Not a valid player

                if (hitbox.Intersects(areaCheck))
                {
                    tooFar = false;
                    break;
                }

            }
        }
        internal class hitbox
        {
            public int player;

            internal static bool Intersects(Rectangle areaCheck)
            {
                throw new NotImplementedException();
            }

        }
    }
}
 
Last edited:
Back
Top Bottom