Jump to content

[SOLVED] Adding/replacing drops.


Recommended Posts

Hello!

I am trying to make a new item that is supposed to drop from birchnut trees instead of regular logs. Unfortunatly I don't know how to do it.

I know that I can add an item to drops of a specific structure/enemy with "inst.components.lootdropper:AddChanceLoot("item_name", "chance_of_droping")" but I can't find anywhere how to replace drops or at least remove them to then add the new one in their place.

Also, looking at the "deciduoustrees.lua" file I noticed that all versions of birchnut trees (tall, normal, short, burnt, monster[I assume it's the poison birchnut tree]) are present in this one file so I'm wondering if this has some effects on the AddChanceLoot.

I'd be thankful for any help regarding this topic.

[SOLUTION]

So I had this problem for a while now and I think I have a solution.

Using the "deciduoustrees.lua" file I tried to edit the "builds" and replace log drops. I, unfortunately, was unable to do it, so I took a different route and added "AddPrefabPostInit" as I saw this piece of code:

if inst.components.growable ~= nil then
        if inst.components.growable.stage == 1 then
            inst.components.lootdropper:SetLoot(GetBuild(inst).short_loot)
        elseif inst.components.growable.stage == 2 then
            inst.components.lootdropper:SetLoot(GetBuild(inst).normal_loot)
        else
            inst.components.lootdropper:SetLoot(GetBuild(inst).tall_loot)
        end
end

I thought it could work if I overwrite this code and replace "GetBuild(inst).)" with my drops. So I did. And it didn't work.

BUT, it was just because every time the tree would grow the main code would overwrite my overwritation(?), so I used "ListenForEvent" and set it to listen for "animover". So now the code looks like this:

AddPrefabPostInit("deciduoustree", function(inst)
	local function setloot(inst)
	    if inst.components.growable ~= nil then
			if inst.components.growable.stage == 1 then
				inst.components.lootdropper:SetLoot({"birchwood"})
			elseif inst.components.growable.stage == 2 then
				inst.components.lootdropper:SetLoot({"birchwood", "birchwood"})
			elseif inst.components.growable.stage == 3 and inst.target_leaf_state == "colorful" then
				inst.components.lootdropper:SetLoot({"birchwood", "birchwood", "birchwood", "acorn"})
			else
				inst.components.lootdropper:SetLoot({"birchwood", "birchwood", "birchwood"})
			end
		end
	end
	
	inst:ListenForEvent("animover", setloot)
end)

But there, still, was 1 problem left. I had to, somehow, make the stump drop my item instead of a normal log and, trust me, that took waaaay longer then it should have.

In the end this code worked for the stump:

AddPrefabPostInit("deciduoustree", function(inst)
	local function whendugup(inst)  --[[This is the function that drops the item]]
		inst.components.lootdropper:SpawnLootPrefab(inst.monster and "livinglog" or "birchwood")
		inst:Remove()
	end

	local function checkfortag(inst) --[[In order to access the drops I had to use tags because there was no other way to change the function that makes stumps drop items]]
		if inst:HasTag("stump") then --[[This one is for when the tree is chopped down, so it checks for the tag]]
			inst.components.workable:SetOnFinishCallback(whendugup)
		end
	end
	
	if inst:HasTag("stump") then --[[This one is when the stump is generated by the world and not created by chopping trees]]
		inst.components.workable:SetOnFinishCallback(whendugup)
	end

	inst:ListenForEvent("workfinished", checkfortag) --[[I used the "workfinished" event as it's pushed when the tree is chopped down]]
end)

This is my way of solving this problem. I'm sure it's not the most efficient way but it works for me. I'm, also, pretty confident that this won't work for anything else other then birchnut trees but I think studying this example could help some modders with their mods.

So, if you found this post helpful in any way, I'm happy it helped.

Edited by IThatGuyI
Solution found
  • Like 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...