Greetings,
I am currently working on creating a script that allows the player to spawn a tree and then chop it down to gather wood. I have the essentials completed, but I'm stuck trying to figure out how I can make the tree able to be chopped down as well as chopped further to gain logs. I will post my script...
I am also open to any critiques/advice as I am a newbie coder. Thank you.
AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include('shared.lua')
function ENT:Initialize()
self:SetModel( "models/props_foliage/ash02.mdl" )
self:PhysicsInit( SOLID_VPHYSICS )
self:SetMoveType( MOVETYPE_NONE )
self:SetSolid( SOLID_VPHYSICS )
self:SetPos(self:GetPos() + Vector(0,0,-10))
self.chopTree = ents.Create("prop_physics")
self.chopTree:SetModel("models/props_foliage/ash02.mdl")
end
function ENT:felledTree()
local chance= math.random(1, 2)
if chance == 1 then
self.chopTree:SetPos(self:GetPos() + Vector(0, 7 , 20))
self.chopTree:Spawn()
elseif chance == 2 then
self.chopTree:SetPos(self:GetPos() + Vector(0, -10 , 20))
self.chopTree:Spawn()
end
local phys = self.chopTree:GetPhysicsObject()
phys:Wake()
phys:SetMass(5000)
end
function ENT:treeFeller()
local stump = ents.Create("prop_physics")
stump:SetModel("models/props_foliage/stump02.mdl")
stump:PhysicsInit( SOLID_VPHYSICS )
stump:SetMoveType( MOVETYPE_NONE )
stump:SetSolid( SOLID_VPHYSICS )
stump:SetPos(self:GetPos() + Vector(0,0,0))
stump:Spawn()
local phys = stump:GetPhysicsObject()
phys:EnableMotion(false)
end
function ENT:OnTakeDamage(damage)
if damage:IsDamageType(DMG_BULLET) then
return nil
else
local chance = math.random(1 , 50)
if chance <= 10 then
self:EmitSound("tree_fall.wav")
self:Remove()
self:treeFeller()
self:felledTree()
end
end
end
I would make an axe swep. In the OnTakeDamage of the tree, you could check if the attacker is the axe swep.
The felled tree will probably need to be its own scripted entity too, not a prop_physics.
You can call CTakeDamageInfo/GetAttacker in OnTakeDamage to get the player who hit the tree to give them logs.
Thank you! Now I'm not too familiar with how to couple scripts together. I would assume I'd have to build another init.lua and reference it from the first script. The axe is a great idea though!
You don't reference them directly. For example:
function ENT:OnTakeDamage(damage)
if damage:GetAttacker():GetActiveWeapon():GetClass() == "weapon_axe" then
local chance = math.random(1 , 50)
if chance <= 10 then
self:EmitSound("tree_fall.wav")
self:Remove()
self:treeFeller()
self:felledTree()
end
end
end
All you need is the weapon's classname, which is set automatically to the name of the folder containing the weapon's init.lua.
Should probably check if the attacker and weapon are valid first.
Fantastic, seems good to use. And I can use this for the other scripted tree too?
Yes, except you would create logs instead of a stump and trunk.
Also use IsValid() on the attacker and his weapon, as they both might be nil when OnTakeDamage is called.
All right, thank you so much for your help!
I'd have some variable set on the weapon instead of a class check, like SWEP.canChopWood, then you just check if the inflicting weapon has canChopWood. This would allow you to create more wood chopping sweps without updating the wood entity each time.
They might be NULL (different than nil) which means you can use the IsValid member function. Ex.
local pAttacker = dmg:GetAttacker()
if (pAttacker:IsValid()) then
local pWeapon = pAttacker:GetActiveWeapon()
if (pWeapon:IsValid()) then
-- Code
end
end
Ah yes, perfect for future uses. Thank you!
Sorry, you need to Log In to post a reply to this thread.