Hello i made a timer that counts down to zero using CurTime() but when i remove or add a another entity it just goes synced.Lets say i spawned my entity it starts to count down from 120 5 seconds have passed its 115 now i remove the entity spawn it again after 5 secs and it says 110 seconds.
init.lua
AddCSLuaFile( "cl_init.lua" )
include('shared.lua')
local timecount = 120
function ENT:Initialize()
self:SetModel( "models/props_lab/crematorcase.mdl" )
self:PhysicsInit( SOLID_VPHYSICS )
self:SetMoveType( MOVETYPE_VPHYSICS )
self:SetSolid( SOLID_VPHYSICS )
self:SetUseType(SIMPLE_USE)
local phys = self:GetPhysicsObject()
if (phys:IsValid()) then
phys:Wake()
end
end
local m = CurTime()
function ENT:Think()
self:SetNWInt("time", timecount - (CurTime() - m));
end
cl_init.lua
include("shared.lua")
function ENT:Initialize()
end
function ENT:Draw()
self:DrawModel()
local Pos = self:GetPos()
local Ang = self:GetAngles()
local owner = "Time: "..self:GetNWInt("time").."s";
surface.SetFont("HUDNumber5")
local text = "AAA"
local TextWidth = surface.GetTextSize(text)
local TextWidth2 = surface.GetTextSize(owner)
Ang:RotateAroundAxis(Ang:Up(), 90)
cam.Start3D2D(Pos + Ang:Up() * 11.5, Ang, 0.11)
draw.WordBox(2, -TextWidth*0.5, -30, text, "HUDNumber5", Color(140, 0, 0, 100), Color(255,255,255,255))
draw.WordBox(2, -TextWidth2*0.5, 18, owner, "HUDNumber5", Color(140, 0, 0, 100), Color(255,255,255,255))
cam.End3D2D()
end
if i was not clear i am trying to make it so evertime i spawn it it stars to countdown from 120 again.it doenst stop counting when i remove it .Any help is appericated !
Well i try to avoid usage of timers since i heared that it is not brilliant for multiple entities do you think that it would cause problems if multiple ppl was using this at the same time in my server ?
EDIT : there is a issue with this code i run it and it works perfectly with a single ent but multiple entitys is still buggy.I spawn one of my entity it works fine and lets say 100s left when i spawn antoher one it just sets back to 120s.
This is how I would do It
[lua]
AddCSLuaFile( “cl_init.lua” )
include(‘shared.lua’)
local timecount = 120
function ENT:SetupDataTables()
self:NetworkVar(“Float”, 0, “Timer”)
end
function ENT:Initialize()
self:SetModel( “models/props_lab/crematorcase.mdl” )
self:PhysicsInit( SOLID_VPHYSICS )
self:SetMoveType( MOVETYPE_VPHYSICS )
self:SetSolid( SOLID_VPHYSICS )
self:SetUseType(SIMPLE_USE)
self:SetTimer(CurTime() + timecount)
local phys = self:GetPhysicsObject()
if (phys:IsValid()) then
phys:Wake()
end
end
function ENT:Think()
end[/lua]
I’d create the timer when the ent initializes.
Well i achieved what i was trying to do but now it gives a error everything works fine though.
my code
init
AddCSLuaFile( "cl_init.lua" )
include('shared.lua')
local timecount = 120
function ENT:Initialize()
self:SetModel( "models/props_lab/crematorcase.mdl" )
self:PhysicsInit( SOLID_VPHYSICS )
self:SetMoveType( MOVETYPE_VPHYSICS )
self:SetSolid( SOLID_VPHYSICS )
self:SetUseType(SIMPLE_USE)
self:SetTimer(CurTime() + timecount)
local phys = self:GetPhysicsObject()
if (phys:IsValid()) then
phys:Wake()
end
end
cl init
include("shared.lua")
function ENT:Initialize()
end
function ENT:Draw()
self.Entity:DrawModel()
local Pos = self:GetPos()
local Ang = self:GetAngles()
local owner = self:GetTimer() - CurTime()
surface.SetFont("HUDNumber5")
local TextWidth3 = surface.GetTextSize(owner)
Ang:RotateAroundAxis(Ang:Up(), 90)
cam.Start3D2D(Pos + Ang:Up() * 6.2, Ang, 0.11)
draw.WordBox(2, -TextWidth3*0.5, -8, owner, "HUDNumber5", Color(255, 255, 255, 100), Color(0,0,0,255))
cam.End3D2D()
end
function ENT:Think()
end
instead of
local timecount
do
ENT.timecount or ENT.TimeCount
and rename every “timecount” in the code to self.timecount
self. makes a value unique to the entity, local makes it global
I didn’t really read much of this thread but to answer the guy who was saying “Why not use timers instead.” The answer is prediction. Using timers messes with prediction why CurTime doesn’t.