Jump to content

How do I get my character to only eat fruits?


Recommended Posts

Well i don't exactly how to do but you can try this. To your characters .lua

local function oneat(inst, food)
	if food and food.components.edible and food.prefab = not "fruit name" or "other fruid" or "other"... then
		local hungerboost = 0
		local sanityboost = 0
		local healtboost = 0
	end
end

with this you still able to eat other things but they do not effect anything. I'm not sure but you can try this for not able to eat

local function oneat(inst, food)
	if food and food.components.edible and food.prefab = not "fruit name" or "other fruid" or "other"... then return
 end
end

I'm not sure about this but give a try and also add this to master postinit

inst.components.eater:SetOnEatFn(oneat).

Hope that will help

Link to comment
Share on other sites

The thing is that the character eating only meat will do this based on the foodtype of the food, as far as i know. But there isn't a foodtype fruit, there is only the veggie one. So it will require a bit different coding, like having a list of "fruit" food somewhere (probably not a good idea to change the foodtype of the food)

Link to comment
Share on other sites

Like AkaiNight already said: If you just want to make your character do different stuff (like a transformation) based on the food item, then you would be better of to supply an oneatfn.

For this you put the following in your master_postinit:

inst.components.eater:SetOnEatFn(OnEat)

where OnEat is something like the following function:

local function OnEat(inst, food)
	if not food:HasTag("spoiled") then
		if food.prefab == "seeds" then
			inst.components.sanity:DoDelta(2)
		elseif food.prefab == "dragonfruit_seeds" then
			inst.components.sanity:DoDelta(-5)
			inst.components.temperature:DoDelta(20)
		elseif food.prefab == "carrot_seeds" then
			inst.components.sanity:DoDelta(6)
			inst.components.health:DoDelta(1)
		elseif food.prefab == "durian_seeds" then
			inst.components.sanity:DoDelta(-9999)
			inst.components.hunger:DoDelta(9999)
		elseif food.prefab == "eggplant_seeds" then
			inst.components.hunger:DoDelta(10)
		elseif food.prefab == "pomegranate_seeds" then
			inst.components.health:DoDelta(15)
		elseif food.prefab == "pumpkin_seeds" then
			inst.components.sanity:DoDelta(10)
		elseif food.prefab == "watermelon_seeds" then
			inst.components.sanity:DoDelta(-5)
			inst.components.temperature:DoDelta(-5)
		elseif food.prefab == "sweet_potato_seeds" then
			inst.components.sanity:DoDelta(5)
			inst.components.hunger:DoDelta(1)
		elseif food.prefab == "corn_seeds" then
			inst.components.sanity:DoDelta(1)
		end
	end
end

This OnEat function is from my character Avia. She can only eat seeds, but each different seed gives her extra benefits.

You could use this function to do the transformation, deal punishment for eating non fruit items, etc.

This function gets called AFTER you eat the food item.

Also i am not really sure what you mean with "normal diet's coding"?

Edited by BakaSchwarz
Link to comment
Share on other sites

inst.components.eater:SetDiet({ FOODGROUP.OMNI }, nil)

because the SetDiet function is defined as follows:

function Eater:SetDiet(caneat, preferseating)
    self.caneat = caneat
    self.preferseating = preferseating or caneat
end

You can ignore the preferseating argument and just set it to nil, because it will then assume caneat is also what the character prefers to eat.

With this setup your character will be able to eat the "default" stuff (as defined in constants.lua):

FOODGROUP =
{
    OMNI =
    {
        name = "OMNI",
        types =
        {
            FOODTYPE.MEAT,
            FOODTYPE.VEGGIE,
            FOODTYPE.INSECT,
            FOODTYPE.SEEDS,
            FOODTYPE.GENERIC,
            FOODTYPE.GOODIES,
        },
    }
}

 

Link to comment
Share on other sites

I found this code that forbids this cactus character from eating other cacti.

local forbidden = {
	cactus_meat = true,
	cactus_meat_cooked = true,
	cactus_flower = true,
	guacamole = true,
	flowersalad = true,
}

	local _PrefersToEat = inst.components.eater.PrefersToEat
	inst.components.eater.PrefersToEat = function(self, food)
		return _PrefersToEat(self, food) and not forbidden[food.prefab]
	end

Maybe it's possible to turn this table into a "eat only" table?

Link to comment
Share on other sites

22 hours ago, icantevenname said:

I found this code that forbids this cactus character from eating other cacti.


local forbidden = {
	cactus_meat = true,
	cactus_meat_cooked = true,
	cactus_flower = true,
	guacamole = true,
	flowersalad = true,
}

	local _PrefersToEat = inst.components.eater.PrefersToEat
	inst.components.eater.PrefersToEat = function(self, food)
		return _PrefersToEat(self, food) and not forbidden[food.prefab]
	end

Maybe it's possible to turn this table into a "eat only" table?

change forbidden to preferred and put all the prefabs you want to be able to eat. If its truit, i looked in the files and there like only 7 fruits in the whole game, pretty restrictive (watermelon, durian, berries_juicy, berries, pomegranate, dragonfruit, cave_banana).

Then change "return _PrefersToEat(self, food) and not forbidden[food.prefab]" to "return _PrefersToEat(self, food) and  preferred[food.prefab]"

Edited by Parusoid
Link to comment
Share on other sites

2 hours ago, Parusoid said:

change forbidden to preferred and put all the prefabs you want to be able to eat. If its truit, i looked in the files and there like only 7 fruits in the whole game, pretty restrictive (watermelon, durian, berries_juicy, berries, pomegranate, dragonfruit, cave_banana).

Then change "return _PrefersToEat(self, food) and not forbidden[food.prefab]" to "return _PrefersToEat(self, food) and  preferred[food.prefab]"

