Jump to content

Assistance Please: Change Loot Drops on Trees


Recommended Posts

I am trying to overwrite the loot drop function of tall jungle trees.

I though I could use

AddPrefabPostInit("jungletrees", function(inst)

to replace the local function "SetTall(inst)" in the jungletrees.lua

the do this, but apparently I am doing something wrong, because it appears to have no effect on the game.

It doesn't crash anything - but it doesn't change what loot the trees drop either.

 

Thank you, in advance.

 

Below is the code from my modmain.lua

I am simply trying to get the jungle trees to drop flint and rocks right now, once I get that working I will change the items*

Spoiler

AddPrefabPostInit("jungletrees", function(inst)
  local function SetTall(inst)
    inst.anims = tall_anims
    if inst.components.workable then
        inst.components.workable:SetWorkLeft(TUNING.JUNGLETREE_CHOPS_TALL)
    end
    -- inst:AddTag("shelter")
    inst.components.lootdropper:SetLoot(GetBuild(inst).tall_loot)

    if math.random() < 0.5 then
        for i = 1, TUNING.SNAKE_JUNGLETREE_AMOUNT_TALL do
            if math.random() < 0.5 and GetClock():GetNumCycles() >= TUNING.SNAKE_POISON_START_DAY then
                inst.components.lootdropper:AddChanceLoot("snake_poison", TUNING.SNAKE_JUNGLETREE_POISON_CHANCE)
            else
                inst.components.lootdropper:AddChanceLoot("snake", TUNING.SNAKE_JUNGLETREE_CHANCE)
            end
        end
    else
        if math.random() < 0.5 then
            inst.components.lootdropper:AddChanceLoot("flint", 1.0) -- this used to be bird_egg
        else
            inst.components.lootdropper:AddChanceLoot("rocks", 1.0) -- this used to be cave_banana 
        end
    end

    Sway(inst)
end
end)

 

* The items the trees normally drop are listed as local prefabs in the jungletrees.lua

If I want the trees to drop new items (from the mod) do they need to be listed as local prefabs as well?

And if so, how would I go about adding local prefabs to the existing jungletrees.lua?

 

 

 

Link to comment
Share on other sites

"jungletrees" is the prefab file. The prefabs that it returns are listed at the end.

AddPrefabPostInit works on prefab names, not files. Prefab names would be "jungletree", "jungletree_tall", ...

 

The postinit function runs on the entity that is generated.

So, what you did is declare a local variable on your function, and do nothing with the entity (passed via inst).

Nothing happens, you just declare a local variable.

Nothing would happen if you changed "jungletrees" for a valid prefab either.

 

Local variables are basically in a black box.

Something you can do here is copy-paste jungletrees.lua into a prefabs folder inside your mod.

That way the game will load that file, instead of the vanilla one. Then you can change the file to your liking.

Link to comment
Share on other sites

@DarkXero

I'm not sure I followed all of that, but my takeaway is that what I am trying to do won't work.

(my knowledge of Lua is nonexistant - I basically piece together things as best I can by looking at

the existing game files and reading the forums for people with similar issues. And then I just hope and

pray that they work.)

 

3 hours ago, DarkXero said:

 

Something you can do here is copy-paste jungletrees.lua into a prefabs folder inside your mod.

That way the game will load that file, instead of the vanilla one. Then you can change the file to your liking.

 

Damn - I was trying to avoid doing that -

But the needs must, I suppose

 

Thank you again

 

 

Link to comment
Share on other sites

21 minutes ago, MidrealmDM said:

Damn - I was trying to avoid doing that -

But the needs must, I suppose

local growablepostinit = false
local function jungletreepostinit(inst)
	if not growablepostinit then
		growablepostinit = true -- So it only runs once
		local growth_stages = inst.components.growable.stages
		local old_fn = growth_stages[3].fn -- Stage 3 is the tall stage
		growth_stages[3].fn = function(inst)
			old_fn(inst) -- This holds SetTall with all its locals
			if math.random() < 0.5 then
				inst.components.lootdropper:AddChanceLoot("flint", 1.0)
			else
				inst.components.lootdropper:AddChanceLoot("rocks", 1.0)
			end
		end
	end
end

AddPrefabPostInit("jungletree", jungletreepostinit)

You can also do this.

It appends the new loot you want to the lootdropper of the tall stage.

Downside is that isn't exactly the same as your code, so eggs or bananas also drop.

Link to comment
Share on other sites

@DarkXero

5 minutes ago, DarkXero said:

You can also do this.

It appends the new loot you want to the lootdropper of the tall stage.

Downside is that isn't exactly the same as your code, so eggs or bananas also drop.

 

So that appears to simply append a chance for additional loot drop at the end

So it would drop either eggs or bananas as normal

AND then also drop either flint and rocks at the same time...

 

There isn't a way to modify it so it dropped only one of the following: eggs, bananas, flint, or rocks ?

(Aside from the previously mentioned method of replacing the entire jungletrees prefab.)

Link to comment
Share on other sites

25 minutes ago, MidrealmDM said:

There isn't a way to modify it so it dropped only one of the following: eggs, bananas, flint, or rocks ?

local growablepostinit = false
local function jungletreepostinit(inst)
	if not growablepostinit then
		growablepostinit = true -- So it only runs once
		local growth_stages = inst.components.growable.stages
		local old_fn = growth_stages[3].fn -- Stage 3 is the tall stage
		growth_stages[3].fn = function(inst)
			old_fn(inst) -- This holds SetTall with all its locals
			local chanceloot = inst.components.lootdropper.chanceloot or {}
			for i, v in ipairs(chanceloot) do
				if v.prefab == "bird_egg" then
					v.prefab = "flint"
					break
				elseif v.prefab == "cave_banana" then
					v.prefab = "rocks"
					break
				end
			end
		end
	end
end

AddPrefabPostInit("jungletree", jungletreepostinit)

This yields the same as your code.

It replaces the existing stuff in the chanceloot table.

Link to comment
Share on other sites

@DarkXero

I hate to bother you after all your help with the jungle tree,

 

But I've been trying to engineer the vine bush to drop add an additional item as well.
But the vine bush "vine.lua" isn't handled the same way as the tree -

the vine bush is hackable(harvestable) at stage 0, and barren at stage 1,

but does not have inst.components.growable.stages,

 

so I kept messing around, and after several attempts, although I'm no longer crashing the game,

the code I settled on apparently does nothing.

 

Ideally, i would like for the chance to happen when hacked, but not when dug up.
But if it can't be separated, then that is fine too.

 

I feel like I am close - but maybe not.

=-=-=-

local growablepostinit = false
local function vinebushpostinit(inst)
    if not growablepostinit then
        growablepostinit = true -- So it only runs once
        local hackable_vine = inst.components.hackable
        local old_vine_fn = hackable_vine
        hackable_vine = function(inst)
            old_vine_fn(inst) -- I think this holds vine bush with all its locals?
            if math.random() < 0.8 then -- reduce chance after testing to 0.1 or less
                inst.components.lootdropper:AddChanceLoot("tomango", 1.0)
            end    
        end
    end
end

Link to comment
Share on other sites

18 hours ago, MidrealmDM said:

Ideally, i would like for the chance to happen when hacked, but not when dug up

local function SpawnLoot(inst, lootprefab)
	local loot = GLOBAL.SpawnPrefab(lootprefab)
	local x, y, z = inst.Transform:GetWorldPosition()
	loot.Transform:SetPosition(x, y, z)
	inst:ApplyInheritedMoisture(loot)
	local angle = math.random() * 2 * GLOBAL.PI
	local speed = math.random()
	loot.Physics:SetVel(speed * math.cos(angle), GLOBAL.GetRandomWithVariance(8, 4), speed * math.sin(angle))
end

AddPrefabPostInit("bush_vine", function(inst)
	inst:ListenForEvent("hacked", function(inst, data)
		SpawnLoot(inst, "flint")
	end)
end)

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.

×
  • Create New...