Virtualistik Posted June 8, 2015 Share Posted June 8, 2015 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)endBut 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 https://forums.kleientertainment.com/forums/topic/54998-listen-for-event-sanitydelta-for-owner-of-equipped-item/ Share on other sites More sharing options...
Blueberrys Posted June 8, 2015 Share Posted June 8, 2015 (edited) @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) Edited June 8, 2015 by Blueberrys Link to comment https://forums.kleientertainment.com/forums/topic/54998-listen-for-event-sanitydelta-for-owner-of-equipped-item/#findComment-645351 Share on other sites More sharing options...
Virtualistik Posted June 9, 2015 Author Share Posted June 9, 2015 Thanks for your helpI'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) endbut 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 https://forums.kleientertainment.com/forums/topic/54998-listen-for-event-sanitydelta-for-owner-of-equipped-item/#findComment-645475 Share on other sites More sharing options...
Virtualistik Posted June 9, 2015 Author Share Posted June 9, 2015 @Blueberrys (Sorry for double post, I should have mentioned you in the last post and I don't know how to edit posts) Link to comment https://forums.kleientertainment.com/forums/topic/54998-listen-for-event-sanitydelta-for-owner-of-equipped-item/#findComment-645488 Share on other sites More sharing options...
Blueberrys Posted June 9, 2015 Share Posted June 9, 2015 @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 https://forums.kleientertainment.com/forums/topic/54998-listen-for-event-sanitydelta-for-owner-of-equipped-item/#findComment-645564 Share on other sites More sharing options...
Virtualistik Posted June 9, 2015 Author Share Posted June 9, 2015 @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)?andHow 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: Link to comment https://forums.kleientertainment.com/forums/topic/54998-listen-for-event-sanitydelta-for-owner-of-equipped-item/#findComment-645592 Share on other sites More sharing options...
Blueberrys Posted June 9, 2015 Share Posted June 9, 2015 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 https://forums.kleientertainment.com/forums/topic/54998-listen-for-event-sanitydelta-for-owner-of-equipped-item/#findComment-645610 Share on other sites More sharing options...
Virtualistik Posted June 10, 2015 Author Share Posted June 10, 2015 @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 https://forums.kleientertainment.com/forums/topic/54998-listen-for-event-sanitydelta-for-owner-of-equipped-item/#findComment-645907 Share on other sites More sharing options...
Blueberrys Posted June 10, 2015 Share Posted June 10, 2015 @Virtualistik That's because you're not referring to the same function, but two functions different which are doing the same thing.Note that in my code I assigned the function to a variable and passed that variable to ListenForEvent and RemoveEventCallback functions. Link to comment https://forums.kleientertainment.com/forums/topic/54998-listen-for-event-sanitydelta-for-owner-of-equipped-item/#findComment-645911 Share on other sites More sharing options...
Virtualistik Posted June 19, 2015 Author Share Posted June 19, 2015 (edited) 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) Edited June 19, 2015 by Virtualistik Link to comment https://forums.kleientertainment.com/forums/topic/54998-listen-for-event-sanitydelta-for-owner-of-equipped-item/#findComment-648302 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