Jump to content

Recommended Posts

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.

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
    
   image.thumb.jpeg.d0f369298c3c74825729a1b0431b3ceb.jpeg

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.

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

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)

 

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 by _zwb

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
  • Create New...