Jump to content

Recommended Posts

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 by dnjswns951

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 initialized
local function flowerPostPrefab(inst)
    -- This will assign the new function in place of the old one
    inst.components.pickable.onpickedfn = newOnPickedFn
end
 
-- 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 loads
AddPrefabPostInit("flower", flowerPostPrefab)
 
Note, I have not tested this exact code, but theoretically it should be functional.

 

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 initialized
local function flowerPostPrefab(inst)
    -- This will assign the new function in place of the old one
    inst.components.pickable.onpickedfn = newOnPickedFn
end
 
-- 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 loads
AddPrefabPostInit("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

 

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
  • Create New...