Nelvul Posted December 10, 2018 Share Posted December 10, 2018 Hello everyone!! Could someone please help me with this coding problem that I have? I want to code so that I get no sanity loss from eating raw food, but I just don't know how to do it. I've tried different methods but none seem to work. I would greatly appreciate if someone could help me with this problem! Link to comment Share on other sites More sharing options...
Ultroman Posted December 10, 2018 Share Posted December 10, 2018 (edited) This is the code from the eater.lua component you're interested in, lines 185-192 self.inst:PushEvent("oneat", { food = food, feeder = feeder }) if self.oneatfn ~= nil then self.oneatfn(self.inst, food) end if food.components.edible ~= nil then food.components.edible:OnEaten(self.inst) end There is a bit of a problem. You can't just give yourself the inverse sanity loss when the oneat event is pushed OR make an oneatfn that does this, since the food's effects aren't applied until afterwards, in OnEaten. So, if you're at close to max sanity, you would add only your remaining sanity, and the food might steal more sanity from you than you just gave yourself. And since the food might be stackable, you can't just change the food, since it will then be permanently changed for everyone who eats it. I hope someone else has a better solution, but right now, one solution I can see, is to listen for the event "oneat". When that triggers, check if the food is edible and has a negative sanity loss, and if it does, you delay a call to a function that gives you the inverse of the sanity loss. Then it should call that function on the next frame, after the food has been eaten, and you actually received the sanity loss already, but the function we wrote will give it back to you in the next frame. Put this in your character's initialization function. inst:ListenForEvent("oneat", function(data) local food = data.food if food and food.components.edible ~= nil and food.components.edible.sanityvalue < 0 then inst:DoTaskInTime(0.01, function(food) -- Give yourself the reverse of the sanity loss applied by the food. self.inst.components.sanity.DoDelta(-food.edible.sanityvalue) end) end end) Edited December 10, 2018 by Ultroman Link to comment Share on other sites More sharing options...
ShiniHa Posted December 10, 2018 Share Posted December 10, 2018 1 hour ago, Ultroman said: inst:ListenForEvent("oneat", function(data) local food = data.food if food and food.components.edible ~= nil and food.components.edible.sanityvalue < 0 then inst:DoTaskInTime(0.01, function(food) -- Give yourself the reverse of the sanity loss applied by the food. self.inst.components.sanity.DoDelta(-food.edible.sanityvalue) end) end end) I'm actually looking for something similar, but with specific food items giving different bonuses, such as monster meat. So raw meat gives me no penalty but monster meat gives extra sanity, would something like that be possible? Link to comment Share on other sites More sharing options...
Ultroman Posted December 11, 2018 Share Posted December 11, 2018 Sure. Just check if food.prefab == "monstermeat" and do something different. Link to comment Share on other sites More sharing options...
ShiniHa Posted December 11, 2018 Share Posted December 11, 2018 1 hour ago, Ultroman said: Sure. Just check if food.prefab == "monstermeat" and do something different. Ah thank you! Hope it wouldn't be too much to ask where to put to code as if to not mess anything up, my coding skill is terrible and I don't know where to put what hah. Link to comment Share on other sites More sharing options...
Ultroman Posted December 11, 2018 Share Posted December 11, 2018 I'm not sure it's a good idea to be making character mods, if you don't know how if-statements work... But anyway: inst:ListenForEvent("oneat", function(data) local food = data.food if food and food.components.edible ~= nil and food.components.edible.sanityvalue < 0 then inst:DoTaskInTime(0.01, function(food) -- Give yourself the reverse of the sanity loss applied by the food which has negative sanity effect. self.inst.components.sanity.DoDelta(-food.edible.sanityvalue) -- Now you can do whichever specific sanity loss or gain you want, since the original negative food effect has already been negated/removed. if food.prefab == "monstermeat" then self.inst.components.sanity.DoDelta(someValue) elseif food.prefab == "someOtherPrefabName" then self.inst.components.sanity.DoDelta(someOtherValue) end end) end end) Link to comment Share on other sites More sharing options...
ShiniHa Posted December 11, 2018 Share Posted December 11, 2018 (edited) 2 hours ago, Ultroman said: I'm not sure it's a good idea to be making character mods, if you don't know how if-statements work... 1 1 It probably isn't but its fun and that's honestly what matters to me, but thank you so much!! And its less of a how if statements work and more of a, wherein what lua i should put stuff in hwsjhadsn Edited December 11, 2018 by Bubbbels Link to comment Share on other sites More sharing options...
Ultroman Posted December 11, 2018 Share Posted December 11, 2018 (edited) The whole ListenForEvent thing needs to be at the bottom of your character prefab's init-function. Yeah, it is fun And you can do a lot with the character template and guide alone, but the fun really begins when you can make literally anything happen, which is what you can do when you know how to code, and know the game's framework. My problem is, that I'm not very creative. If you're a creative person, think what you could do, in a setting where you can do anything Edited December 11, 2018 by Ultroman Link to comment Share on other sites More sharing options...
ShiniHa Posted December 12, 2018 Share Posted December 12, 2018 Nice, then I remembered that listen for events should be down there. I will try seeing if everything works later. And yeah, sadly it takes me so long to learn languages, I quickly pick up and understand them so I can put stuff here and there but I cannot ever learn to actually write for hand. But even if you might not be a creative person what matters is that you are sweet and help others, I really hope to someday be as helpful as you are on these forums. Some day I might learn how to properly code hah, then I will be able to as much as the game lets me. Link to comment Share on other sites More sharing options...
Ultroman Posted December 12, 2018 Share Posted December 12, 2018 Thanks, you're very kind The LUA Crash Course is a great start Link to comment 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