Jump to content

Recommended Posts

Alright so this is a weird set of questions to be pairing together but they kind of go hand-in-hand. Hopefully they'll be simple enough. I have attached my character's prefab file and will be referencing it in this post.

Question A: As shown below, I am trying to make my character say certain things when he eats any foodtype other than meat or pigskin. The problem is, even though my friend and I have tried making exceptions for meat and pigskin, my character is still saying the "badfoodresponses" for those two food groups (if pigskin can even be called a food group). I have looked in eater.lua, edible.lua, pigskin.lua, basically anything pertaining to food or pigskin that I could find in the game's files.

This is near the top of the file:

local eatbadfoodresponses = {
    "Ooooh... My belly...",
    "My body can't digest that...",
    "URK! I probably shouldn't have eaten that...",
}

local function oneat(inst, food)
    print("Running oneat function")
    if food ~= nil then -- Make sure food is not nil for whatever case that is even possible
		if food.prefab == "pigskin" then -- Is the food pigskin?
			print("eating pigskin")
            inst.components.health:DoDelta(10) -- Add 10 health
        elseif food.components ~= nil and food.components.edible ~= nil and food.components.edible.foodtype == "meat" then -- Does the food have the edible component and is meat
			print("eating meat")
		-- Do nothing, we have a different area of code managing this.
        else -- Otherwise if it was not pigskin or meat
			print("eating other")
			inst.components.talker:Say(eatbadfoodresponses[math.random(#eatbadfoodresponses)]) -- Say that the food did not agree with gara.
        end
    end -- If we put an else here we can have something special for the edge-case of food being nil.
end
local _Eat = inst.components.eater.Eat --backup original function
    inst.components.eater.Eat = function(self, food, feeder, ...) --override original function
    if food and food.components.edible and food.components.edible.foodtype ~= "MEAT" then
        inst.components.health:DoDelta(-5)
        self.healthabsorption = 0 --lower our healthabsorption to 0 so we get no health from nonmeats
end
local result = _Eat(self, food, feeder, ...) --call backuped function copy of original
  self.healthabsorption = 1 --return it to default
  return result --return the results from _Eat
end

^ And this is near the bottom. As you can see, anything non-meat is supposed to deal 5 damage to Garamonde, while actual meat food does not lower his health or sanity at all. Which brings me to Question B:

As you can also see, we have made it so Garamonde can eat pigskin and, much like spiders, he is supposed to heal 10 health from it. Instead, it's being treated like a non-meat item and removing 5 health. On another note, pigskin nor meat is being registered when it's printed in console. They're only being treated as "other" food.

I hope that's enough information and that anybody at all can help. Me nor my friend can figure this out.

garamonde.lua

Edited by Garamonde
Forgot some info on the prints.

I'd suggest changing this part:

43 minutes ago, Garamonde said:

elseif food.components ~= nil and food.components.edible ~= nil and food.components.edible.foodtype == "meat" then

To:

elseif food.components ~= nil and food.components.edible ~= nil and food.components.edible.foodtype == FOODTYPE.MEAT then

As it makes it more compatible if Klei ever changes the "MEAT" food type name (for some unknown reason).

Also, don't use ListenForEvent to test what he's eating. You should use:

inst.components.eater:SetOnEatFn(oneat)

I tested it, and using SetOnEatFn actually makes the character detect the right food.

  • GL Happy 1

It works! However now pigskin is... healing 10 but also dealing 5 damage at the same time? So he's only gaining a net value of 5 health, as I screenshotted here:

unknown.png

I also attached the newest revision of my prefab file.

Thank you!

garamonde.lua

Edited by Garamonde

Huh. Couldn't you just test if the food prefab is not "pigskin"?

Like this:

local _Eat = inst.components.eater.Eat --backup original function
	inst.components.eater.Eat = function(self, food, feeder, ...) --override original function
		if food ~= nil and not (food.prefab == "pigskin" or (food.components.edible ~= nil and food.components.edible.foodtype == FOODTYPE.MEAT)) then -- This tests if the food prefab is not Pig Skin, or if the food is not meat.
			inst.components.health:DoDelta(-5)
			self.healthabsorption = 0 --lower our healthabsorption to 0 so we get no health from nonmeats
		end
		local result = _Eat(self, food, feeder, ...) --call backuped function copy of original
		self.healthabsorption = 1 --return it to default
		return result --return the results from _Eat
	end

It took me a surprisingly long time to get to this.

Edited by HarryPPP
  • Thanks 1
14 minutes ago, HarryPPP said:

Huh. Couldn't you just test if the food prefab is not "pigskin"?

Sorry, I am still pretty new to modding, so I don't really know to do these things. I am learning but I simply do not know what to do.

So the good news is that console is finally detecting that the eaten item is pigskin with that code, however now the healing values are more messed up; they heal now but by 20 instead of 10...

You don't know how much I appreciate the help, though.:wickerbottomthanks:

Wow! I actually didn't consider that. I removed the DoDelta component to test not having a "bonus number" show up in front of the 10, and now everything works perfectly! Thank you! :D

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
×
  • Create New...