ARGARGARGARG Posted March 15, 2025 Share Posted March 15, 2025 (edited) Greetings gentlemen of the Klei Forum, I would like to learn how to make an item for a character I have been working on. I want to make a hat that fully absorbs 1 hit of damage, but then gets dropped afterwards. I believe it would create a really interesting dynamic during combat. It could be like when you're hit by a frog, or if that's not possible, It could break after one hit and spawn a duplicate of itself on the ground to similar effect. Unfortunately I lack any knowledge when it comes to adding items, and would greatly appreciate assistance in programming this function. I will pay you in kind words and gratitude. Please I'm desperate Edited March 15, 2025 by ARGARGARGARG fixed a typo Link to comment https://forums.kleientertainment.com/forums/topic/164894-i-need-help-programming-a-hat/ Share on other sites More sharing options...
oregu Posted March 16, 2025 Share Posted March 16, 2025 (edited) Do you have a template/prefab file of your hat? I think the relevant components are armor, finiteuses, and then inst.components.inventory:DropItem. Gonna whip something up with that. Edited March 16, 2025 by oregu Link to comment https://forums.kleientertainment.com/forums/topic/164894-i-need-help-programming-a-hat/#findComment-1807645 Share on other sites More sharing options...
ARGARGARGARG Posted March 17, 2025 Author Share Posted March 17, 2025 4 hours ago, oregu said: Do you have a template/prefab file of your hat? I think the relevant components are armor, finiteuses, and then inst.components.inventory:DropItem. Gonna whip something up with that. I used Hat Example as a template, and added a couple lines for the armor values. Besides that, I believe I haven't edited anything except for the name. I have some code in my Character's Prefab file that makes them take knockback when attacked, so I have the absorption set to .99 to avoid completely disabling that. Hopefully that doesn't get in the way, because I feel the knockback would keep it interesting. Thank you very much for your time! leafpin.lua Link to comment https://forums.kleientertainment.com/forums/topic/164894-i-need-help-programming-a-hat/#findComment-1807677 Share on other sites More sharing options...
oregu Posted March 17, 2025 Share Posted March 17, 2025 (edited) Spoiler STRINGS.NAMES.LEAFPIN = "Leaf Pin" STRINGS.RECIPE_DESC.LEAFPIN = "It's a slippery helmet, don't you know?" for char,desc in pairs { GENERIC = "It barely stays on my head!", WILLOW = "This protects my brain, but not for long.", WOLFGANG = "Ahh! No matter how much force, it can't stay on good...", WENDY = "It's too safe for my liking.", WX78 = "PERFECT EXAMPLE OF HOW CARELESS FLESHLINGS ARE.", WICKERBOTTOM = "A clandestine accessory.", WOODIE = "Not as slippery as most things I've handled", WAXWELL = "This is too silly for my liking.", WATHGRITHR = "A fleeting helmet is not ideal for combat, but I may utilize it.", WEBBER = "It keeps trying to get away from us.", WINONA = "This hat needs repairs badly.", WORTOX = "Oohoh, maybe I could get someone to wear this!", WORMWOOD = "Funny dangerous hat.", WARLY = "I think I would rather use something else, but I can't be picky.", WURT = "Flurt, can't make holes for horns...", WALTER = "Wow, I would glue it, but it would ruin my hair.", } do if STRINGS.CHARACTERS[char] then STRINGS.CHARACTERS[char].DESCRIBE.LEAFPIN = desc end end local assets= { Asset("ANIM", "anim/leafpin.zip"), Asset("ANIM", "anim/leafpin_swap.zip"), Asset("ATLAS", "images/inventoryimages/leafpin.xml"), Asset("IMAGE", "images/inventoryimages/leafpin.tex"), } local prefabs = { } local function OnArmorDamaged(inst, amount) --c_announce(tostring(inst)..tostring(amount)) local owner = inst.components.inventoryitem and inst.components.inventoryitem.owner local inventory = owner and owner.components.inventory if amount <= 0 or not inventory then return end inst:AddTag"slippingaway" inventory:DropItem(inst, nil, true) inst.SoundEmitter:PlaySound("dontstarve/common/tool_slip") -- Makes sound when falling off end local function fn() local function OnEquip(inst, owner) inst:RemoveTag"slippingaway" -- So the hat can be equipped and unequipped normally owner.AnimState:OverrideSymbol("swap_hat", "leafpin_swap", "swap_hat") if owner:HasTag("player") then end end local function OnUnequip(inst, owner) if owner:HasTag("player") then end end local inst = CreateEntity() local trans = inst.entity:AddTransform() local anim = inst.entity:AddAnimState() inst.entity:AddSoundEmitter() -- Add sound emitter MakeInventoryPhysics(inst) anim:SetBank("leafpin") anim:SetBuild("leafpin") anim:PlayAnimation("idle") inst:AddTag("hat") inst.entity:SetPristine() if not TheWorld.ismastersim then return inst end inst:AddComponent("inventoryitem") inst.components.inventoryitem.imagename = "leafpin" inst.components.inventoryitem.atlasname = "images/inventoryimages/leafpin.xml" inst:AddComponent("equippable") inst.components.equippable.equipslot = EQUIPSLOTS.HEAD inst.components.equippable:SetOnEquip(OnEquip) inst.components.equippable:SetOnUnequip(OnUnequip) inst:AddComponent("armor") inst.components.armor:InitCondition(TUNING.ARMORSNURTLESHELL, TUNING.FULL_ABSORPTION) -- (armor, absorbtion) doesnt matter which armor value you use inst:ListenForEvent("armordamaged", OnArmorDamaged) inst:AddComponent("waterproofer") inst.components.waterproofer:SetEffectiveness(TUNING.WATERPROOFNESS_SMALL) -- most head armor is waterproof, i'm guessing you want yours to be the same return inst end return Prefab("leafpin", fn, assets, prefabs) you need to overwrite a stategraph in modmain.lua so that you're not doing the unequip animation while the hat is slipping away Spoiler AddStategraphPostInit("wilson", function(sg) local unequip_old for name,event in pairs(sg.events) do if name == "unequip" then unequip_old = event.fn event.fn = function(inst, data, ...) -- GLOBAL.c_announce(tostring(inst)..", "..tostring(data)) if data.item and data.item:HasTag"slippingaway" then return end return unequip_old(inst, data, ...) end break end end if not unequip_old then print"Warning: Could not locate unequip fn in SGwilson" end end) You could probably rewrite you knockback event to be if you have an data.attacker then knock back. Edited March 18, 2025 by oregu Link to comment https://forums.kleientertainment.com/forums/topic/164894-i-need-help-programming-a-hat/#findComment-1807815 Share on other sites More sharing options...
ARGARGARGARG Posted March 18, 2025 Author Share Posted March 18, 2025 It works! Thank you so much! I didn't know how to give it a name in the inventory, so I'm glad you added that lol. The slipping sound effect was also a really nice touch. The knockback stopped working on my end though, so I had to set the absorption back to 0.99. That might be my fault however. I'm using ListenForEvent "attacked", but there could be a better alternative. It really is everything I could've asked for though, I seriously can't thank you enough. 1 Link to comment https://forums.kleientertainment.com/forums/topic/164894-i-need-help-programming-a-hat/#findComment-1807834 Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now