Jump to content

Adding Loot to Trees


Recommended Posts

Hello,

Ive been trying to add a chance for a fruit to drop from lumpy evergreen

First attempt:

Spoiler

        local function GuavaLootHigh(prefab)
            inst.components.lootdropper:AddRandomLoot("guavacado",0.75)
        end
        local function GuavaLootLow(prefab)
            inst.components.lootdropper:AddRandomLoot("guavacado",0.25)
        end
        
        AddPrefabPostInit("evergreen_sparse_normal", AddGuavaLootLow)
        AddPrefabPostInit("evergreen_sparse_tall", AddGuavaLootHigh)

which works for mobs, but apparently not for trees.

So I tried this...

Spoiler

local guavapostinit = false
local function sparsetreepostinit(inst)
    if not guavapostinit then
        guavapostinit = true -- So it only runs once
        local growth_stages = inst.components.growable.stages
                    
        local old_fn = growth_stages[3].fn
        growth_stages[3].fn = function(inst)
            old_fn(inst)
            local chanceloot = inst.components.lootdropper.chanceloot or {}
            for i, v in ipairs(chanceloot) do
                if math.random() < 0.15 then
                    v.prefab = "guavacado"

                    break
                end
            end
        end

    
    end
end

AddPrefabPostInit("evergreen_sparse", sparsetreepostinit) 

Which is based on some code provided to me by @DarkXero some time ago... so I was making guesses at how to change it for my needs.

And apparently guessed wrong,

Nothing appears to crash the mod, but the guavacado fruit also never drops when the tree is cut down.

Thanks in advance for the advice

 

 

Edited by MidrealmDM
re-open closed topic
Link to comment
Share on other sites

I did this for my issabelle mod that makes it drop fruits from various trees:

--Tree Test
--Add resources from trees

function EvergreenPostInit(inst)    
if GLOBAL.TheWorld.ismastersim then local oldonfinish = inst.components.workable.onfinish
		inst.components.workable.onfinish = function(inst, chopper)            
		if chopper then 
		inst.components.lootdropper:AddChanceLoot("acapple", 0.1)
		inst.components.lootdropper:AddChanceLoot("accherry", 0.1)
		inst.components.lootdropper:AddChanceLoot("acpear", 0.1)
		inst.components.lootdropper:AddChanceLoot("acpeach", 0.1)
		inst.components.lootdropper:AddChanceLoot("acorange", 0.1)         
	end            
		oldonfinish(inst, chopper)        
			end    
		end
	return inst
end 

local trees = {"evergreen", 
	"evergreen_normal", 
	"evergreen_tall", 
	"evergreen_short", 
	"evergreen_sparse",
	"evergreen_sparse_normal", 
	"evergreen_sparse_tall", 
	"evergreen_sparse_short",
	"deciduoustree",
	"deciduoustree_normal",
	"deciduoustree_tall",
	"deciduoustree_short",
	"twiggytree",
	"twiggy_tall",
	"twiggy_normal",
	"twiggy_short"} for k,v in pairs(trees) do AddPrefabPostInit(v, EvergreenPostInit)
end

--End tree test

just take out all the unwanted trees u dont want and it should work

Link to comment
Share on other sites

@rons0n

I may have spoken too soon -

The code (below) works if I use c_spawn to bring in the prefab, but not for those naturally occurring in the game
 

Spoiler

 

--Guavacado Drop
--Add guavacado to trees (code provided by rons0n)
function GuavaHighPostInit(inst)    
    if GLOBAL.TheWorld.ismastersim then
        local oldonfinish = inst.components.workable.onfinish
            inst.components.workable.onfinish = function(inst, chopper)            
                if chopper then
                    inst.components.lootdropper:AddChanceLoot("guavacado", 0.6)
                end            
            oldonfinish(inst, chopper)        
        end    
      end 
    return inst
end

function GuavaLowPostInit(inst)
    if GLOBAL.TheWorld.ismastersim then
        local oldonfinish = inst.components.workable.onfinish
            inst.components.workable.onfinish = function(inst, chopper)            
                if chopper then
                    inst.components.lootdropper:AddChanceLoot("guavacado", 0.3)
                end            
            oldonfinish(inst, chopper)        
        end    
    end
    return inst
end

local hightrees = {    
    "evergreen_sparse_tall",
    }
    for k,v in pairs(hightrees) do AddPrefabPostInit(v, GuavaHighPostInit)
end

local lowtrees = {    
    "evergreen_sparse_normal"
    }
    for k,v in pairs(lowtrees) do AddPrefabPostInit(v, GuavaLowPostInit)
end

--End Guavacado Drop

 

If I add "evergreen_sparse" it works, but adds the drop to all growth stages.

 

.

Link to comment
Share on other sites

To be honest I don't know why your method doesn't work. I just tried it out and the way I look at it, it should've worked but it didnt. Sorry wish I knew.

You may have a typo somewhere or someone more tech savvy can explain the situation. Cause mine works

 

Link to comment
Share on other sites

ok - this seems to work... thanks @SuperDavid and @rons0n

Have a Happy New Year.
 

Spoiler

 

function GuavaPostInit(inst)    
    
        local oldonfinish = inst.components.workable.onfinish
            inst.components.workable.onfinish = function(inst, chopper)            
                if chopper then
                    if inst.components.growable then -- Will not be added to trees that don't have growth stages
                        if inst.components.growable.stage == 3 then -- adds 5% chance to stage 3 (adult) tree
                          inst.components.lootdropper:AddChanceLoot("guavacado", 0.05)
                        elseif inst.components.growable.stage == 2  then-- adds 3% chance to stage 2 (medium/normal) tree
                          inst.components.lootdropper:AddChanceLoot("guavacado", 0.03)
                        end
                    else end -- does not add anything to stage 1 (young) tree           
                end    
            oldonfinish(inst, chopper)        
        end    
        
    return inst
end

local guavatrees = {    
    "evergreen_sparse",
    }
    for k,v in pairs(guavatrees) do AddPrefabPostInit(v, GuavaPostInit)
end

 

 

Edited by MidrealmDM
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...