• CurTime() Timer
    8 replies, posted
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 [CODE]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[/CODE] cl_init.lua [CODE]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 [/CODE] 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 !
Is there a particular reason you want to use CurTime() over a timer? If not, try this: [lua]AddCSLuaFile( "cl_init.lua" ) include('shared.lua') local function doTime() timeCount = 120 timer.Create( "countTimer", 1, timeCount, function() timeCount = timeCount - 1 end) 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) local phys = self:GetPhysicsObject() if (phys:IsValid()) then phys:Wake() end doTime() -- Calling it on Init should reset the timer each time, since it gets set each time doTime() is called. end function ENT:Think() self:SetNWInt( "time", timeCount ) end[/lua]
[QUOTE=angrypygmy;49435519]Is there a particular reason you want to use CurTime() over a timer? If not, try this: [lua]AddCSLuaFile( "cl_init.lua" ) include('shared.lua') local function doTime() timeCount = 120 timer.Create( "countTimer", 1, timeCount, function() timeCount = timeCount - 1 end) 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) local phys = self:GetPhysicsObject() if (phys:IsValid()) then phys:Wake() end doTime() -- Calling it on Init should reset the timer each time, since it gets set each time doTime() is called. end function ENT:Think() self:SetNWInt( "time", timeCount ) end[/lua][/QUOTE] 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.
[QUOTE=KTBM;49436029]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.[/QUOTE] How do we get a valvue from a networked float ? I have seen the wiki but i get errors from using self:GetTimer()
if you wanted to get the time in seconds do [lua]self:GetTimer() - CurTime()[/lua] and what errors are you getting?
[QUOTE=KTBM;49439093]if you wanted to get the time in seconds do [lua]self:GetTimer() - CurTime()[/lua] and what errors are you getting?[/QUOTE] Well i achieved what i was trying to do but now it gives a error everything works fine though. my code init [CODE]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[/CODE] cl init [CODE]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[/CODE] shared [CODE]ENT.Type = "anim" ENT.Base = "base_gmodentity" ENT.Category = "shootby"; ENT.Author= "shootby" ENT.Contact= "lol" ENT.Purpose= "Ehahaal" ENT.Instructions= "Use wisely my firend" ENT.Spawnable = true ENT.AdminSpawnable = false ENT.PrintName = "test"; ENT.Category = "Berk"; function ENT:SetupDataTables() self:NetworkVar("Float", 0, "Timer") end[/CODE] [CODE]....error attempted to call method GetTimer (a nil valvue) [/CODE]
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.
Sorry, you need to Log In to post a reply to this thread.