tModLoader Need help with changing item damage values depending on NPC defense

D3MIGODLY

Terrarian
So I'm making a meme item that's supposed to change its damage value depending on the nearest NPC's defense. However, it only changes upon saving & exiting the world it was used in.

C#:
public override void AutoDefaults()
        {
           
            for (int i = 0; i < 2; i++)
            {
                NPC target = Main.npc[i];
           
                if (target.active && !target.friendly)
                {
                    item.damage = target.defense + 69; //Only updates when save & exit button is pressed
                    SetDefaults();
                }
                else
                {
                    item.damage = 2;
                    SetDefaults();
                }
            }
        }
 
You can't change the item.damage like this indeed.
You might be able to do what you want in the ModifyWeaponDamage class instead.
Code:
public override void ModifyWeaponDamage(Player player, ref StatModifier damage)
        {
            
        }
 
You can change the damage within ModifyHitNPC() for the item. However I'm not sure about the part of detecting the nearest NPC and seeing what it's DEF is to raise the damage. You can certainly raise the damage based on the DEF of the NPC you hit.

The code you posted will also only look at 2 NPCs in the array rather than which is closest.
 
Back
Top Bottom