I've got a strange issue what was previously not a problem, I have an entity that has a value and adds 1 every 10 seconds then let's the player take that value followed by resetting it back to 0 but for some reason in the USE hook for the entity it always finds the value to be 0 when the rest of the code shows different.
Here is the code with a few annotated lines.
[CODE]AddCSLuaFile( "cl_init.lua" ) AddCSLuaFile( "shared.lua" )
include('shared.lua')
ENT.color = Color(255, 0, 0, 255) //set the color of the model
ENT.amount = 0 //Records how many elements exist ///////////////////////THIS IS THE VALUE//////////////////////////
ENT.MyHealth = 500 //entity health
function ENT:Initialize()
self:SetModel( "models/props_c17/canister01a.mdl" )
self:PhysicsInit( SOLID_VPHYSICS )
self:SetMoveType( MOVETYPE_NONE )
self:SetSolid( SOLID_VPHYSICS )
local phys = self:GetPhysicsObject()
if (phys:IsValid()) then
phys:Wake()
end
end
function ENT:Use(activator, caller)
///////////////////////ALWAYS SHOWS VALUE OF ZERO???////////////////////////////////
if self.amount == 0 then
activator:PrintMessage( HUD_PRINTTALK, "There are no elements collected.")
return
end //if no entities exist, exit
activator:PrintMessage( HUD_PRINTTALK, "You collected "..self.amount.." fire element(s).") //inform the player
self.amount = 0 //reset the elements
self:SetNWInt("Amount", self.amount)
end
function ENT:Think()
return
end
function ENT:SpawnFunction(ply, tr)
if ( !tr.Hit ) then return end
local ent = ents.Create( self.Classname )
ent:SetPos( tr.HitPos + tr.HitNormal * 30)
ent:SetColor(ent.color)
//ent:SetOwner(ply) //set the owner
ent:Spawn()
ent:Activate()
timer.Create(ent:EntIndex(), 10, 0, function()
self.amount=self.amount+1
///////////////////////ALWAYS SHOWS THE CORRECT VALUE////////////////////////////////
ent:SetNWInt("Amount", self.amount) //add 1 element every 10 seconds
end)
return ent
end
function ENT:OnRemove( )
local effectdata = EffectData()
effectdata:SetOrigin( self:GetPos() )
effectdata:SetScale( 10 )
effectdata:SetMagnitude( 10 )
util.Effect( "HelicopterMegaBomb", effectdata, true, true )
timer.Destroy(self:EntIndex()) //remove the timer for elemental increase
end
function ENT:OnTakeDamage(dmg)
if !dmg:GetAttacker():IsPlayer() then return end //make sure it's a player
if dmg:GetAttacker() == self:GetOwner() then //Make sure it isn't the owner
dmg:GetAttacker():PrintMessage( HUD_PRINTTALK, "You cannot destroy your lab.")
return
elseif dmg:GetAttacker():Team() == 1 then //Alchmists cannot destroy
dmg:GetAttacker():PrintMessage( HUD_PRINTTALK, "You cannot destroy a lab as an Alchemist.")
return
elseif dmg:GetAttacker():Team() == self:GetOwner():Team() then //Cannot destroy your teammates
dmg:GetAttacker():PrintMessage( HUD_PRINTTALK, "You cannot destroy a lab owned by someone of the same team.")
return
end
self:TakePhysicsDamage(dmg)
if(self.MyHealth <= 0) then return end
self.MyHealth = self.MyHealth - dmg:GetDamage()
if(self.MyHealth <= 0) then
self:Remove()
dmg:GetAttacker():PrintMessage( HUD_PRINTTALK, "You destroyed a Fire Alchemy Lab and gained 5 Imperial coins.")
//dmg:GetAttacker().Coins = dmg:GetAttacker().Coins + 5 //give imperial coins
end
end[/CODE]
So basically, in the USE hook self.amount is always 0 but it shouldn't be, could anyone assist?
Try networking it , or changing the value cause i don't see you saying anything other than self.amount = 0
In the spawn function I create a timer that increments the value +1 each iteration (10 seconds per iteration)
[CODE]self.amount=self.amount+1[/CODE]
That code always shows the amount as the correct value and used to work but now has decided to not work since I appended the damage code.
The value is also networked for client side viewing what works 200% but the server side code isn't working for some reason.
[editline]14th September 2011[/editline]
Fixed, the problem was that I called the timer during creation of the entity and I referred to it as self but I needed ent.
Sorry, you need to Log In to post a reply to this thread.