Project Zomboid

Project Zomboid

Trees Have Loot
Von Bon Dec 25, 2024 @ 1:59pm
code for a Quick Fix + Branches When Cutting
Right, so, there's currently a bug with the mod and while waiting for an official update I fixed it and the mod-dev requested I post it into a thread. So for anyone else wanting to any item with SharpKnife ( like sharp flint rocks ) to cut branches, and to have both branches and saplings added : here is a quick way.

in ISCutBranchFromTreeAction.lua
CutBranchFromTreeAction = ISBaseTimedAction:derive("CutBranchFromTreeAction"); local listOfKnives = tostring(getScriptManager():getItemsTag("SharpKnife")); local listOfAxes = tostring(getScriptManager():getItemsTag("ChopTree")); function CutBranchFromTreeAction:isValid()

You'll want to place the two local arrays near the top, right above the Function.
This performs an array/list of all items with the tags of "SharpKnife" and "ChopTree".




A bit down, find this block of code and alter it as thus :

function CutBranchFromTreeAction:start() local player = self.character local primary = player:getPrimaryHandItem(); local isoTree = self.isoTree player:faceLocation(isoTree:getX(), isoTree:getY()) if primary and (string.find(listOfKnives, primary:getType()) or string.find(listOfAxes, primary:getType())) then self:setActionAnim(CharacterActionAnims.Chop_tree); else self:setActionAnim("Loot"); player:clearVariable("LootPosition"); self:setAnimVariable("LootPosition", "High"); end self:setOverrideHandModels(primary, nil); end

The major change here is the if statement for checking if the primary-held item is any item in the arrays made earlier. If you find that being able to use a knife to cut branches is not to your liking or too-easy, then alter it with :

if primary and string.find(listOfAxes, primary:getType()) then




Scroll down some more and find and change the first IF Statement in the function CutBranchFromTreeAction:perform(), and then if wanting to add branches in the drop or whatever else you want then :

if primary and (string.find(listOfKnives, primary:getType()) or string.find(listOfAxes, primary:getType())) then if ZombRand(2) == 0 then treeSquare:AddWorldInventoryItem("Base.Sapling", treeSquare:getX() + ZombRand(0, 10)/10, treeSquare:getY() + ZombRand(0, 10)/10, treeSquare:getZ()); else treeSquare:AddWorldInventoryItem("Base.TreeBranch2", treeSquare:getX() + ZombRand(0, 10)/10, treeSquare:getY() + ZombRand(0, 10)/10, treeSquare:getZ()); end if ZombRand(30 - player:getPerkLevel(Perks.Strength)) == 0 then if ZombRand(2) == 0 then treeSquare:AddWorldInventoryItem("Base.DeadSquirrel", treeSquare:getX() + ZombRand(0, 10)/10, treeSquare:getY() + ZombRand(0, 10)/10, treeSquare:getZ()); else treeSquare:AddWorldInventoryItem("Base.DeadBird", treeSquare:getX() + ZombRand(0, 10)/10, treeSquare:getY() + ZombRand(0, 10)/10, treeSquare:getZ()); end end else local item = player:getInventory():AddItem("Base.Sapling"); local textToDisplay = item:getDisplayName(); HaloTextHelper.addTextWithArrow(player, textToDisplay, true, HaloTextHelper.getColorGreen()); end

The changes here, being : checking the primary held item for arrays, again ( don't know why double-checked), and adding another conditional if statement of causing a 50-50 drop between saplings or branches being produced.
Everything else is practically left intact.

The primary reason I found this was I was trying a survival run and when I got a sharp flint, it allowed me to try cutting a branch off a tree but produced an error. The bug is TreeMenu.lua allows showing the option and initiating the action with anything tagged SharpKnife and ChopTree, but the check being performed during CutBranchFromTreeAction:start() and CutBranchFromTreeAction:perform() INSTEAD checks if the primary held item is in the CATEGORY of "Axe" instead of the list of arrays. And that you get no branches, only saplings.

if it's requested or desired by a bunch of people I could go about including randomizing through a list of possible tree/branch related drops from the new .42 update. And maybe seeing if I can make the code better : i just quick-fixed it is all.
< >
Showing 1-3 of 3 comments
Deon ☣ Dec 29, 2024 @ 10:42am 
Thank you for sharing with us!
please fix this mod !!! because trees without branches wtf ???
PepperCat  [developer] Jan 6 @ 6:33am 
The validation in the start function is just for the type of animation to play. If an Axe, a Chop animation is played, if a Knife, then the other animation is played.
That's it. The bug with Sharp Stone was fixed in the TreeMenu file excluding the Sharp Stone from the list.
Thanks for this! :)
< >
Showing 1-3 of 3 comments
Per page: 1530 50