Amalleus Posted February 23, 2017 Share Posted February 23, 2017 Hello. Need some advice or help on next problem. There are components "edible" and "finiteuses". Is there a way to make food with for example, 3 uses? I assigned these components to item and when i try to use it as eat - it dissapears without using charges. I have thoughts to make straightforward solution to summon this item again with function OnEaten with less charges but this is a little bit stupid. Is there other way to combine "edible" and "finiteuses"? Link to comment Share on other sites More sharing options...
RandomFace Posted February 23, 2017 Share Posted February 23, 2017 In eater.lua -> function Eater:Eat( food ) If you scroll down you can see: if food.components.stackable and food.components.stackable.stacksize > 1 and not self.eatwholestack then food.components.stackable:Get():Remove() else food:Remove() end Maybe you could override the Eater:Eat function and add a check for your item name and it's finite uses. If >1 (or 0?) reduce finite uses and skip removing food, otherwise remove food as normal? Link to comment Share on other sites More sharing options...
Amalleus Posted February 23, 2017 Author Share Posted February 23, 2017 4 hours ago, RandomFace said: In eater.lua -> function Eater:Eat( food ) If you scroll down you can see: if food.components.stackable and food.components.stackable.stacksize > 1 and not self.eatwholestack then food.components.stackable:Get():Remove() else food:Remove() end Maybe you could override the Eater:Eat function and add a check for your item name and it's finite uses. If >1 (or 0?) reduce finite uses and skip removing food, otherwise remove food as normal? Thanks for idea. I made this code from your help: if food.components.edible and food.components.finiteuses and food.prefab == "estus_full" then local useAmount = food.components.finiteuses:GetUses() if (useAmount > 1) then food.components.finiteuses:Use(1) elseif (useAmount == 1) then food.components.finiteuses:Use(1) food:Remove() self.inst.components.inventory:GiveItem(SpawnPrefab("estus_empty")) end end This willl allow me to do functions which i need. Another question: I am really newbie in Lua and so could you also tell me how to add this code in Eater:Eat function? I don't understand how "AddPrefabPostInit( "eater", somefunction )" works. Link to comment Share on other sites More sharing options...
RandomFace Posted February 24, 2017 Share Posted February 24, 2017 Not sure if it's the best way to do it but I decided to redirect ACTIONS.EAT.fn in actions.lua instead of the eater:eat function Try it in your items prefab (local fn) inst:AddComponent("finiteuses") inst.components.finiteuses:SetMaxUses(3) inst.components.finiteuses:SetUses(3) local ActionsEat_old = ACTIONS.EAT.fn ACTIONS.EAT.fn = function(act) local obj = act.target or act.invobject if act.doer.components.eater and obj and obj.components.edible then if obj.prefab=="estus_full" then local useAmount = obj.components.finiteuses:GetUses() if (useAmount > 1) then --Decrease finiteuses and give potion bonuses obj.components.finiteuses:Use(1) act.doer.components.sanity:DoDelta(10) act.doer.components.health:DoDelta(10) return true --Return true so the character doesnt say "can't do that" elseif (useAmount == 1) then obj.components.finiteuses:Use(1) act.doer.components.inventory:GiveItem(SpawnPrefab("estus_empty")) act.doer.components.sanity:DoDelta(10) act.doer.components.health:DoDelta(10) return act.doer.components.eater:Eat(obj) --Here we allow the game to consume the item as normal end else return act.doer.components.eater:Eat(obj) --Not our item, do normal stuff end end end I added the sanity / health increase as an example. Link to comment Share on other sites More sharing options...
Amalleus Posted February 24, 2017 Author Share Posted February 24, 2017 7 hours ago, RandomFace said: Not sure if it's the best way to do it but I decided to redirect ACTIONS.EAT.fn in actions.lua instead of the eater:eat function Try it in your items prefab (local fn) inst:AddComponent("finiteuses") inst.components.finiteuses:SetMaxUses(3) inst.components.finiteuses:SetUses(3) local ActionsEat_old = ACTIONS.EAT.fn ACTIONS.EAT.fn = function(act) local obj = act.target or act.invobject if act.doer.components.eater and obj and obj.components.edible then if obj.prefab=="estus_full" then local useAmount = obj.components.finiteuses:GetUses() if (useAmount > 1) then --Decrease finiteuses and give potion bonuses obj.components.finiteuses:Use(1) act.doer.components.sanity:DoDelta(10) act.doer.components.health:DoDelta(10) return true --Return true so the character doesnt say "can't do that" elseif (useAmount == 1) then obj.components.finiteuses:Use(1) act.doer.components.inventory:GiveItem(SpawnPrefab("estus_empty")) act.doer.components.sanity:DoDelta(10) act.doer.components.health:DoDelta(10) return act.doer.components.eater:Eat(obj) --Here we allow the game to consume the item as normal end else return act.doer.components.eater:Eat(obj) --Not our item, do normal stuff end end end I added the sanity / health increase as an example. I see. Actions.eat firstly looks forward my item and then if it's not my redirect to usual action of eating. Good one. Tested, it is working. Thanks for helping out Link to comment Share on other sites More sharing options...
RandomFace Posted February 24, 2017 Share Posted February 24, 2017 No problem. One thing that occurred to me; In the above code we give the estus_empty bottle before allowing the full one to be removed. This probably isn't a good idea in a situation where the players inventory is full. I would change it to remove the estus_full before giving the estus_empty. Link to comment Share on other sites More sharing options...
Amalleus Posted February 25, 2017 Author Share Posted February 25, 2017 20 hours ago, RandomFace said: No problem. One thing that occurred to me; In the above code we give the estus_empty bottle before allowing the full one to be removed. This probably isn't a good idea in a situation where the players inventory is full. I would change it to remove the estus_full before giving the estus_empty. that's right. because if you will give something it will appear on cursor if inventory is full or on the floor if there is some item on cursor also. i already did this, thanks Link to comment Share on other sites More sharing options...
Recommended Posts
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.