Jump to content

Recommended Posts

I'm trying to keep track of when the player chops down a tree, but i can't figure it out
i have this code right now:

Spoiler
function TreePostInit(inst)
	print("[Analytics] Tree evergreen_sparse spawned!")
	print("[Analytics] components: ")
	printTable(inst.components)
	if inst.components.workable == nil then
		print("[Analytics] components.workable is nil!")
		return
	end
	local oldonfinish = inst.components.workable.onfinish
	inst.components.workable.onfinish = function(inst, chopper)
		if chopper then
			print("[Analytics] Tree chopped down!")
			print("[Analytics] Chopped by: " .. chopper.name)
		end
		oldonfinish(inst, chopper)
	end
end

local trees = {
	"evergreen",
	"evergreen_normal",
	"evergreen_tall",
	"evergreen_short",
	"twiggy_short"
}
for k,v in pairs(trees) do
	AddPrefabPostInit(v, TreePostInit)
end

 

 

Instead of editing the tree prefabs it would be better to override the player prefab. Something like this should do the trick:

local function OnFinishWork(inst, data)
    if data.action == ACTIONS.CHOP then
        -- Your code here
    end
end

inst:ListenForEvent("finishedwork", OnFinishWork)

 

  • Like 1

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