YordlePrincess Posted July 18, 2015 Share Posted July 18, 2015 I made a custom food and gave it stats, but I want to change the things it does if a certain character eats it, so I wrote this lines...but of course the game ignores it completly..can someone explain me why? :/ Thanks a lot AddPrefabPostInit("custom_food", function(inst) local _oneatenfn = inst.components.edible.oneatenfn inst.components.edible.oneatenfn = function( inst, eater) if eater and eater.components.health and eater.prefab == "custom_character" then eater.components.health:DoDelta(TUNING.HEALING_MEDLARGE) _oneatenfn( inst, eater ) else _oneatenfn( inst, eater ) end endend) Link to comment https://forums.kleientertainment.com/forums/topic/56274-game-ignores-custom-food-script/ Share on other sites More sharing options...
Maris Posted July 18, 2015 Share Posted July 18, 2015 AddPrefabPostInit("custom_food", function(inst) print("Okay, custom food exists.") local _oneatenfn = inst.components.edible.oneatenfn if not _oneatenfn then print("Btw old oneaten is nil!!! Your bad.") end inst.components.edible.oneatenfn = function( inst, eater) print("Custom OnEaten works! eater = "..tostring(eater)..", food = "..tostring(inst)) if eater and eater.components.health and eater.prefab == "custom_character" then print("Everything should be okay!") eater.components.health:DoDelta(TUNING.HEALING_MEDLARGE) print("Executing old oneat fn...") _oneatenfn( inst, eater ) else print("Something goes wrong... eater.components.health = "..tostring(eater.components.health)..", eater.prefab = "..tostring(eater.prefab)) _oneatenfn( inst, eater ) end endend)Then check your log.txt or even ingame log via Ctrl+L Link to comment https://forums.kleientertainment.com/forums/topic/56274-game-ignores-custom-food-script/#findComment-655446 Share on other sites More sharing options...
YordlePrincess Posted July 18, 2015 Author Share Posted July 18, 2015 (edited) You're right it stucks on the oneaten is nill part :/ not sure what can I do against it...I've tried some alternatives without any progress AddPrefabPostInit("custom_food", function(inst) print("Okay, custom food exists.") local _oneatenfn = inst.components.edible.oneatenfn if not _oneatenfn then print("Btw old oneaten is nil!!! Your bad.") end inst.components.edible.oneatenfn = function( inst, eater) print("Custom OnEaten works! eater = "..tostring(eater)..", food = "..tostring(inst)) if eater and eater.components.health and eater.prefab == "custom_character" then print("Everything should be okay!") eater.components.health:DoDelta(TUNING.HEALING_MEDLARGE) print("Executing old oneat fn...") _oneatenfn( inst, eater ) else print("Something goes wrong... eater.components.health = "..tostring(eater.components.health)..", eater.prefab = "..tostring(eater.prefab)) _oneatenfn( inst, eater ) end endend)Then check your log.txt or even ingame log via Ctrl+L Edited July 18, 2015 by YordlePrincess Link to comment https://forums.kleientertainment.com/forums/topic/56274-game-ignores-custom-food-script/#findComment-655518 Share on other sites More sharing options...
Ryuushu Posted July 18, 2015 Share Posted July 18, 2015 @YordlePrincessIt's a custom food, right? That means it doesn't has a oneatenfn yet, that's why it's nil.Just don't do anything if the eater wasn't your character. Also, if you're making a custom edible, don't use AddPrefabPostInit, instead put the code inside your food's prefab, like so:local function fn()... inst.components.edible.oneatenfn = function( inst, eater) if eater and eater.components.health and eater.prefab == "custom_character" then eater.components.health:DoDelta(TUNING.HEALING_MEDLARGE) end endendIf you are modifying an existing food, just add a check asking if the function isn't nil before calling it. Link to comment https://forums.kleientertainment.com/forums/topic/56274-game-ignores-custom-food-script/#findComment-655545 Share on other sites More sharing options...
YordlePrincess Posted July 19, 2015 Author Share Posted July 19, 2015 I tried your method but it still ignores it I've tried to check but it doesn't print anything where does it stuck @YordlePrincessIt's a custom food, right? That means it doesn't has a oneatenfn yet, that's why it's nil.Just don't do anything if the eater wasn't your character. Also, if you're making a custom edible, don't use AddPrefabPostInit, instead put the code inside your food's prefab, like so:local function fn()... inst.components.edible.oneatenfn = function( inst, eater) if eater and eater.components.health and eater.prefab == "custom_character" then eater.components.health:DoDelta(TUNING.HEALING_MEDLARGE) end endendIf you are modifying an existing food, just add a check asking if the function isn't nil before calling it. Link to comment https://forums.kleientertainment.com/forums/topic/56274-game-ignores-custom-food-script/#findComment-655642 Share on other sites More sharing options...
Ryuushu Posted July 19, 2015 Share Posted July 19, 2015 @YordlePrincess Hmm, could you upload the file where you are trying to modify the food? (modmain.lua or mycustomfood.lua). I think it'd be easier to take a look at the code as a whole. Link to comment https://forums.kleientertainment.com/forums/topic/56274-game-ignores-custom-food-script/#findComment-655755 Share on other sites More sharing options...
DarkXero Posted July 19, 2015 Share Posted July 19, 2015 It'sinst.components.edible.oneatenNo fn there. Link to comment https://forums.kleientertainment.com/forums/topic/56274-game-ignores-custom-food-script/#findComment-655782 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