Jump to content

Listen for event "sanitydelta" for owner of equipped item


Recommended Posts

I'm trying to make the uses of an equipable item increment each time the owner's sanity goes down:

local function drainsanity(inst, owner)Inst:listenforevent("sanitydelta", function(inst) inst.components.finiteuses:SetUses(inst.components.finiteuses:GetUses + 1) end)end

But the code doesn't seem to do anything much so far, and I am out of ideas.

 

Anyone know a solution to this?

 

Link to comment
Share on other sites

@Virtualistik Your variables are all jumbled up. Read this post about instances.

And, lua is case sensitive.

Inst != inst

-

 

Functions must have () when called, even when no arguments are passed.

GetUses()-- NotGetUses

-

 

The sanitydelta event is pushed to the player, not items.

player_inst:ListenForEvent("sanitydelta", function(inst, data)	-- inst == player_inst because of function parameter above		item_inst.components.finiteuses:SetUses(item_inst.components.finiteuses:GetUses()+1)end)
Link to comment
Share on other sites

Thanks for your help

I've tried putting your code like so:

    local function OnEquip(inst, owner)        owner.AnimState:OverrideSymbol("swap_object", "swap_darkorb", "swap_darkorb")        owner.AnimState:Show("ARM_carry")        owner.AnimState:Hide("ARM_normal")		player_inst:ListenForEvent("sanitydelta", function(inst, data)			-- inst == <span class="searchlite">player_inst</span> because of function parameter above     			item_inst.components.finiteuses:SetUses(item_inst.components.finiteuses:GetUses()+1)		end)    end

but I got the error "variable 'player_inst' is not declared"

So I changed player_inst to owner and it came up with the error "variable 'item_inst' is not declared". I'm guessing I am supposed to replace item_inst and player_inst with something else, but I don't know what. And even if the way I used your code did work I feel like it might just happen once when the item is first equipped.

 

If you could help with the whole item_inst player_inst thing that would be great

Link to comment
Share on other sites

@Virtualistik I used player_inst and item_inst to describe what the variable should refer to. In your code, those variables don't exist. You need to use a variable that refers to the player in place of player_inst, and one for the item in place of item_inst.

