Jump to content

Recommended Posts

I made a custom hat item and it doesn't show up when I add Cave. It works well without cave, though. I really want to know what's the matter with it.

local assets =
{
    Asset("ANIM", "anim/hat_deatiara.zip"),
    Asset("ANIM", "anim/hat_deatiara_swap.zip"),
 
    Asset("ATLAS", "images/inventoryimages/hat_deatiara.xml"),
    Asset("IMAGE", "images/inventoryimages/hat_deatiara.tex"),
}
 
local prefabs =
{
}
 
local function fn()
 
    local function OnEquip(inst, owner)
        owner.AnimState:OverrideSymbol("swap_hat", "hat_deatiara_swap", "swap_hat")
        owner.AnimState:Show("HAT")
        owner.AnimState:Show("HAT_HAIR")
        owner.AnimState:Hide("HAIR_NOHAT")
        owner.AnimState:Hide("HAIR")
        print('A')
        if owner:HasTag("player") then
            print('B')
            owner.AnimState:Hide("HEAD")
            owner.AnimState:Show("HEAD_HAT")
        end
    end
 
    local function OnUnequip(inst, owner)
        owner.AnimState:Hide("HAT")
        owner.AnimState:Hide("HAT_HAIR")
        owner.AnimState:Show("HAIR_NOHAT")
        owner.AnimState:Show("HAIR")
 
        if owner:HasTag("player") then
            owner.AnimState:Show("HEAD")
            owner.AnimState:Hide("HEAD_HAT")
        end
    end
 
    local inst = CreateEntity()
    local trans = inst.entity:AddTransform()
    local anim = inst.entity:AddAnimState()
    MakeInventoryPhysics(inst)
 
    anim:SetBank("hat_deatiara")
    anim:SetBuild("hat_deatiara")
    anim:PlayAnimation("idle")
 
    inst:AddComponent("inventoryitem")
    inst.components.inventoryitem.imagename = "hat_deatiara"
    inst.components.inventoryitem.atlasname = "images/inventoryimages/hat_deatiara.xml"
 
    inst:AddComponent("equippable")
    inst.components.equippable.equipslot = EQUIPSLOTS.HEAD
    inst.components.equippable:SetOnEquip(OnEquip)
    inst.components.equippable:SetOnUnequip(OnUnequip)
 
    -- Add tags and components
    inst:AddTag("umbrella")
    inst:AddTag("waterproofer")
    inst:AddTag("electricdamageimmune")
   
    inst:AddComponent("burnable")
    inst.components.burnable.lightningimmune = true -- Immune to lightning.
 
    inst:AddComponent("waterproofer")
    inst.components.waterproofer:SetEffectiveness(TUNING.WATERPROOFNESS_ABSOLUTE)
 
    inst:AddComponent("insulator")
    inst.components.insulator:SetInsulation(TUNING.INSULATION_LARGE)
    inst.components.insulator:SetSummer()
 
    inst.components.equippable.insulated = true -- Makes the character immune to electric damage when equipped
 
    return inst
end
 
return Prefab("common/inventory/hat_deatiara", fn, assets, prefabs)

 

Move the OnEquip and OnUnEquip functions to the outside of the fn functions (also, OverrideSymbol and ClearOverrideSymbol should be enough for changing the anim state properly)

Then add these lines right below inst:PlayAnimation("idle)

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

This separates what part of the code runs for both client and server, and which runs for just server. This is important when caves are added to the world because you become just a client to the server, whereas without you are directly hosting and count as both server and client. The item doesn't work as is because its trying to do things on the client that it can not do. Things like AddTag, visuals like animation, and sounds should be done above this line so clients get it, and things like AddComponent should be done below as components are a server only thing can't be used by clients.

Finally, move your three inst:AddTag lines above the inst.entity:SetPristine() line, for the above reason.

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...