Jump to content

Recommended Posts

Not sure why, but this hat disappears when I equip it. The graphics show me wearing it, but it's not in my toolbar. Furthermore, if I equip another hat, the first just disappears. Any idea what part of this code is breaking?

 

local assets=
{
	Asset("ANIM", "anim/hat_ushanka.zip"),
	
	Asset("IMAGE", "images/inventoryimages/hat_ushanka.tex"),	
    Asset("ATLAS", "images/inventoryimages/hat_ushanka.xml")
}

local function onequip(inst, owner) 
        owner.AnimState:OverrideSymbol("swap_hat", "hat_ushanka", "swap_hat")
        owner.AnimState:Show("HAT")
        owner.AnimState:Show("HEAD_HAT")
        owner.AnimState:Hide("HAIR_NOHAT")
        owner.AnimState:Hide("HAIR")
        
        if owner:HasTag("player") then
			owner.AnimState:Hide("HEAD")
			owner.AnimState:Show("HEAD_HAT")
		end
		
		if inst.components.fueled then
			inst.components.fueled:StartConsuming()
		end
end

local function onunequip(inst, owner) 
        owner.AnimState:Hide("HAT")
        owner.AnimState:Hide("HEAD_HAT")
        owner.AnimState:Show("HAIR_NOHAT")
        owner.AnimState:Show("HAIR")

		if owner:HasTag("player") then
	        owner.AnimState:Show("HEAD")
			owner.AnimState:Hide("HEAD_HAT")
		end
		
		if inst.components.fueled then
			inst.components.fueled:StopConsuming()
		end
end


local function fn(Sim)
	local inst = CreateEntity()
	local trans = inst.entity:AddTransform()
	local anim = inst.entity:AddAnimState()
    MakeInventoryPhysics(inst)
    
    inst:AddTag("hat")
    
    anim:SetBank("strawhat")
    anim:SetBuild("hat_ushanka")
    anim:PlayAnimation("anim")    
        
    inst:AddComponent("inspectable")
    
    inst:AddComponent("inventoryitem")
	inst.components.inventoryitem.imagename = "hat_ushanka"
    inst.components.inventoryitem.atlasname = "images/inventoryimages/hat_ushanka.xml"
    
    inst:AddComponent("equippable")
    inst.components.equippable.equipslot = EQUIPSLOTS.HEAD
	inst.components.equippable.dapperness = TUNING.DAPPERNESS_MED
	inst.components.equippable:SetOnEquip( onequip )
    inst.components.equippable:SetOnUnequip( onunequip )
	
	inst:AddComponent("insulator")
    inst.components.insulator:SetInsulation(TUNING.INSULATION_LARGE)
	
	inst:AddComponent("waterproofer")
    inst.components.waterproofer:SetEffectiveness(TUNING.WATERPROOFNESS_SMALL)
	
	inst:AddComponent("fueled")
    inst.components.fueled.fueltype = FUELTYPE.USAGE
    inst.components.fueled:InitializeFuelLevel(TUNING.BEEFALOHAT_PERISHTIME)
    inst.components.fueled:SetDepletedFn(inst.Remove)
    
    return inst
end

return Prefab( "common/inventory/hat_ushanka", fn, assets) 

 

Link to comment
https://forums.kleientertainment.com/forums/topic/80831-hat-disappears-on-equip/
Share on other sites

2 hours ago, KirbyJParasol said:

The graphics show me wearing it, but it's not in my toolbar.

Usually this kind of problem comes because you don't have an inventory image. If the hat exist, but isn't shown in the toolbar, verify your inventory image (right name, no mispelling, assets declared somewhere, etc). If the hat really disappears, i don't know.

Edited by Lumina

I think this is a port from singleplayer Don't Starve...? I'm trying to make a long unsupported mod actually work right, so I don't know the development process in depth, but I DO know that he released the singleplayer version before the multiplayer. So yeah he probably ported it straight over.

The graphics do exist for the toolbar, because sometimes it works right. Just nowhere near consistently.

I have no idea how to check for client/host code.

Edited by KirbyJParasol

Alright, so most of the code works. Hat is working with correct stats, is craftable, doesn't turn invisible, etc.

HOWEVER.

If I ever drop the hat on the ground, all three other players immediately crash. Here's the code and the error message:

