Fancy_Fox_Pers Posted May 30, 2015 Share Posted May 30, 2015 (edited) Hi, I'm trying to make a character loose sanity when eating any form of meat. This is what I have in the character's lua : -- Decrease sanity when eating meatlocal function oneat(inst, food) if food and food.components.edible and food.components.edible.foodtype == FOODTYPE.MEAT then inst.components.sanity:DoDelta(-15) endendSo why is this not working? I also tried adding a ListenForEvent in the master_postinit but that didn't fix it ^^' Edited May 30, 2015 by Thibooms Link to comment https://forums.kleientertainment.com/forums/topic/54597-help-sanity-penalty-from-eating-meat/ Share on other sites More sharing options...
Seiai Posted May 30, 2015 Share Posted May 30, 2015 (edited) So why is this not working?uhm, i guess because u dont use your function anywhere? if this is all u added, then u really only created a function, without using it anywhere. the eater-component pushes this event: self.inst:PushEvent("oneat", {food = food}) if u use a correct listener, u can use this to call your function, when u eat something ingame. Edited May 30, 2015 by Seiai Link to comment https://forums.kleientertainment.com/forums/topic/54597-help-sanity-penalty-from-eating-meat/#findComment-642284 Share on other sites More sharing options...
Fancy_Fox_Pers Posted May 30, 2015 Author Share Posted May 30, 2015 (edited) @Seiai,Well I now added : inst:ListenForEvent("oneat", oneat) In the master_postinit but I get this crash when eating (meat and veggies) : 69: attempt to index field 'components' (a nil value)The line that crashes is this one : if food and food.components.edible and food.components.edible.foodtype == FOODTYPE.MEAT then Or maybe the one after it of course Edited May 30, 2015 by Thibooms Link to comment https://forums.kleientertainment.com/forums/topic/54597-help-sanity-penalty-from-eating-meat/#findComment-642287 Share on other sites More sharing options...
Isosurface Posted May 30, 2015 Share Posted May 30, 2015 Wouldn't setting Eater:SetOnEatFn( fn ) be easier? Link to comment https://forums.kleientertainment.com/forums/topic/54597-help-sanity-penalty-from-eating-meat/#findComment-642299 Share on other sites More sharing options...
Fancy_Fox_Pers Posted May 30, 2015 Author Share Posted May 30, 2015 @Isosurface, Meaning I have to list all the meats? Wouldn't that be much worse? Link to comment https://forums.kleientertainment.com/forums/topic/54597-help-sanity-penalty-from-eating-meat/#findComment-642301 Share on other sites More sharing options...
Isosurface Posted May 30, 2015 Share Posted May 30, 2015 (edited) Why would you list the meat? C&P this to you modmain and try it yourself: local function oneat(inst, food) if food and food.components.edible and food.components.edible.foodtype == GLOBAL.FOODTYPE.MEAT then inst.components.sanity:DoDelta(-15) endendAddPlayerPostInit(function(inst) if inst.components.eater then inst.components.eater:SetOnEatFn( oneat ) endend) In the actual mod, make a copy of the old oneatfn and append that to you new oneatfn. Edited May 30, 2015 by Isosurface Link to comment https://forums.kleientertainment.com/forums/topic/54597-help-sanity-penalty-from-eating-meat/#findComment-642305 Share on other sites More sharing options...
Fancy_Fox_Pers Posted May 30, 2015 Author Share Posted May 30, 2015 @Isosurface, Oh, I didn't get what you meant at first. But this will make the penalty for all characters, I want only mine to be affected. I just don't get why I get that crash when eating. Why does it have a problem with the components? It seems like a simple function so I'd like to just use that, or at least know what's not working. I'm trying to learn from this sort of stuff... ^^ Link to comment https://forums.kleientertainment.com/forums/topic/54597-help-sanity-penalty-from-eating-meat/#findComment-642311 Share on other sites More sharing options...
Seiai Posted May 30, 2015 Share Posted May 30, 2015 (edited) But this will make the penalty for all characters, I want only mine to be affected.well, then add a check for if inst is your character.inst.prefab=="yourcharactersprefabname"also, the eventlistener calls the function u provide with the the parameters inst and the parameter from the pushevent-function(as u can see from all the other eventlistener and pushevents in the gamecode). the parameter in the pushevent-function here is {food = food}, which is a table, containing a variable food, which references the fooditem u eat.which means that u have to select the item out of that table, either by changing the Listener:inst:ListenForEvent("oneat", function(inst,data) oneat(inst,data.food) end) OR by changing the oneatfunctionlocal 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(-15) endend Edited May 30, 2015 by Seiai Link to comment https://forums.kleientertainment.com/forums/topic/54597-help-sanity-penalty-from-eating-meat/#findComment-642322 Share on other sites More sharing options...
Seiai Posted May 30, 2015 Share Posted May 30, 2015 Why does it have a problem with the components?this happens because u get a table instead of the item, like i explained in the last post, and that table has no "components" obviously^^ Link to comment https://forums.kleientertainment.com/forums/topic/54597-help-sanity-penalty-from-eating-meat/#findComment-642324 Share on other sites More sharing options...
Isosurface Posted May 30, 2015 Share Posted May 30, 2015 Thibooms, you are missing the point, the point is that you should add inst.components.eater:SetOnEatFn( oneat ) to your character file, not modmain. My code is just to demonstrate what oneatfn does exactly what you want. Link to comment https://forums.kleientertainment.com/forums/topic/54597-help-sanity-penalty-from-eating-meat/#findComment-642339 Share on other sites More sharing options...
Fancy_Fox_Pers Posted May 30, 2015 Author Share Posted May 30, 2015 @Seiai,Alright, now I get this x) @Isosurface, Sorry, man, but I'm just a real newbie when it comes to coding so I was lacking a lot of information on that oneThanks to the both of you, it's working now Link to comment https://forums.kleientertainment.com/forums/topic/54597-help-sanity-penalty-from-eating-meat/#findComment-642387 Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now