That works fine, but when the character transforms back, the fruit diet persists.

Link to comment
Share on other sites

7 hours ago, icantevenname said:

That works fine, but when the character transforms back, the fruit diet persists.

Then you need to put that code in the part of code that represents the transformation and put the code that makes the diet back to normal in the part of code that represents the other form

 

Spoiler

local _PrefersToEat = inst.components.eater.PrefersToEat
	inst.components.eater.PrefersToEat = function(self, food)
		return _PrefersToEat(self, food)
	end

 

 

Edited by Parusoid
Link to comment
Share on other sites

15 hours ago, Parusoid said:

Then you need to put that code in the part of code that represents the transformation and put the code that makes the diet back to normal in the part of code that represents the other form

 

  Hide contents


local _PrefersToEat = inst.components.eater.PrefersToEat
	inst.components.eater.PrefersToEat = function(self, food)
		return _PrefersToEat(self, food)
	end

 

 

That doesn't seem to be working. Should I post the LUA?

Link to comment
Share on other sites

I'm not sure if what I did would apply to you but I made an item that changes a characters diet when you equip and reverts it on unequip. The code I used to change it is 

--onequip

owner.eattags = owner.components.eater.caneat
owner.preferseattags = owner.components.eater.preferseating
owner.components.eater:SetDiet({})--stick the new diet in here

and on the unequip i used

 

--onunequip

if owner.eattags then
	owner.components.eater:SetDiet(owner.eattags, owner.preferseattags)
end

 

Link to comment
Share on other sites

1 hour ago, Wolf_EX said:

I'm not sure if what I did would apply to you but I made an item that changes a characters diet when you equip and reverts it on unequip. The code I used to change it is 


--onequip

owner.eattags = owner.components.eater.caneat
owner.preferseattags = owner.components.eater.preferseating
owner.components.eater:SetDiet({})--stick the new diet in here

and on the unequip i used

 


--onunequip

if owner.eattags then
	owner.components.eater:SetDiet(owner.eattags, owner.preferseattags)
end

 

It keeps crashing because "owner" is not declared? I think it's because "owner" is meant for equipable items. But I bet we can turn this into something usable.

Edited by icantevenname
Link to comment
Share on other sites

1 hour ago, icantevenname said:

It keeps crashing because "owner" is not declared? I think it's because "owner" is meant for equipable items. But I bet we can turn this into something usable.

I was just using my code as an example on how it was done, you would probably use "inst" instead.

maybe something like 

local eattags = inst.components.eater.caneat
local preferseattags = inst.components.eater.preferseating
inst.components.eater:SetDiet({"pomegranate", "dragonfruit", "cave_banana"})--im not sure if this is how you set it's diet.

--and to undo
if eattags then
	inst.components.eater:SetDiet(eattags, preferseattags)
end

my gpu is dying atm so I can't really test this.  You probably don't even need to do it this way if your character normally eats regular food.  Could probably just set the diet to FOODTYPE.GENERIC or something.

Edited by Wolf_EX
Link to comment
Share on other sites

16 hours ago, Wolf_EX said:

I was just using my code as an example on how it was done, you would probably use "inst" instead.

maybe something like 


local eattags = inst.components.eater.caneat
local preferseattags = inst.components.eater.preferseating
inst.components.eater:SetDiet({"pomegranate", "dragonfruit", "cave_banana"})--im not sure if this is how you set it's diet.

--and to undo
if eattags then
	inst.components.eater:SetDiet(eattags, preferseattags)
end

my gpu is dying atm so I can't really test this.  You probably don't even need to do it this way if your character normally eats regular food.  Could probably just set the diet to FOODTYPE.GENERIC or something.

It seems to ignore all foods...

Link to comment
Share on other sites

I found an answer here

You have to make a custom food group.

 

In the modmain add 

local FOODTYPE = GLOBAL.FOODTYPE
local FOODGROUP = GLOBAL.FOODGROUP

FOODTYPE.FRUITS = "FRUIT"

FOODGROUP.FRUIT = {}
FOODGROUP.FRUIT.name = "FRUIT"
FOODGROUP.FRUIT.types =
{
	FOODTYPE.FRUITS,
}

local function MakeFruit(inst)
	inst:AddTag("edible_"..FOODTYPE.FRUITS)
end

--Do this for each food that you want in the fruit group
AddPrefabPostInit("dragonfruit", MakeFruit)
AddPrefabPostInit("pomegranate", MakeFruit)

 

and the code to change the caneat is inst.components.eater:SetDiet({FOODGROUP.FRUIT}) and assuming your character has a regular diet when you transform back, just use FOODGROUP.OMNI(or whatever type of diet it has)

Edited by Wolf_EX
Link to comment
Share on other sites

45 minutes ago, Wolf_EX said:

I found an answer here

You have to make a custom food group.

 

In the modmain add 


local FOODTYPE = GLOBAL.FOODTYPE
local FOODGROUP = GLOBAL.FOODGROUP

FOODTYPE.FRUITS = "FRUIT"

FOODGROUP.FRUIT = {}
FOODGROUP.FRUIT.name = "FRUIT"
FOODGROUP.FRUIT.types =
{
	FOODTYPE.FRUITS,
}

local function MakeFruit(inst)
	inst:AddTag("edible_"..FOODTYPE.FRUITS)
end

--Do this for each food that you want in the fruit group
AddPrefabPostInit("dragonfruit", MakeFruit)
AddPrefabPostInit("pomegranate", MakeFruit)

 

and the code to change the caneat is inst.components.eater:SetDiet({FOODGROUP.FRUIT}) and assuming your character has a regular diet when you transform back, just use FOODGROUP.OMNI(or whatever type of diet it has)

FINALLY. Thank you! It's weird that fruit ain't it's own food group from the get-go...

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