Random_kin Posted September 17, 2023 Share Posted September 17, 2023 I'm trying to make a sword that does quite a bit of damage but that can only be equipped by someone whit their sanity a certain level (let's say 40-20 sanity pt). I tried to make it work but then I realized that I had no idea of what I was doing. Link to comment https://forums.kleientertainment.com/forums/topic/151002-is-there-a-way-to-make-a-custom-item-equippable-only-when-at-low-sanity/ Share on other sites More sharing options...
_zwb Posted September 17, 2023 Share Posted September 17, 2023 You can make it that when picked up, check if player sanity is within range, if not drop the item, just like how other players can't pick up Lucy the axe Link to comment https://forums.kleientertainment.com/forums/topic/151002-is-there-a-way-to-make-a-custom-item-equippable-only-when-at-low-sanity/#findComment-1665462 Share on other sites More sharing options...
Random_kin Posted September 20, 2023 Author Share Posted September 20, 2023 Yeah I've tried that, the game crashed so I guess it was poor coding on my end. I'll take a look at lucy's code ,thanks. Link to comment https://forums.kleientertainment.com/forums/topic/151002-is-there-a-way-to-make-a-custom-item-equippable-only-when-at-low-sanity/#findComment-1665896 Share on other sites More sharing options...
Random_kin Posted October 1, 2023 Author Share Posted October 1, 2023 no luck Link to comment https://forums.kleientertainment.com/forums/topic/151002-is-there-a-way-to-make-a-custom-item-equippable-only-when-at-low-sanity/#findComment-1669140 Share on other sites More sharing options...
Random_kin Posted October 1, 2023 Author Share Posted October 1, 2023 I wrote this under Local Function Mainfunction () inst:DoPeriodicTask( 2.5, function(inst) if inst.components.sanity.current>=40 then self:DropItem(item) self:GiveItem(item) end end And got this error Link to comment https://forums.kleientertainment.com/forums/topic/151002-is-there-a-way-to-make-a-custom-item-equippable-only-when-at-low-sanity/#findComment-1669233 Share on other sites More sharing options...
_zwb Posted October 1, 2023 Share Posted October 1, 2023 If I'm guessing correctly, inst is the hand item entity? Then you'd need: Spoiler local owner = inst.components.inventoryitem:GetGrandOwner() if owner and owner.components.sanity then -- do some sanity check etc end I'm not at my PC right now, do check components/inventoryitem.lua and see if I'm using the correct function to get the item's owner. Link to comment https://forums.kleientertainment.com/forums/topic/151002-is-there-a-way-to-make-a-custom-item-equippable-only-when-at-low-sanity/#findComment-1669263 Share on other sites More sharing options...
Random_kin Posted October 2, 2023 Author Share Posted October 2, 2023 This is what I've found inventoryitem.lua . function InventoryItem:GetGrandOwner() if self.owner then if self.owner.components.inventoryitem then return self.owner.components.inventoryitem:GetGrandOwner() else return self.owner end end end Link to comment https://forums.kleientertainment.com/forums/topic/151002-is-there-a-way-to-make-a-custom-item-equippable-only-when-at-low-sanity/#findComment-1669451 Share on other sites More sharing options...
Random_kin Posted October 2, 2023 Author Share Posted October 2, 2023 I've probably made a mess but it's not working , when I try to equip the weapon it just crashes . Here's the script ,I tried to fix it without success, Spoiler local Assets = { -- Animation files for the item (showing it on the ground and swap symbols for the players). Asset("ANIM", "anim/custom_handitem.zip"), Asset("ANIM", "anim/custom_handitem_ground.zip"), -- Inventory image and atlas file used for the item. Asset("ATLAS", "images/inventoryimages/custom_handitem.xml"), Asset("IMAGE", "images/inventoryimages/custom_handitem.tex"), } local function OnEquip(inst,owner,self) inst:DoPeriodicTask( 2.5, function (inst) local owner = inst.components.inventoryitem:GetGrandOwner() if owner and owner.components.sanity then if owner.components.sanity.current>=40 then self:DropItem(item) self:GiveItem(item) end end end ) -- This will override symbol "swap_body" of the equipping player with your custom build symbol. -- Here's what this function is overriding: -- owner.AnimState:OverrideSymbol(Player's_symbol, Your_build(*.zip_filename), Symbol_from_your_build(name_of_subfolder_with_art) owner.AnimState:OverrideSymbol("swap_object", "custom_handitem", "swap_object") -- Players have 2 left arms - one of them is hidden when we are not holding an item and vice versa. -- Since we want to show an item on equip - hide ARM_normal and show ARM_carry. owner.AnimState:Show("ARM_carry") owner.AnimState:Hide("ARM_normal") end local function OnUnequip(inst, owner) owner.AnimState:Hide("ARM_carry") owner.AnimState:Show("ARM_normal") end local function MainFunction() -- Functions which are performed both on Client and Server start here. local inst = CreateEntity() inst.entity:AddTransform() inst.entity:AddAnimState() inst.entity:AddNetwork() inst.entity:AddSoundEmitter() MakeInventoryPhysics(inst) -- Add minimap icon. Remember about its XML in modmain.lua! local minimap = inst.entity:AddMiniMapEntity() minimap:SetIcon("custom_handitem.tex") --[[ ANIMSTATE ]]-- -- This is the name visible on the top of hierarchy in Spriter. inst.AnimState:SetBank("custom_handitem_ground") -- This is the name of your compiled*.zip file. inst.AnimState:SetBuild("custom_handitem_ground") -- This is the animation name while item is on the ground. inst.AnimState:PlayAnimation("anim") --[[ TAGS ]]-- inst:AddTag("custom_handitem") MakeInventoryFloatable(inst, "small", 0.05, {1.2, 0.75, 1.2}) inst.entity:SetPristine() if not TheWorld.ismastersim then -- If we're not the host - stop performing further functions. -- Only server functions below. return inst end inst:AddComponent("inspectable") inst:AddComponent("inventoryitem") inst.components.inventoryitem.imagename = "custom_handitem" inst.components.inventoryitem.atlasname = "images/inventoryimages/custom_handitem.xml" inst:AddComponent("equippable") inst.components.equippable:SetOnEquip(OnEquip) inst.components.equippable:SetOnUnequip(OnUnequip) inst:AddComponent("weapon") inst.components.weapon:SetDamage(100) MakeHauntableLaunch(inst) return inst end return Prefab("common/inventory/custom_handitem", MainFunction, Assets) Link to comment https://forums.kleientertainment.com/forums/topic/151002-is-there-a-way-to-make-a-custom-item-equippable-only-when-at-low-sanity/#findComment-1669475 Share on other sites More sharing options...
_zwb Posted October 2, 2023 Share Posted October 2, 2023 (edited) Try this: Spoiler local function OnEquip(inst, owner, from_ground) local function sanity_check() if owner and owner.components.sanity then if owner.components.sanity.current>=40 then owner.components.inventory:Unequip(EQUIPSLOTS.HANDS) end end end -- Immediately check sanity when equipped sanity_check() -- Cancel any existing task if inst.sanity_check_task then inst.sanity_check_task:Cancel() inst.sanity_check_task = nil end -- Start a new task inst.sanity_check_task = inst:DoPeriodicTask(2.5, sanity_check) -- This will override symbol "swap_body" of the equipping player with your custom build symbol. -- Here's what this function is overriding: -- owner.AnimState:OverrideSymbol(Player's_symbol, Your_build(*.zip_filename), Symbol_from_your_build(name_of_subfolder_with_art) owner.AnimState:OverrideSymbol("swap_object", "custom_handitem", "swap_object") -- Players have 2 left arms - one of them is hidden when we are not holding an item and vice versa. -- Since we want to show an item on equip - hide ARM_normal and show ARM_carry. owner.AnimState:Show("ARM_carry") owner.AnimState:Hide("ARM_normal") end local function OnUnequip(inst, owner) -- Cancel the task if inst.sanity_check_task then inst.sanity_check_task:Cancel() inst.sanity_check_task = nil end owner.AnimState:Hide("ARM_carry") owner.AnimState:Show("ARM_normal") end Edited October 2, 2023 by _zwb Link to comment https://forums.kleientertainment.com/forums/topic/151002-is-there-a-way-to-make-a-custom-item-equippable-only-when-at-low-sanity/#findComment-1669565 Share on other sites More sharing options...
Random_kin Posted October 3, 2023 Author Share Posted October 3, 2023 Ehi thanks! So it's working , when sanity is above 40 pt ihe weapon gets unequipped but it doesn't get back into the invetory, it just disappears. Link to comment https://forums.kleientertainment.com/forums/topic/151002-is-there-a-way-to-make-a-custom-item-equippable-only-when-at-low-sanity/#findComment-1669707 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