Jump to content

How do I make my character able to eat grass


Recommended Posts

So I decided to try and make my own custom character for don't starve together and it was all going well until I hit a wall - I have no idea how to make it so that he either is able to eat cut grass in his inventory with right click, or just eat grass off of the ground by right clicking it.

 

Would anyone be able to show me how I would code this? The character's name is "InvinciBULL"

 

My steam name is also invicnibull so you could contact me there if it's complicated, and I would really appreciate it if someone could mentor me about how I could do some more advanced modding, like making custom items.

 

Thanks :encouragement:

Link to comment
Share on other sites

@InvinciBULL, try something like the following.

 

-- This goes in modmain.lua

GrassPostInit = function(inst)

    inst:AddComponent("edible")

    inst.components.edible.ismeat = false    
    inst.components.edible.foodtype = FOODTYPE.VEGGIE
    inst.components.edible.healthvalue = -TUNING.HEALING_SMALL
    inst.components.edible.hungervalue = TUNING.CALORIES_SMALL
    inst.components.edible.sanityvalue = -TUNING.SANITY_SMALL
 
   return inst
end
 
AddComponentPostInit("cutgrass", GrassPostInit)
 

This makes it so that every one can eat grass. If you're wanting to make it so that only your character can eat grass you will need to modify this a little.

 

Link to comment
Share on other sites

@Kzisor

 

Thank you for the help, but unfortunately after trying multiple times I can't seem to get the character to eat the grass, the right mouse click instead still only being used to examine it in the inventory.

 If it is it possible to replace the examine option witht he eat option for the character, would it also be possible to limit the amount of grass that could be eaten each day, or maybe have it so grass can spoil?

Link to comment
Share on other sites

@InvinciBULL, Grass actually already has an edible component, and foodtype wood. You can do something like what was used for Wark:

 

To add the food type:

GLOBAL.FOODTYPE.BIRDFOOD = "BIRDFOOD"local bird_food = {"bird_egg", "bird_egg_cooked", "fish", "fish_cooked", "cutgrass"}local function AddBirdFood(inst)    inst:AddTag("edible_"..GLOBAL.FOODTYPE.BIRDFOOD)endfor k,v in pairs(bird_food) do    AddPrefabPostInit(v, AddBirdFood)end

To give it custom stats for your character  (this should be in a postinit on cutgrass):

inst.components.edible.oneaten = function(inst, eater)    if eater.prefab == "wark" then        eater.components.health:DoDelta(health_amount)        eater.components.hunger:DoDelta(hunger_amount) --note that this is in addition to whatever it has already        eater.components.sanity:DoDelta(sanity_amount)    endend

To set your character's food preferences to include the new type:

    inst.components.eater.foodprefs = { FOODTYPE.VEGGIE, FOODTYPE.INSECT, FOODTYPE.SEEDS, FOODTYPE.GENERIC, FOODTYPE.BIRDFOOD }
Link to comment
Share on other sites

@rezecib

 

Thank you kindly, seems to be working great now.

  This is kind of off-topic but it's for the same character so there's no point in me making a new topic;

Is there anyway for me to have it so that after a certain amout of time moving (say for example after moving for 5 seconds continuously) that the character's walkspeed would increase, but he loses this increase when he stops moving and has to "rev it up" so to speak when he starts moving again?

 If that's as complicated as it sounds then don't worry about it, the grass was already a big help.

Link to comment
Share on other sites

Who specifically can eat "wood" other than Woodie?
Just Woodie, I think xD. They had to write so much to make Woodie work, even in single-player o_o

 

@InvinciBULL, I believe a component like this will do what you describe

local Accelerator = Class(function(self, inst)    self.inst = inst	-- self.basewalkspeed = inst.components.locomotor.walkspeed --by default, this is 4, but do players ever walk? I'm not sure	self.base_runspeed = inst.components.locomotor.runspeed --by default, this is 6	self.bonus_runspeed = 0	self.acceleration = 0.5	self.max_bonus_speed = 3	self.inst:StartUpdatingComponent(self)end,nil,{})function Accelerator:OnUpdate(dt)	local speed_delta = dt*self.acceleration	if not self.inst.components.locomotor.isrunning then		-- the line below would cause his bonus speed to drain while standing still		speed_delta = -speed_delta		-- if instead you want the speed to disappear all at once, use the line below:		-- speed_delta = -self.bonus_runspeed	end	self.bonus_runspeed = math.max(0, math.min(self.max_bonus_speed, self.bonus_runspeed + speed_delta)) -- this clamps the bonus speed between 0 and max	self.inst.components.locomotor.runspeed = self.base_runspeed + self.bonus_runspeedendreturn Accelerator 

However, I expect this will run into rather intractable issues with client action prediction... (the clients will think they still have normal speed, so they will rubberband) I'm not sure exactly how to go about fixing that.

Link to comment
Share on other sites

@rezecib

 

Hey thanks again for the help, but I can't seem to get it to have any effect in game, though I have a feeling this is my fault for putting the script in the wrong place. I changed the code to "speed_delta = -self.bonus_runspeed" so that the speed is instantly taken away when the player stops moving, and then first placed the code in modmain.lua, then invincibull.lua, and finally tried making a new .lua called "accelerator.lua" which I stuck under the local prefabs in invincibull.lua, but none of the three seemed to work.

 Was there a value I had to fiddle with, or am I just putting it in the wrong part of the file?

Link to comment
Share on other sites

@InvinciBULL, That should be its own file, accelerator.lua, stored in scripts/components. You should be adding the component to your character in the prefab file. I didn't test it myself-- it's possible that player movement doesn't actually populate isrunning, so if that's the case you might have to detect it another way (such as comparing the last position and current position, perhaps). 

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