Jump to content

Recommended Posts

Good afternoon,

 

I've been looking of the forums a way to change a character's sanity depending of what he eats. Actually, I do wish to make this character's sanity increase when eating meat and decrease when eating vegetables. However, despite what I have found so far, it does not work.

 

This is what I added right after the 'local fn = function(inst)' part :

local function oneat(inst, data)      local food=data.food     if food and food.components.edible and food.components.edible.foodtype == FOODTYPE.MEAT then          inst.components.sanity:DoDelta(5)     end     if food and food.components.edible and food.components.edible.foodtype ~= FOODTYPE.MEAT then          inst.components.sanity:DoDelta(-5)     endend

Can someone help me with this little thing, please ?

I did not, this is true. I'm getting started with these things, so I do have lots of things to learn ! Thanks for your help, I'm starting to get this little thing.

 

However, I am not sure about where to parse this line. Is it meant to be put in the 'local fn' instance ?

I did not, this is true. I'm getting started with these things, so I do have lots of things to learn ! Thanks for your help, I'm starting to get this little thing.

 

However, I am not sure about where to parse this line. Is it meant to be put in the 'local fn' instance ?

 

Yes in that function, well spotted! :wilson_goodjob:

Seriously, most beginners aren't that talented/quick learners/lucky/eager/whatever you are.

 

I am an extraterrestrial who made some studies in information technology %)

 

I changed the "foodtype ~= FOODTYPE.MEAT" by "foodtype == FOODTYPE.VEGGIE", in the meanwhile. This database is amazing, highlight it somewhere on the forum !

 

However, despite this, sanity is not decreasing when eating a berry. So I suppose there is something going wrong with the 'oneat' function since it is called correctly. And I want my character to go crazy when eating healthy vegetables >A<

 

Since I copied/pasted the code above after understanding it, I don't really understand where it could go wrong at this point. Do you have an idea ?

Is it custom character? Yes? Then:

in function of creating of character, post 

inst.components.eater.oneatfn = atesomething

this is gonna be condition if character ate something and send to function "atesomething"

local function atesomething(inst, data)      local food=data.food     if food and food.components.edible and food.components.edible.foodtype == FOODTYPE.MEAT then          inst.components.sanity:DoDelta(5)     end      if food and food.components.edible and food.components.edible.foodtype ~= FOODTYPE.MEAT then          inst.components.sanity:DoDelta(-5)     endend

this function must be out of character function. Or you can make even easier, but i don't use such variant (not sure if it will work):

inst.components.eater.oneatfn = function(inst, data) --it must be in character function     local food=data.food     if food and food.components.edible and food.components.edible.foodtype == FOODTYPE.MEAT then          inst.components.sanity:DoDelta(5)     end      if food and food.components.edible and food.components.edible.foodtype ~= FOODTYPE.MEAT then          inst.components.sanity:DoDelta(-5)     endend

It is actually what I wrote, word by word, in my script. I also tried both and, strangely, none worked.

 

If someone comes there with the wish to know how this function works, however, your post is easier to understand since it makes things clear. Thanks for you wish to help, but I'm afraid it does not help me this much. None of these scripts are making sanity decrease by eating berries (so veggies), and my current issue is : why ?

 

I will test the meat part, however, to see if it does work there... or not.

I honestly don't get what could be wrong. It is doing the same thing, over and over. I didn't add anything else than what you can see way above.

 

After some time, here is what I went with : 

local prefabs = {}local function OnEat(inst, food) --these are special food-effects    if food.components.edible       and food.components.edible.foodtype == "MEAT" then       inst.components.sanity:DoDelta(TUNING.SANITY_TINY)    end        if food.components.edible       and food.components.edible.foodtype == "VEGGIE" then       inst.components.sanity:DoDelta(-TUNING.SANITY_TINY)    endendlocal fn = function(inst) -- stuff

What do you think about this ? Is there something to change ?

function OnEat seems good. did you add string?

local fn = function(inst) -- stuffinst.components.eater.oneatfn = OnEat --this one

EDIT: in my custom food i have similar thing:

local fn = function(inst)--blahblahinst.components.edible:SetOnEatenFn(item_oneaten)--blahblahend
local function item_oneaten(inst, eater)    eater.components.locomotor.walkspeed = eater.components.locomotor.walkspeed * SPEED    eater.components.locomotor.runspeed = eater.components.locomotor.runspeed * SPEEDend 

and it works for me

Edited by Amalleus

I did, but it was compiled as a comment. My bad x) so the problem is solved ! I'll write down how to make it if other users want to do the same thing.

 

===============================================================

 

In order to make a character's sanity change depending on the food eaten, here is how to do it :

 

Add "inst.components.eater.oneatfn = OnEat" in the 'local fn' function.

 

Then, add a new function above the main one :

local function OnEat(inst, food) --these are special food-effects    if food.components.edible       and food.components.edible.foodtype == "MEAT" then       inst.components.sanity:DoDelta(TUNING.SANITY_TINY) -- for sanity gain    end         if food.components.edible       and food.components.edible.foodtype == "VEGGIE" then       inst.components.sanity:DoDelta(-TUNING.SANITY_TINY) -- for sanity loss    endend

Thanks for your help !

I have no clue why but adding the string 

inst.components.eater.oneatfn = OnEat

Crashed the game on loading.

I have been trying to make a simple item spawn from eating something and it's just not cooperating.

 

local function OnEat(inst, food) --poop fun stuff
       
      if food.prefab == "nitre"
       then    
        SpawnPrefab("gunpowder").Transform:SetPosition(inst:GetPosition():Get())
         
      end
     end

 

Thats just an example and to me it seems like it should work???

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