Jump to content

Recommended Posts

Hi everybody,

 

My name is Inward, I'm a software developper in France.

I'm a Don't Starve player almost since its release and now I would like to create my own mods.

 

I started with a custom character with a friend studying graphism art.

 

I almost did all the job and now I'm adding some powers to my character (I will give more details about him on a new thread when he will be able to play).

 

I want him to eat monster meat, so I added :

 

character.lua :

inst.components.eater.strongstomach = true

But my character hates vegetables. What I want to do is that he can eat them, but their hunger values are 0.

So I can keep their health and sanity bonus/malus, but the hunger value is always empty.

 

I already did that :

 

modmain.lua :

AddComponentPostInit("edible", function(Edible, inst) local oldGetHunger = Edible.GetHungerfunction Edible:GetHunger(eater)	local multiplier = 1		local ignore_spoilage = (eater and eater.components.eater and eater.components.eater.ignoresspoilage) or self.hungervalue < 0		if self.inst.components.perishable and not ignore_spoilage then		if self.inst.components.perishable:IsStale() then			multiplier = self.stale_hunger		elseif self.inst.components.perishable:IsSpoiled() then			multiplier = self.spoiled_hunger		end	end		if self.foodtype == "VEGGIE" or self.foodtype == "SEEDS" then		self.hungervalue = 0	end	return oldGetHunger(self, eater)	--return multiplier*(self.hungervalue)endend)

It works well, I surcharge the GetHunger() function from the Edible class, adding what I want. But now there is a problem : this edit is enabled for all the characters (example : because of this modification, Wilson can't eat vegetables, same for the others).

 

I would like to give this "power" only to my custom character.

 

I have looked for a solution for severals hours now and I still don't find it.

 

Can anyone help me please ?

 

Thanks in advance

if self.foodtype == "VEGGIE" or self.foodtype == "SEEDS" then        self.hungervalue = 0    end

since this is the only part that seems to currently be getting used, just place it inside another if clause that checks if the player has a  'hatesveggies' tag. Of course then you would need to add that tag to your character.  Alternately you could just check the character prefab name but that is less extensible.

Thank you very much for your answer !

 

I already found the solution about checking the prefab character, but I guess you're right that this solution is less extensible.

I added a tag in character.lua :

local fn = function(inst)	inst:AddTag("hatesVegetables")		-- other stuff hereend

And then I check if the character has this tag in my modmain.lua :

GetPlayer = GLOBAL.GetPlayerAddComponentPostInit("edible", function(Edible, inst)local oldGetHunger = Edible.GetHungerfunction Edible:GetHunger(eater)	local multiplier = 1	local character = GetPlayer()	if character:HasTag("hatesVegetables") then		if self.foodtype == "VEGGIE" or self.foodtype == "SEEDS" then			multiplier = 0		elseif self.foodtype == "MEAT" then			multiplier = 1.5		end	end	return (multiplier * oldGetHunger(self, eater))endend)

You can also notice that I changed a bit the way to change the food value : I change a multiplier variable and then I return the old value of the item with a multiplication.

The old way (changing directly the hungervalue) caused some bugs : if I would multiply it (for example hungervalue = hungervalue * 2), each time the function is called, the value is again changed with the previous value.

A "meat" has 25 hungervalue.

First call : 50

Second call : 100

With a multiplier, we always return multiplier * 25 (because 25 is never changed)

 

I hope it can help.

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