SenL Posted February 11, 2016 Share Posted February 11, 2016 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 More sharing options...
zUsername Posted February 11, 2016 Share Posted February 11, 2016 Try this code, put the code in character.lua local function IsWearing(inst) for k, v in pairs(inst.components.inventory.equipslots) do if v.prefab == "yourprefab" then return true end end return false end Link to comment Share on other sites More sharing options...
Kzisor Posted February 11, 2016 Share Posted February 11, 2016 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 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