Jump to content

Need Help with Custom Hunger Values from Food for Character


Recommended Posts

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

@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 by Seiai
Link to comment
Share on other sites

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

@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

@ViewtifulDom, because

item.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

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 suggested

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)

If eater is nil, then

eater and eater.prefab == "domo" and .75

gets evaluated to nil, therefore

nil or 1

gets evaluated to 1.

 

Thus, no more crashes.

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...