ViewtifulDom Posted May 28, 2015 Share Posted May 28, 2015 I'm creating a character with the following perk: *Eats a lot, but not often. For this, I have doubled the characters max hunger and decreased the hunger rate by 25%. To compensate, I want him to receive 25% less hunger from anything he eats. I've tried the following ways to accomplish this:The 'oneat' function (doesn't affect the actual eating, just fires when performed)Editing the GetHunger function (affected all characters despite the clause) see: http://forums.kleien...etables-values/Editing the 'Eat' function. I'm not sure how sensible that was in the first place.Looking for advice or tutelage on the subject. More information can be provided if necessary. Thank you! - Dominic Link to comment Share on other sites More sharing options...
Seiai Posted May 28, 2015 Share Posted May 28, 2015 (edited) @ViewtifulDom,the eatercomponent pushes this when u eat something:self.inst:PushEvent("oneat", {food = food})so u can just add a listener to your character, read out the eating value of the fooditem, which is passed along with the event, and just reduce your hunger by a fraction of it. Edited May 28, 2015 by Seiai Link to comment Share on other sites More sharing options...
DarkXero Posted May 28, 2015 Share Posted May 28, 2015 Modifying GetHunger should have worked.I imagine you replaced GetPlayer with ThePlayer, or left it alone, as despite being deprecated, it works. Since the code runs in the server, ThePlayer is always the host.So in your case, either everybody would have the hunger modification, or nobody would, depending on the host. The correct edible modification:-- in modmainAddComponentPostInit("edible", function(self) local old = self.GetHunger self.GetHunger = function(self, eater) return old(self, eater) * ((eater.prefab == "wilson" and 0.75) or 1) endend)The Seiai's approach:-- in master_postinit of character's prefabinst:ListenForEvent("oneat", function(src, data) src.components.hunger:DoDelta(-0.25 * data.food.components.edible:GetHunger(src), true)end) Link to comment Share on other sites More sharing options...
ViewtifulDom Posted July 15, 2015 Author Share Posted July 15, 2015 @DarkXero Months of frustration, and it was six simple lines. Thanks, man. On to the last lingering touches, and I've got myself a mod. Link to comment Share on other sites More sharing options...
ViewtifulDom Posted October 14, 2015 Author Share Posted October 14, 2015 @DarkXero Hey, so I've encountered an error in this code afterall:AddComponentPostInit("edible", function(self) local old = self.GetHunger self.GetHunger = function(self, eater) return old(self, eater) * ((eater.prefab == "domo" and .75) or 1) endend)It seems to work properly with all the characters, Domo takes less hunger from food and Wilson does not, but as soon as you try to feed something to a pig, it breaks down.[00:00:53]: [string "../mods/domo/modmain.lua"]:9: attempt to index local 'eater' (a nil value)LUA ERROR stack traceback: ../mods/domo/modmain.lua:9 in (method) GetHunger (Lua) <8-10> scripts/prefabs/pigman.lua:65 in (field) onaccept (Lua) <55-86> scripts/components/trader.lua:113 in (method) AcceptGift (Lua) <90-125> scripts/actions.lua:732 in (field) fn (Lua) <726-735> scripts/bufferedaction.lua:23 in (method) Do (Lua) <19-33> scripts/entityscript.lua:1199 in (method) PerformBufferedAction (Lua) <1191-1209> scripts/stategraphs/SGwilson.lua:2971 in (field) fn (Lua) <2970-2972> scripts/stategraph.lua:562 in (method) UpdateState (Lua) <530-574> scripts/stategraph.lua:601 in (method) Update (Lua) <593-621> scripts/stategraph.lua:123 in (method) Update (Lua) <107-146> scripts/update.lua:205 in () ? (Lua) <146-219>It's apparently losing track of the eater component at some point, but I can't figure out why or where. Any idea why it's getting hung up? Link to comment Share on other sites More sharing options...
DarkXero Posted October 14, 2015 Share Posted October 14, 2015 @ViewtifulDom, becauseitem.components.edible:GetHunger()in line 65. eater is nil when the pig calls it.AddComponentPostInit("edible", function(self) local old = self.GetHunger self.GetHunger = function(self, eater) return old(self, eater) * ((eater and eater.prefab == "domo" and .75) or 1) endend) Link to comment Share on other sites More sharing options...
ViewtifulDom Posted October 15, 2015 Author Share Posted October 15, 2015 (edited) @DarkXero I see what you mean. I need to find a way to retrieve the eater without requiring it to be passed to the function. Can you suggest anything? Edited October 15, 2015 by ViewtifulDom Link to comment Share on other sites More sharing options...
DarkXero Posted October 15, 2015 Share Posted October 15, 2015 I see what you mean. I need to find a way to retrieve the eater without requiring it to be passed to the function. Can you suggest anything? I suggestedAddComponentPostInit("edible", function(self) local old = self.GetHunger self.GetHunger = function(self, eater) return old(self, eater) * ((eater and eater.prefab == "domo" and .75) or 1) endend)If eater is nil, theneater and eater.prefab == "domo" and .75gets evaluated to nil, thereforenil or 1gets evaluated to 1. Thus, no more crashes. Link to comment Share on other sites More sharing options...
ViewtifulDom Posted October 15, 2015 Author Share Posted October 15, 2015 @DarkXero I'm not sure why that part got lost, but you're right, that fixed it up nice. Thank you very much! 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