Garamonde

How to remove health gain when eating non-meat foods [SOLVED]

Recommended Posts

Garamonde    38

So I have an issue I ran into when someone was helping me make it so all non-meat food causes damage to my character; we have achieved this, but now we're stuck with the issue of that same non-meat food also healing them at the same time. So, say my character eats petals, he will take 5 damage but also heal 1 health at the same time. I want to remove the healing aspect for all non-meat food, but keep the sanity healing intact.

I have attached my character's .lua, I hope that is enough. If not, let me know and I will attach the entire mod's folder.

Thanks!

 

garamonde.lua

Edited by Garamonde
Made the thread just for a single-subject and uploaded updated character lua.

Share this post


Link to post
Share on other sites
local function OnEat(inst, food)
      local foodhp = food and food.components.edible and food.components.edible:GetHealth(inst) or nil
      if food and food.components.edible and not food.components.edible.foodtype == "meat" then
         return
      elseif foodhp > 0 then
         --if health value is minus then it should not work(because it will heal character)
         inst.components.health:DoDelta(-foodhp)
         inst.components.health:DoDelta(-5)
         --first, food heal you(+hp) and first code will deal damage(-hp = 0), and second one will deal another damage (-5 in final)
      else
		 inst.components.health:DoDelta(-5)
         --it will work even food damages you
      end
end

this will not stop "healing", so it could show pulsing green visual effect and playing "heal" sound effect, but finally code will work as intended and deal 5 damage to your health if non-meat food is eaten.

Edited by Combustiblemon
  • Thanks 1

Share this post


Link to post
Share on other sites
IronHunter    570

To prevent the green circle and heal sound, you'll need to extend the function for Eat in the eater component for your character, you can then check what the food you are eating before it is eaten. Disable/Enable healthabsorption and then play the original function. The oneat function you are using plays after the food is already eaten and such can't modify the past.

  • Like 1
  • Thanks 1

Share this post


Link to post
Share on other sites
Garamonde    38
5 hours ago, IronHunter said:

To prevent the green circle and heal sound, you'll need to extend the function for Eat in the eater component for your character, you can then check what the food you are eating before it is eaten. Disable/Enable healthabsorption and then play the original function. The oneat function you are using plays after the food is already eaten and such can't modify the past.

I'm still learning lua so I'm struggling to understand how to do this. How do I extend the eater component and how do I tell it to check for meat food specifically? I tried a few different ways but they all led to a crash that doesn't really inform me of how to fix it. Also, by healthabsorption are you referring to this portion here?

local function OnEat(inst, food)
      if food and food.components.edible and not food.components.edible.foodtype == "meat" then
      
	  else
		 inst.components.health:DoDelta(-5)
      end
end

Sorry if this seems kind of obvious, I am trying very hard to learn and I've barely ever touched code in my life so I'm getting stuck on such small things as spacing, what words/symbols I can use and how it all ties together. Thank you for your response though!

Share this post


Link to post
Share on other sites
IronHunter    570
Spoiler
43 minutes ago, Garamonde said:

I'm still learning lua so I'm struggling to understand how to do this. How do I extend the eater component and how do I tell it to check for meat food specifically? I tried a few different ways but they all led to a crash that doesn't really inform me of how to fix it. Also, by healthabsorption are you referring to this portion here?



local function OnEat(inst, food)
      if food and food.components.edible and not food.components.edible.foodtype == "meat" then
      
	  else
		 inst.components.health:DoDelta(-5)
      end
end

Sorry if this seems kind of obvious, I am trying very hard to learn and I've barely ever touched code in my life so I'm getting stuck on such small things as spacing, what words/symbols I can use and how it all ties together. Thank you for your response though!

 

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

 

  • Like 1
  • Thanks 1

Share this post


Link to post
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