Jump to content

[Solved (with alternative-coding)] How to make an item add you a tag when equipped?


Recommended Posts

Hey there!

Long story short: my custom survivor has a constant Health drain, but if he equips his custom item, he would gain Health instead of losing.

My approach was to give a tag to the custom item wielder the time he equips the item, and remove the tag when unequips it.

This is the coding I've came up with:

character.lua, local master_postinit = function(inst) section:

Quote

    if inst:HasTag("mylamp") then
        inst.components.health:StartRegen(3, 2)
    else
        inst.components.health:StartRegen(-2, 3)
    end

item.lua:

Quote

 

local function OnEquip(inst, owner)
    owner.AnimState:OverrideSymbol("swap_object", "swap_wagiclamp", "swap_wagiclamp")
    owner.AnimState:Show("ARM_carry")
    owner.AnimState:Hide("ARM_normal")
    
    if owner and owner:HasTag("lamprenter") then
        inst:AddTag("mylamp")
    end
    
end


local function OnUnequip(inst, owner)
    owner.AnimState:Hide("ARM_carry")
    owner.AnimState:Show("ARM_normal")
    
    if owner and owner:HasTag("lamprenter") then
        inst:RemoveTag("mylamp")
    end
    
end

 

 

I might've put something in the wrong place, since it crashes the game when I equip the item.

Lamprenter is a tag my character always has. I've put the "if" part there, so other survivors don't benefit from the healing, only the custom one.

What should I change?

Thanks in advance guys!

Edited by BillTheCipher
problem got solved
Link to comment
Share on other sites

Whats your error log?

I did something similar with the flower amulet. why are you adding the "mylamp" tag to the lamp (inst) itself and not to the owner? maybe that the problem. before you use "inst.components.health:StartRegen(3, 2)" you should check before if the ist has a component and health
"if inst.components ~= nil and inst.components.health ~= nil then" to take care of the client side and owner without health.

 

Edited by krylincy
Link to comment
Share on other sites

I'd say, instead of making your custom item add a tag to your character and that way control the regeneration, make it so that the character itself controls it.

For that you could add ListenForEvent("equip") and ListenForEvent("unequip") and add a function that checks whether the item that has been equipped is your custom item.

Example:

local function OnEquipCustom(inst, data)
	if data.item.prefab == "customitem" then
		inst.components.health:StartRegen(3, 2)
	end
end

local function OnUnequipCustom(inst, data)
	if data.item.prefab == "customitem" then
		inst.components.health:StartRegen(-2, 3)
	end
end

inst:ListenForEvent("equip", OnEquipCustom)
inst:ListenForEvent("unequip", OnUnequipCustom)

 

  • Like 2
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...