local function OnEquip(inst, owner)	owner.AnimState:OverrideSymbol("swap_object", "swap_darkorb", "swap_darkorb")	owner.AnimState:Show("ARM_carry")	owner.AnimState:Hide("ARM_normal")		-- owner = player instance	-- inst = item instance	-- Both are received from the parameters if OnEquip		local item_inst = inst -- for use below		-- Attach event listener to the player instance	owner:ListenForEvent("sanitydelta", function(inst, data)		-- owner = player instance (same as before)		-- inst = player instance (from this function's parameters)		-- item_inst = item instance				item_inst.components.finiteuses:SetUses(item_inst.components.finiteuses:GetUses()+1)	end)end

-

Alternately, you could also just get rid of or change the names of the parameters of the inner function and use the inst variable. If that makes it more confusing, just ignore it and stick with the original idea above.

 

 

More on event listeners. The code attached to the listener will be executed every time the sanitydelta event is pushed. You can see from sanity.lua that it's pushed whenever the sanity level is changed.

Link to comment
Share on other sites

@Blueberrys Well that code kinda works, but it seems that once the listener is active the item's uses will just constantly increment no matter whether the sanity changes. The item continues to do this whether it is equipped or not, even if it is on the ground. So I have a couple of questions:

 

Could you show me how to make the code only trigger when the sanity actually changes (preferably decreases)?

and

How would I cancel the listener so that it doesn't cause the item uses to increase any further?

 

It also starts a new listener each time the item is equipped, leading to some quite ridiculous consequences:

VgrSAbr.png

Link to comment
Share on other sites

Could you show me how to make the code only trigger when the sanity actually changes (preferably decreases)?

The data passed with the event can be used to check it.

owner:ListenForEvent("sanitydelta", function(inst, data)	if data.newpercent < data.oldpercent then		-- Sanity decreased	endend)

Alternately, you could use your own variable to store the oldpercent.

 

 

How would I cancel the listener so that it doesn't cause the item uses to increase any further?

Using the RemoveEventCallback function.

local function SanityDelta(inst, data)	if data.newpercent < data.oldpercent then		-- Sanity decreased	endend-- Start listeningowner:ListenForEvent("sanitydelta", SanityDelta)-- Stop listeningowner:RemoveEventCallback("sanitydelta", SanityDelta)

-

 

It also starts a new listener each time the item is equipped, leading to some quite ridiculous consequences:

If you remove the event upon unequipping, it won't be a problem. You could also use a boolean flag just in case.

-- ...local listening = falselocal function OnEquip(inst, owner)	if not listening then		owner:ListenForEvent("sanitydelta", SanityDelta)	end		-- ...end-- When unequpped	owner:RemoveEventCallback("sanitydelta", SanityDelta)	listening = false
Link to comment
Share on other sites

@Blueberrys, Thanks, the first bit of code works and now the uses only changes when the players sanity decreases, but the listener ending thing doesn't seem to be working. There is no error, it just doesn't end the listener.

    local inst = CreateEntity()    local trans = inst.entity:AddTransform()    local anim = inst.entity:AddAnimState()    local sound = inst.entity:AddSoundEmitter()    MakeInventoryPhysics(inst)		inst.OnSanityDelta = function(inst, data, item_inst)		if data.newpercent < data.oldpercent then			item_inst.components.finiteuses:SetUses(item_inst.components.finiteuses:GetUses()+1)		end	end		local function OnEquip(inst, owner)		owner.AnimState:OverrideSymbol("swap_object", "swap_darkorb", "swap_darkorb")		owner.AnimState:Show("ARM_carry")		owner.AnimState:Hide("ARM_normal")     		-- owner = player instance		-- inst = item instance		-- Both are received from the parameters if OnEquip     		local item_inst = inst -- for use below	 		-- Attach event listener to the player instance		owner:ListenForEvent("sanitydelta", function(inst, data)			item_inst.OnSanityDelta(inst, data, item_inst)		end)	end	    local function OnUnequip(inst, owner)        owner.AnimState:Hide("ARM_carry")        owner.AnimState:Show("ARM_normal")				local item_inst = inst				owner:RemoveEventCallback("sanitydelta", function(inst, data)			item_inst.OnSanityDelta(inst, data, item_inst)		end)	end	
Link to comment
Share on other sites

Thanks that worked perfectly. Here's my final code if anyone is interested:

    local inst = CreateEntity()    local trans = inst.entity:AddTransform()    local anim = inst.entity:AddAnimState()    local sound = inst.entity:AddSoundEmitter()    MakeInventoryPhysics(inst)		local item_inst = inst		inst.OnSanityDelta = function(inst, data)		if data.newpercent < data.oldpercent then			item_inst.components.finiteuses:SetUses(item_inst.components.finiteuses:GetUses()+1)		end	end	    local function OnEquip(inst, owner)	owner.AnimState:OverrideSymbol("swap_object", "swap_darkorb", "swap_darkorb")	owner.AnimState:Show("ARM_carry")	owner.AnimState:Hide("ARM_normal")     	-- owner = player instance	-- inst = item instance	-- Both are received from the parameters if OnEquip	-- Attach event listener to the player instance	owner:ListenForEvent("sanitydelta", item_inst.OnSanityDelta)    end	    local function OnUnequip(inst, owner)        owner.AnimState:Hide("ARM_carry")        owner.AnimState:Show("ARM_normal")			owner:RemoveEventCallback("sanitydelta", item_inst.OnSanityDelta)    end	    inst:AddComponent("equippable")    inst.components.equippable:SetOnEquip( OnEquip )    inst.components.equippable:SetOnUnequip( OnUnequip )    inst.components.equippable.dapperness = -5    inst:AddComponent("finiteuses")    inst.components.finiteuses:SetMaxUses(500)    inst.components.finiteuses:SetUses(0)
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.

×
  • Create New...