• Entity Question
    14 replies, posted
Hello, I am trying to make a mining woodcutting script, and I want to make it so when the entity is at 0 health it disappears and creates a new entity (logs). I managed to get it to disapear, but the logs I am having issues with, Also I would like to respawn the tree after a set time, but I'm having problems getting curtime in it. Here is my code so far, and it doesn't work. If possible, please leave a explanation or a link so I can learn this I don't want easy answers. [CODE]function ENT:Initialize() self:SetModel( "models/props/CS_militia/tree_large_militia.mdl" ) self:PhysicsInit(SOLID_VPHYSICS) self:SetMoveType(MOVETYPE_VPHYSICS) self:SetSolid(SOLID_VPHYSICS) end function ENT:Use( activator, caller ) return end function ENT:Think() if(self.OurHealth <= 0) then ents.Create("lognormal") end end -- Take damage when hit or shot. ENT.OurHealth = 100; function ENT:OnTakeDamage(dmg) self:TakePhysicsDamage(dmg); if(self.OurHealth <= 0) then return; end self.OurHealth = self.OurHealth - dmg:GetDamage(); if(self.OurHealth <= 0) then self:Remove(); end end[/CODE] Thanks for your time!
[CODE]function ENT:Initialize() self:SetModel( "models/props/CS_militia/tree_large_militia.mdl" ) self:PhysicsInit(SOLID_VPHYSICS) self:SetMoveType(MOVETYPE_VPHYSICS) self:SetSolid(SOLID_VPHYSICS) self.OurHealth = 100; end function ENT:Use( activator, caller ) return end function ENT:Think() end function ENT:OnTakeDamage(dmg) self:TakePhysicsDamage(dmg); if(self.OurHealth <= 0) then return; end self.OurHealth = self.OurHealth - dmg:GetDamage(); if(self.OurHealth <= 0) then local log = ents.Create("lognormal") log:SetPos(self.Entity:GetPos()) log:Spawn() self:Remove(); end end[/CODE]
Oh thank you! That works perfect, and I do understand what you did here. As for a second question, how would I go about spawning a new tree after the health hits 0 maybe 20 seconds, I tried a curtimer, but couldn't get it working in the function I know I am doing it wrong like the last one.
[QUOTE=sggamer;47622900]Oh thank you! That works perfect, and I do understand what you did here. As for a second question, how would I go about spawning a new tree after the health hits 0 maybe 20 seconds, I tried a curtimer, but couldn't get it working in the function I know I am doing it wrong like the last one.[/QUOTE] Show us your code? Use this: [url]http://wiki.garrysmod.com/page/ENTITY/OnRemove[/url] and within it use this: [url]http://wiki.garrysmod.com/page/ents/Create[/url] along with: [url]http://wiki.garrysmod.com/page/timer/Simple[/url]
[CODE] function ENT:Initialize() self:SetModel( "models/props/CS_militia/tree_large_militia.mdl" ) self:PhysicsInit(SOLID_VPHYSICS) self:SetMoveType(MOVETYPE_VPHYSICS) self:SetSolid(SOLID_VPHYSICS) self.OurHealth = 100; end function ENT:Use( activator, caller ) end function ENT:Think() if CurTime() > self.RespawnTime then self:Respawn() end end function ENT:Respawn() --Make the tree visible and have collisions end function ENT:Break() self.RespawnTime = CurTime() + 20 --Make the tree invisible and collisionless end function ENT:OnTakeDamage(dmg) self:TakePhysicsDamage(dmg); if(self.OurHealth <= 0) then return; end self.OurHealth = self.OurHealth - dmg:GetDamage(); if(self.OurHealth <= 0) then self:Break() local log = ents.Create("lognormal") log:SetPos(self.Entity:GetPos()) log:Spawn() end end [/CODE]
[QUOTE=thegrb93;47623181][CODE] function ENT:Initialize() self:SetModel( "models/props/CS_militia/tree_large_militia.mdl" ) self:PhysicsInit(SOLID_VPHYSICS) self:SetMoveType(MOVETYPE_VPHYSICS) self:SetSolid(SOLID_VPHYSICS) self.OurHealth = 100; end function ENT:Use( activator, caller ) end function ENT:Think() if CurTime() > self.RespawnTime then self:Respawn() end end function ENT:Respawn() --Make the tree visible and have collisions end function ENT:Break() self.RespawnTime = CurTime() + 20 --Make the tree invisible and collisionless end function ENT:OnTakeDamage(dmg) self:TakePhysicsDamage(dmg); if(self.OurHealth <= 0) then return; end self.OurHealth = self.OurHealth - dmg:GetDamage(); if(self.OurHealth <= 0) then self:Break() local log = ents.Create("lognormal") log:SetPos(self.Entity:GetPos()) log:Spawn() end end [/CODE][/QUOTE] I get this error when I tried [ERROR] gamemodes/runescape/entities/entities/treenormal/init.lua:20: attempt to compare nil with number 1. unknown - gamemodes/runescape/entities/entities/treenormal/init.lua:20 It is on this line to be more exact if CurTime() > self.RespawnTime then
Ah forgot about that. [code] function ENT:Think() if self.RespawnTime and CurTime() > self.RespawnTime then self.RespawnTime = nil self:Respawn() end end [/code]
The tree doesn't damage now with that put in. Here is what I got, I do have another system I am kinda building off of, but don't want to post the code cause I paid for it on scriptfodder and don't know the rules here with that. [CODE]function ENT:Initialize() self:SetModel( "models/props/CS_militia/tree_large_militia.mdl" ) self:PhysicsInit(SOLID_VPHYSICS) self:SetMoveType(MOVETYPE_VPHYSICS) self:SetSolid(SOLID_VPHYSICS) self.OurHealth = 100; end function ENT:Use( activator, caller ) end function ENT:Think() if self.RespawnTime and CurTime() > self.RespawnTime then self.RespawnTime = nil self:Respawn() end end function ENT:Respawn() --Make the tree visible and have collisions end function ENT:Break() self.RespawnTime = CurTime() + 20 --Make the tree invisible and collisionless end function ENT:OnTakeDamage(dmg) self:TakePhysicsDamage(dmg); if(self.OurHealth <= 0) then return; end self.OurHealth = self.OurHealth - dmg:GetDamage(); if(self.OurHealth <= 0) then self:Break() local log = ents.Create("lognormal") log:SetPos(self.Entity:GetPos()) log:Spawn() end end[/CODE] Thanks for your help!
[QUOTE=sggamer;47623734]The tree doesn't damage now with that put in. Here is what I got, I do have another system I am kinda building off of, but don't want to post the code cause I paid for it on scriptfodder and don't know the rules here with that. [CODE]function ENT:Initialize() self:SetModel( "models/props/CS_militia/tree_large_militia.mdl" ) self:PhysicsInit(SOLID_VPHYSICS) self:SetMoveType(MOVETYPE_VPHYSICS) self:SetSolid(SOLID_VPHYSICS) self.OurHealth = 100; end function ENT:Use( activator, caller ) end function ENT:Think() if self.RespawnTime and CurTime() > self.RespawnTime then self.RespawnTime = nil self:Respawn() end end function ENT:Respawn() --Make the tree visible and have collisions end function ENT:Break() self.RespawnTime = CurTime() + 20 --Make the tree invisible and collisionless end function ENT:OnTakeDamage(dmg) self:TakePhysicsDamage(dmg); if(self.OurHealth <= 0) then return; end self.OurHealth = self.OurHealth - dmg:GetDamage(); if(self.OurHealth <= 0) then self:Break() local log = ents.Create("lognormal") log:SetPos(self.Entity:GetPos()) log:Spawn() end end[/CODE] Thanks for your help![/QUOTE] I'm not sure why it isn't taking damage, but make sure you reset the health to 100 in the Respawn function.
Instead of using this[CODE]function ENT:Think() if self.RespawnTime and CurTime() > self.RespawnTime then self.RespawnTime = nil self:Respawn() end end[/CODE] couldn't you just create a timer every time the tree is brokering and only let it run once? for example [CODE]if(self.OurHealth <= 0) then self:Break() local log = ents.Create("lognormal") log:SetPos(self.Entity:GetPos()) log:Spawn() --create the respawn timer here, and inside the function of the respawn timer would be self respawn() timer.Create("respawntimer",20, 1, function() --the 20 means 20seconds, the 1 means how many cycles the timer dose self:Respawn() end) end[/CODE] now im not sure if it would work, or if I coded it correctly, because im a novice lua coder to, but I dont see why it wouldnt work
If you do the timer that way, it would fail if more than one tree is cut at the same time. You would need to use timer.Simple or timer.Create("treeRespawn"..self:EntIndex(),20,1,function() self:Respawn() end) but yes, a timer would work too. It might be preferable to use the Think method though since it is called less often and may use less cpu than the timer method. Also you won't have to worry about the Respawn function being called if the tree gets deleted while the respawn timer is going.
[QUOTE=thegrb93;47625976]If you do the timer that way, it would fail if more than one tree is cut at the same time. You would need to use timer.Simple or timer.Create("treeRespawn"..self:EntIndex(),20,1,function() self:Respawn() end) but yes, a timer would work too. It might be preferable to use the Think method though since it is called less often and may use less cpu than the timer method. Also you won't have to worry about the Respawn function being called if the tree gets deleted while the respawn timer is going.[/QUOTE] I agree, he should use timer.Simple as he wouldn't need access to the timer.
I said he could use that. You might want to cancel respawning the tree or extend the respawn timer though so I wouldn't say he doesn't need access to the timer. Just use the Think method in my orignal code. Simple and easy.
[QUOTE=thegrb93;47626253]I said he could use that. You might want to cancel respawning the tree or extend the respawn timer though so I wouldn't say he doesn't need access to the timer. Just use the Think method in my orignal code. Simple and easy.[/QUOTE] Oops sorry, I didn't notice. I'll edit my post. Thanks.
Okay so here is what I did, I tried it the way you said and the tree wouldn't take damage. So I made this code and it worked out. [CODE]function ENT:OnRemove() local tree = ents.Create( "treenormal" ) tree:SetPos(self.Entity:GetPos() ) timer.Simple( 15, function() tree:Spawn() end ) end[/CODE] Thanks everybody.
Sorry, you need to Log In to post a reply to this thread.