dnjswns951 Posted December 21, 2014 Share Posted December 21, 2014 (edited) I want to change that when I pick up a flower, sanity drops by 5. And I noticed that flower.lua has a function local function onpickedfn(inst, picker) if picker and picker.components.sanity then picker.components.sanity:DoDelta(TUNING.SANITY_TINY)end inst:Remove()end So I wrote same function and did local function onpickedfn(inst, picker) if picker and picker.components.sanity then picker.components.sanity:DoDelta(-TUNING.SANITY_TINY)end inst:Remove()end AddPrefabPostInit("flower", onpickedfn) in modmain.lua but it doesn't work right. Can anyone help me with this using AddPrefabPostInit() ? Edited December 21, 2014 by dnjswns951 Link to comment https://forums.kleientertainment.com/forums/topic/47179-working-with-addprefabpostinit-function-can-anyone-help/ Share on other sites More sharing options...
Blueberrys Posted December 21, 2014 Share Posted December 21, 2014 From what I've gathered through experimenting, the post prefab command will actually run the function you pass, after the prefab is initialized. Your code doesn't actually modify the original "onpickedfn" function of the prefab (flower) simply because of the name. Assuming the picker variable is null (It obviously should be on initialization), it doesn't do anything at all when it's run. But if, hypothetically, picker was assigned a value, it would decrease sanity of the picker (character who did the picking) by 5 only once when the flower is initialized. To modify the original "onpickedfn" for flowers, you'd have to override each instance. There may be a better way to achieve it, but this method does work correctly.So something like this should do what you need: -- This is the function that will actually run every time a flower is picked.local function newOnPickedFn = function(inst, picker) if picker and picker.components.sanity then picker.components.sanity:DoDelta(-TUNING.SANITY_TINY) end inst:Remove()end -- This function will run only once after the flower prefab is initializedlocal function flowerPostPrefab(inst) -- This will assign the new function in place of the old one inst.components.pickable.onpickedfn = newOnPickedFnend -- And finally, this puts our one-time function at the end of the initialization of the flower prefab, so it gets a chance to run when the game loadsAddPrefabPostInit("flower", flowerPostPrefab) Note, I have not tested this exact code, but theoretically it should be functional. Link to comment https://forums.kleientertainment.com/forums/topic/47179-working-with-addprefabpostinit-function-can-anyone-help/#findComment-590526 Share on other sites More sharing options...
Blueberrys Posted December 21, 2014 Share Posted December 21, 2014 Whoops. There's a slight mistake in that code. I actually copied part of my code from elsewhere and forgot to edit this bit."local function newOnPickedFn = function(inst, picker)" doesn't make sense.It should be "local function newOnPickedFn(inst, picker)" Here's the full fixed code: -- This is the function that will actually run every time a flower is picked.local function newOnPickedFn(inst, picker) if picker and picker.components.sanity then picker.components.sanity:DoDelta(-TUNING.SANITY_TINY) end inst:Remove()end -- This function will run only once after the flower prefab is initializedlocal function flowerPostPrefab(inst) -- This will assign the new function in place of the old one inst.components.pickable.onpickedfn = newOnPickedFnend -- And finally, this puts our one-time function at the end of the initialization of the flower prefab, so it gets a chance to run when the game loadsAddPrefabPostInit("flower", flowerPostPrefab) For future reference, you can find out where the function is located by reading through the code. Usually searching for the name of the old function will give you an idea of how it's implemented. Alternately, you could use this great mod to look through many, many useful variables.N Tools, by Nycidian Link to comment https://forums.kleientertainment.com/forums/topic/47179-working-with-addprefabpostinit-function-can-anyone-help/#findComment-590531 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