Jump to content

Recommended Posts

I spent last 12+ hours trying to make a mod for changing treeguard chance depending on amount of cut trees and i don't find anything that might be wrong, but it still refuses to work. Forgive me if error/s is/are super obvious, i'm making this mod only because im that much annoyed of rng

 


TreePity = 0
print(TreePity)--prints succesfully
--Reset treeguard chance increase when world loads
-----------------------------
AddPrefabPostInit("evergreens", function(inst)
        local function chop_down_tree(inst, chopper)
            inst.SoundEmitter:PlaySound("dontstarve/forest/treefall")
            local pt = inst:GetPosition()
        
            local he_right = true
            if chopper then
                local hispos = chopper:GetPosition()
                he_right = (hispos - pt):Dot(TheCamera:GetRightVec()) > 0
            else
                if math.random() > 0.5 then
                    he_right = false
                end
            end
        
            if he_right then
                inst.AnimState:PlayAnimation(inst.anims.fallleft)
                inst.components.lootdropper:DropLoot(pt - TheCamera:GetRightVec())
            else
                inst.AnimState:PlayAnimation(inst.anims.fallright)
                inst.components.lootdropper:DropLoot(pt + TheCamera:GetRightVec())
            end
        
            if inst.build ~= "twiggy" then
                inst:DoTaskInTime(GetBuild(inst).chop_camshake_delay, chop_down_tree_shake)
            elseif inst.components.growable == nil or inst.components.growable.stage > 1 then
                inst:DoTaskInTime(GetBuild(inst).chop_camshake_delay, chop_down_twiggy_shake)
            end
        
            make_stump(inst)
            inst.AnimState:PushAnimation(inst.anims.stump)
        
            if GetBuild(inst).leif ~= nil then
                local days_survived = chopper.components.age ~= nil and chopper.components.age:GetAgeInDays() or TheWorld.state.cycles
                if days_survived >= TUNING.LEIF_MIN_DAY then
                    chance = TUNING.LEIF_PERCENT_CHANCE + (TreePity * 0.00333) --Increases chance of treeguard depending on amount of trees without treeguard
                    if chopper:HasTag("beaver") then
                        chance = chance * TUNING.BEAVER_LEIF_CHANCE_MOD
                    elseif chopper:HasTag("woodcutter") then
                        chance = chance * TUNING.WOODCUTTER_LEIF_CHANCE_MOD
                    end
                    if math.random() <= chance then
                        for k = 1, (days_survived <= 30 and 1) or math.random(days_survived <= 80 and 2 or 3) do
                            local target = FindEntity(inst, TUNING.LEIF_MAXSPAWNDIST, find_leif_spawn_target, LEIFTARGET_MUST_TAGS, LEIFTARGET_CANT_TAGS)
                            if target ~= nil then
                                target.noleif = true
                                target.leifscale = GetGrowthStages(target)[target.components.growable.stage].leifscale or 1
                                target.chopper = chopper
                                target:DoTaskInTime(1 + math.random() * 3, spawn_leif)
                                TreePity = 0 --Reset chance increase since treeguard succesfully spawned
                            end
                        end
                    else
                        TreePity = TreePity + 1 --Increase treeguard chance increase
                        TUNING.LEIF_PERCENT_CHANCE = 1
                    end
                end
            end
        end
    end)
print("finish..?")--prints succesfully

only 2 of these prints happen, first and last

modmain.lua

I'm not sure if you are just defining a function?
Hooks don't work that way.

For example, let's say inst.onchoppeddown=some_fn

then you can decorate it as either inst.onchoppeddown=new_fn or local old_fn=inst.on......... inst.on.......=function(inst)new_fn(inst) return old_fn(inst) end

As for those local functions, as far as I know, no way to change them! Because your mod works in a different environment from each and every other lua file( at least mod files).

You need to add

inst.components.workable:SetOnFinishCallback(chop_down_tree)

inside the postinit to have the game call your function instead of the old one when a tree is chopped down. (You can actually define your entire "chop_down_tree" function before the postinit instead of inside of it, and the above line will attach it properly. One less level of indent to worry about.)

Then, you're going to have to provide a copy of "makestump", "LEIFTARGET_MUST_TAGS", etc., (since you can't access the local stuff in "evergreens.lua"), provide a copy of anything those rely on, and finally make sure you're using GLOBAL where required (e.g., GLOBAL.TheCamera.)

Edited by Bumber64

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...