tModLoader Need help making armor piece give health on npc kill

BiggulFottum

Terrarian
I'm kinda new to Terraria mod development and have been struggling to make this work.
can anyone help me out?

Here's my code:
using Terraria;
using Terraria.ID;
using Terraria.Localization;
using Terraria.ModLoader;
namespace biggulsadditions.Content.Items.Equipable.Armor.Head
{
// The AutoloadEquip attribute automatically attaches an equip texture to this item.
// Providing the EquipType.Head value here will result in TML expecting a X_Head.png file to be placed next to the item's main texture.
[AutoloadEquip(EquipType.Head)]
public class ObsidianMind : ModItem
{
public override void SetStaticDefaults()
{
ArmorIDs.Head.Sets.DrawHead[Item.headSlot] = false;
}
public override void SetDefaults() {
Item.width = 18; // Width of the item
Item.height = 18; // Height of the item
Item.value = Item.sellPrice(gold: 1); // How many coins the item is worth
Item.rare = ItemRarityID.Green; // The rarity of the item
Item.defense = 5; // The amount of defense the item will give when equipped
}
public override void OnHitNPC(Player player, NPC target, NPC.HitInfo hit, int damageDone)
{
if (target.life <= 0) // Check if the NPC will die from this hit
{
player.statLife += 2;
player.HealEffect(10);
}
}
}
}
 
This bot info from the TML discord will help. It talks about an accessory but functionally it's the same for Armor, just in UpdateEquip() instead.


Sometimes it isn't possible to create an effect for an accessory without supplementing it with other stuff. A common pattern to follow is using a bool field/property on a class that inherits ModPlayer. Here's a quick rundown of how to set this pattern up:
  • Your accessory item uses its UpdateAccessory hook to set our bool field, SomeAccessoryBool, on a class inheriting ModPlayer, SomeAccessoryPlayer: player.GetModPlayer&lt;SomeAccessoryPlayer&gt;().SomeAccessoryBool = true
  • Then, SomeAccessoryPlayer uses its ResetEffects hook to set SomeAccessoryBool back to false. This makes it so we don't need to add any extra logic for handling unequiping our accessory
  • Finally, we can implement any effect we want using any hooks on SomeAccessoryPlayer
Here's an example of this pattern in action:
Accessory: tModLoader/ExampleMod/Content/Items/Accessories/ExampleImmunityAccessory.cs at stable · tModLoader/tModLoader
Player: tModLoader/ExampleMod/Common/Players/ExampleImmunityPlayer.cs at stable · tModLoader/tModLoader
 
Back
Top Bottom