AlaranGaerlan Posted June 22, 2015 Share Posted June 22, 2015 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) endendCan someone help me with this little thing, please ? Link to comment Share on other sites More sharing options...
Mobbstar Posted June 22, 2015 Share Posted June 22, 2015 Did you put the following line somewhere? (you need to do that so the game knows what to do with the code) inst.components.eater.oneatfn = oneat source Link to comment Share on other sites More sharing options...
AlaranGaerlan Posted June 22, 2015 Author Share Posted June 22, 2015 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 ? Link to comment Share on other sites More sharing options...
Mobbstar Posted June 22, 2015 Share Posted June 22, 2015 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! Seriously, most beginners aren't that talented/quick learners/lucky/eager/whatever you are. Link to comment Share on other sites More sharing options...
AlaranGaerlan Posted June 22, 2015 Author Share Posted June 22, 2015 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 ? Link to comment Share on other sites More sharing options...
Amalleus Posted June 22, 2015 Share Posted June 22, 2015 Is it custom character? Yes? Then:in function of creating of character, post inst.components.eater.oneatfn = atesomethingthis 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) endendthis 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 Link to comment Share on other sites More sharing options...
AlaranGaerlan Posted June 22, 2015 Author Share Posted June 22, 2015 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. Link to comment Share on other sites More sharing options...
AlaranGaerlan Posted June 22, 2015 Author Share Posted June 22, 2015 Well, vegetables are doing nothing but meat decreases sanity by ten. I'll look for what is causing that. Link to comment Share on other sites More sharing options...
AlaranGaerlan Posted June 22, 2015 Author Share Posted June 22, 2015 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) -- stuffWhat do you think about this ? Is there something to change ? Link to comment Share on other sites More sharing options...
Amalleus Posted June 22, 2015 Share Posted June 22, 2015 function OnEat seems good. did you add string?local fn = function(inst) -- stuffinst.components.eater.oneatfn = OnEat --this oneEDIT: in my custom food i have similar thing:local fn = function(inst)--blahblahinst.components.edible:SetOnEatenFn(item_oneaten)--blahblahendlocal 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 Link to comment Share on other sites More sharing options...
AlaranGaerlan Posted June 22, 2015 Author Share Posted June 22, 2015 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 endendThanks for your help ! Link to comment Share on other sites More sharing options...
shanharlmal Posted September 24, 2015 Share Posted September 24, 2015 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??? Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.
Please be aware that the content of this thread may be outdated and no longer applicable.