local assets=
{
	Asset("ANIM", "anim/hat_ushanka.zip"),
	
	Asset("IMAGE", "images/inventoryimages/hat_ushanka.tex"),	
    Asset("ATLAS", "images/inventoryimages/hat_ushanka.xml")
}

local function onequip(inst, owner) 
        owner.AnimState:OverrideSymbol("swap_hat", "hat_ushanka", "swap_hat")
        owner.AnimState:Show("HAT")
        owner.AnimState:Show("HEAD_HAT")
        owner.AnimState:Hide("HAIR_NOHAT")
        owner.AnimState:Hide("HAIR")
        
        if owner:HasTag("player") then
			owner.AnimState:Hide("HEAD")
			owner.AnimState:Show("HEAD_HAT")
		end
		
		if inst.components.fueled then
			inst.components.fueled:StartConsuming()
		end
end

local function onunequip(inst, owner) 
        owner.AnimState:Hide("HAT")
        owner.AnimState:Hide("HEAD_HAT")
        owner.AnimState:Show("HAIR_NOHAT")
        owner.AnimState:Show("HAIR")

		if owner:HasTag("player") then
	        owner.AnimState:Show("HEAD")
			owner.AnimState:Hide("HEAD_HAT")
		end
		
		if inst.components.fueled then
			inst.components.fueled:StopConsuming()
		end
end


local function fn(Sim)
	local inst = CreateEntity()
	local trans = inst.entity:AddTransform()
	local anim = inst.entity:AddAnimState()
	inst.entity:AddNetwork()
    MakeInventoryPhysics(inst)
    
    inst:AddTag("hat")
    
    anim:SetBank("strawhat")
    anim:SetBuild("hat_ushanka")
    anim:PlayAnimation("anim")    
    
	if not TheWorld.ismastersim then
		return inst
	end
	
    inst:AddComponent("inspectable")
    
    inst:AddComponent("inventoryitem")
	inst.components.inventoryitem.imagename = "hat_ushanka"
    inst.components.inventoryitem.atlasname = "images/inventoryimages/hat_ushanka.xml"
    
    inst:AddComponent("equippable")
    inst.components.equippable.equipslot = EQUIPSLOTS.HEAD
	inst.components.equippable.dapperness = TUNING.DAPPERNESS_MED
	inst.components.equippable:SetOnEquip( onequip )
    inst.components.equippable:SetOnUnequip( onunequip )
	
	inst:AddComponent("insulator")
    inst.components.insulator:SetInsulation(TUNING.INSULATION_LARGE)
	
	inst:AddComponent("waterproofer")
    inst.components.waterproofer:SetEffectiveness(TUNING.WATERPROOFNESS_SMALL)
	
	inst:AddComponent("fueled")
    inst.components.fueled.fueltype = FUELTYPE.USAGE
    inst.components.fueled:InitializeFuelLevel(TUNING.BEEFALOHAT_PERISHTIME)
    inst.components.fueled:SetDepletedFn(inst.Remove)
    
    return inst
end

return Prefab( "common/inventory/hat_ushanka", fn, assets) 

649682B986DDE056994EEE88F6AF5B2036029E74

The SetPristine goes before returning the instance when you run on the host usually. Not sure what it's doing exactly because it's a C-side function and we don't have access to it, but my guess considering where and when it appears in the original prefab codes and the name is to say that before this point, the host and client are identical and after that the differences will appear. I don't think it it what cause the deserialization between the host and client, I believe it happens after, but I cannot be certain.

Edited by ZupaleX

Yes, i don't know why i have it after and not before, probably a mistake i repeated from somewhere. But the line is important.

 

I don't think it's causing the crash itself, but at the moment i don't see any other difference between my working-and-not-crashing hat in my mod and this one except the pristine line (but i'm bad at seeing errors in code).

SetPristine did not fix the crash.

Note that this is not my personal mod, but another player's mod that has long since been abandoned, which I'm trying to fix.

Download link of my most current version: https://www.dropbox.com/s/h2z3x56ro9sz88d/workshop-402531514.zip?dl=0

 

EDIT: It's worth noting that if I put the ushanka in a chest, and another player opens the chest, he immediately crashes to desktop.

Edited by KirbyJParasol

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