Jump to content

Code to check if player is wearing certain prefab?


Recommended Posts

What kind of code would be used to check if player is wearing* certain prefab (in my case, it's a custom hat "myhat")?

*(not just in the inventory/backpack, but must be wearing)

Would it be easier just to set some flag on the hat's onequip and onunequip?

Thanks.

Link to comment
Share on other sites

11 hours ago, SenL said:

Would it be easier just to set some flag on the hat's onequip and onunequip?

While @zUsername 's code works, it wastes CPU cycles. If you don't want to waste CPU cycles then you need to do something like the following, which runs at the start of the game initialization and only when someone equips/unequips the item.

local function ChangePrefab( prefab )

	if not GLOBAL.TheWorld.ismastersim then
		return
	end

	local _OnEquip = prefab.components.equippable.onequipfn
	local function onequip( inst, owner )
		_OnEquip( inst, owner )

		owner["has_" .. inst.prefab] = true
	end

	local _OnUnequip = prefab.components.equippable.onunequipfn
	local function onunequip( inst, owner )
		_OnUnequip( inst, owner )

		owner["has_" .. inst.prefab] = false
	end

	prefab.components.equippable:SetOnEquip( onequip )
	prefab.components.equippable:SetOnUnequip( onunequip )

end

AddPrefabPostInit( "[INSERT PREFAB NAME HERE]", ChangePrefab )

Once you set the prefabs in your other code all you have to do is simply check to see if "has_prefabname" equals true or false.

Link to comment
Share on other sites

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
 Share

×
  • Create New...