Jump to content

Recommended Posts

I'm making my first ever mod for like, anything, and I have pretty much no programming experience. I'm making a bunnyman character and I need help with the code for giving him the following traits.

No problems with stale or spoiled food (like WX)

-25% of the benefits from any Meat items

+25% damage when under 30% sanity

Thanks in advance.

Welcome to the world of DS/DST modding :)

If you value your time on this planet, start by reading through the newcomer post. It will save you a lot of grief getting started.

I already have an extensive array of posts for all kinds of stuff, including what you want for your character, so I'll just link you those. I did, however, rewrite the example I had in the post for the first perk, since the code could use a cleaning, anyway.

Perk 1. You need to read the stuff at the top of this post, then find "Applying Changes Specific To Your Own Character Mod" and then "Changing Food Values Based On Foodtype (CHARACTER)". You can change the food values however you'd like.

Code snippet (hopefully fully working, but you need to read the post to know how to use it):

Spoiler

-- This is the ONLY function you should be making changes to.
local calculateFoodValues(player, food)
	-- We want the caller of this function to be told whether our code made changes to the food.
	-- Therefore, we also send back a bool, called changesweremade, which we set to true if we change anything.
	-- In this case it's very simple. If we find the food in our food_stats, we will make changes to it.
	local changesweremade = false
	
	-- Local variables to hold our food values.
	local healthval, hungerval, sanityval = 0, 0, 0
	
	---------- ONLY EDIT BELOW THIS LINE ----------
	
	-- HERE you calculate the values you want this food to have.
	
	-- In this example, we just want to lower beneficial effects of MEAT.
	
	local edible_comp = food.components.edible
	
	if edible_comp and edible_comp.foodtype == "MEAT" then
		changesweremade = true
		
		-- We just want to lower any beneficial stat-effects of MEAT,
		-- so, e.g., if healthvalue is negative, we will still use it. Otherwise, we do our calculation.
		healthval = edible_comp.healthvalue < 0 and edible_comp.healthvalue or edible_comp.healthvalue * 0.75
		hungerval = edible_comp.hungervalue < 0 and edible_comp.hungervalue or edible_comp.hungervalue * 0.75
		sanityval = edible_comp.sanityvalue < 0 and edible_comp.sanityvalue or edible_comp.sanityvalue * 0.75
	end
	
	---------- ONLY EDIT ABOVE THIS LINE ----------
	
	-- Return the results.
	return changesweremade, healthval, hungerval, sanityval
end

local old_Eat = inst.components.eater.Eat
inst.components.eater.Eat =  function(self, food)
	-- Make a local variable holding the edible component of the food (optimization).
	local edible_comp = food.components.edible

	-- Make a local variable saying whether we made changes to the food.
	local changesweremade = false
	
	-- If the food has an edible component...
	if edible_comp then
		-- Local variables to hold the new food values.
		local healthval, hungerval, sanityval
		
		-- Calculate the food values, and let us know if changes were made to them.
		changesweremade, healthval, hungerval, sanityval = calculateFoodValues(self.inst, food)
		
		if changesweremade then
			-- We first save the original food values, since we want to reset them after changing them temporarily for our character.
			edible_comp.originalhealthvalue = edible_comp.healthvalue
			edible_comp.originalhungervalue = edible_comp.hungervalue
			edible_comp.originalsanityvalue = edible_comp.sanityvalue
			
			-- We change the food to have our new stat values, and default to 0 if the stat was omitted from the dictionary entry.
			edible_comp.healthvalue = healthval
			edible_comp.hungervalue = hungerval
			edible_comp.sanityvalue = sanityval
		end
	end
	
	-- Call the original Eat function, while the food has our new values, and save the result in a variable.
	local returnvalue = old_Eat(self, food)
	
	-- If we made changes to the food, and the food is still valid (meaning it has not been destroyed
	-- because it was the last in the stack), and the edible component is still accessible...
	if food:IsValid() and changesweremade then
		-- We reset the food values after eating it.
		edible_comp.healthvalue = edible_comp.originalhealthvalue
		edible_comp.hungervalue = edible_comp.originalhungervalue
		edible_comp.sanityvalue = edible_comp.originalsanityvalue
		
		-- Remove the temporary values from the food to save memory.
		edible_comp.originalhealthvalue = nil
		edible_comp.originalhungervalue = nil
		edible_comp.originalsanityvalue = nil
	end
	
	-- Then we return the value returned by the original Eat function.
	return returnvalue
end

-- This last piece of code will make the food values display correctly when using the popular ShowMe mod.
inst.FoodValuesChanger = function(player, food)
	local changesweremade, healthval, hungerval, sanityval = calculateFoodValues(food)
	if changesweremade then
		return healthval, hungerval, sanityval
	end
	local e = food.components.edible
	return e.healthvalue, e.hungervalue, e.sanityvalue
end

 

Perk 2. 

 

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