Jump to content

Flower Picking + Sanity


Recommended Posts

I have a character mod I've been working on for a while, and she basically plays on sanity. One of her main perks is that she regains more sanity from picking flowers than others. Right now, the idea is that each flower gives her +10, whereas others receive +5

I have added a character tag "winnie" into her character.lua, and the following code is in her modmain.

Quote

 

AddPrefabPostInit("flower", function(inst)

local function OnPickedfn(inst, picker)
     if picker and picker.components.sanity and picker:HasTag("winnie") then
        picker.components.sanity:DoDelta(10) 
    elseif picker and picker.components.sanity and not picker:HasTag("winnie") then
        picker.components.sanity:DoDelta(5)
    end
    
    if inst.animname == ROSE_NAME and picker.components.combat ~= nil then
        picker.components.combat:GetAttacked(inst, TUNING.ROSE_DAMAGE)
        picker:PushEvent("thorns")
    end

    GLOBAL.TheWorld:PushEvent("beginregrowth", inst)

    inst:Remove()    
end

inst.components.pickable.onpickedfn = OnPickedfn
end)

 

I boot up the game, load the mod, and everything works fine, but only when Winnie is alone. The MOMENT another player joins, it falls apart. The OTHER player's game crashes, and it gives them a memory error: 

Quote

[00:01:32]: error calling PrefabPostInit: flower in mod Winifred Update4 (Winnie the Witch): 
[string "../mods/Winifred Update4/modmain.lua"]:271: attempt to index field 'pickable' (a nil value)

(Taken from a friend's client_log.txt after Winnie crashed their game.)

Obvious it's something about that final line, with "pickable", that the game doesn't like, but I checked and "pickable" is indeed listed as a component in the main script files of DST, so im not sure what the problem is? 

 

Edited by Faerendipitous
Link to comment
Share on other sites

31 minutes ago, thomas4846 said:

if inst.components.pickable ~= nil then
--inst:AddComponent("pickable") maybe not too sure
inst.components.pickable.onpickedfn = OnPickedfn
end


 

Adding this to her modmain removes her ability to pick flowers all together, which I find absolutely hilarious.

Link to comment
Share on other sites

It's bad form to copy-paste functions when all your mod is doing is adding functionality, you kill all other mods' functionality and forces you to update your mod if Klei makes any changes to the copied bit.

This is a prehook and it'll give a flat rate of +5 sanity for flowers.  If you want to say double whatever Klei does, then record the value of sanity before the old function is called and delta the after value to see how much it increases baseline, then double it.

AddPrefabPostInit(
    "flower",
    function(inst)
        if not GLOBAL.TheWorld.ismastersim
        then
            return
        end
        local pickable = inst.components.pickable
        if not pickable
        then
            return
        end
        local onpickedfn_old = pickable.onpickedfn
        pickable.onpickedfn = function(inst, picker, ...)
            local rets = {}
            if picker and picker:HasTag("winnie") -- I prefer actually checking character prefab over tags, tags are limited in how many may exist on one entity: picker.prefab == "winnie"
            then
                if picker.components.sanity
                then
                    picker.components.sanity:DoDelta(5)
                end
            end
            if onpickedfn_old
            then
                rets = {onpickedfn_old(inst, picker, ...)}
            end
            return GLOBAL.unpack(rets)
        end
    end
)

You also left off the `GLOBAL.TheWorld.ismastersim` check which is required when you're touching components.  Components exist on the server, and clients sometimes get replicated versions of them which house minimal information.

  • Like 1
  • Sanity 1
Link to comment
Share on other sites

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
 Share

×
  • Create New...