How do I draw text that counts down

Hi,

I want to create a bomb entity.
What I wonder is how do I, in the cl_init.lua, draw the time remaining until explosion?
I have a timer on the ENT:Use(), in my init.lua:


timer.Simple(explodeTime function() ENT:Explode() end)

I want to have this time counting down in my cl_init.lua:


draw.WordBox(2, -TextWidth*0.5, -30, explodeTime, "HUDNumber5", Color(140, 0, 0, 100), Color(255,255,255,255))

Thank you.

draw.WordBox(2, -TextWidth*0.5, -30, explodeTime…" lol", “HUDNumber5”, Color(140, 0, 0, 100), Color(255,255,255,255))
If I know enough about lua it should say

<TIME> lol

Bump

This may work. Read the comments.
[lua]
timer.Simple(explodeTime, function() ENT:Explode() end) – Comma added.

–This would go in the HUDPaint hook that draw.WordBox is in.
local time = explodeTime
timer.Create(“CountDown”, 1, explodeTime, function()
time = time - 1
end)
draw.WordBox(2, -TextWidth*0.5, -30, time, “HUDNumber5”, Color(140, 0, 0, 100), Color(255,255,255,255))
[/lua]

Will that work in the init.lua?
Doesn’t HUD need to be in cl_init?

No, that wouldn’t work in a serverside file. This all needs to be clientside. For instance. If all you have is the code I posted above it would be:
[lua]
hook.Add(“HUDPaint”, “DrawCountdown”, function()
local time = explodeTime
timer.Create(“CountDown”, 1, explodeTime, function()
time = time - 1
end)
draw.WordBox(2, -TextWidth*0.5, -30, time, “HUDNumber5”, Color(140, 0, 0, 100), Color(255,255,255,255))
end)
[/lua]

How would I call on the explode function that is in init.lua then?

The explode function would stay there. This is what you should have.

cl_init.lua
[lua]
hook.Add(“HUDPaint”, “DrawCountdown”, function()
local time = explodeTime
timer.Create(“CountDown”, 1, explodeTime, function()
time = time - 1
end)
draw.WordBox(2, -TextWidth*0.5, -30, time, “HUDNumber5”, Color(140, 0, 0, 100), Color(255,255,255,255))
end)
[/lua]

init.lua
[lua]
timer.Simple(explodeTime, function() ENT:Explode() end) – Comma added.
[/lua]
You never read my comments.


local CountDownTime = false 
function SetCountDownTime(seconds) 
    CountDownTime = CurTime()+seconds 
end 

hook.Add("HUDPaint", "CountDownTimer", function(w, h) 
    if CountDownTime then 
        local Text = tostring(CountDownTime-CurTime())
        surface.SetFont("HUDNumber5") 
        local tw = surface.GetTextSize(Text) 
        draw.WordBox(2, w/2-tw/2, 30, Text, "HUDNumber5", Color(140, 0, 0, 100), Color(255,255,255,255)) 

        if CurTime() >= CountDownTime then 
            -- Explode da bomb 
        end 
    end 
end ) 

I’ll let you figure out how to set the time and stuff from here.

Thank you for your help.

This is what it looks like:
init.lua:



function ENT:Use(ply)
	timer.Simple(explodeTime, function() ENT:Explode() end) -- Comma added.
end


cl_init.lua:



include("shared.lua")

function ENT:Initialize()
end

function ENT:Draw()
	self:DrawModel()
	self:SetColor(Color(71, 67, 69,255))
	
	local Pos = self:GetPos()
	local Ang = self:GetAngles()

	surface.SetFont("HUDNumber5")
	local TextWidth = surface.GetTextSize(explodeTime)

	Ang:RotateAroundAxis(Ang:Up(), 90)

hook.Add("HUDPaint", "DrawCountdown", function()
	local time = explodeTime
	timer.Create("CountDown", 1, explodeTime, function()
		time = time - 1
	end)
	draw.WordBox(2, -TextWidth*0.5, -30, time, "HUDNumber5", Color(140, 0, 0, 100), Color(255,255,255,255))
end)
end

function ENT:Think()
end


Error:
[ERROR] cl_init.lua:23: bad argument #3 to ‘Create’ (number expected, got nil)

Oh. You’re doing this an in ENT. (derp)
Replace this
[lua]
function ENT:Draw()
self:DrawModel()
self:SetColor(Color(71, 67, 69,255))

local Pos = self:GetPos()
local Ang = self:GetAngles()

surface.SetFont("HUDNumber5")
local TextWidth = surface.GetTextSize(explodeTime)

Ang:RotateAroundAxis(Ang:Up(), 90)

hook.Add(“HUDPaint”, “DrawCountdown”, function()
local time = explodeTime
timer.Create(“CountDown”, 1, explodeTime, function()
time = time - 1
end)
draw.WordBox(2, -TextWidth*0.5, -30, time, “HUDNumber5”, Color(140, 0, 0, 100), Color(255,255,255,255))
end)
end
[/lua]
with this:
[lua]
function ENT:Draw()
self:DrawModel()
self:SetColor(Color(71, 67, 69,255))

local Pos = self:GetPos()
local Ang = self:GetAngles()

surface.SetFont("HUDNumber5")
local TextWidth = surface.GetTextSize(explodeTime)

Ang:RotateAroundAxis(Ang:Up(), 90)


local time = explodeTime
timer.Create("CountDown", 1, explodeTime, function()
	time = time - 1
end)
draw.WordBox(2, -TextWidth*0.5, -30, time, "HUDNumber5", Color(140, 0, 0, 100), Color(255,255,255,255))

end
[/lua]

Thank you, however, it’s still spamming this:
timer.Create(“CountDown”, 1, explodeTime, function()

and also bad argument to GetTextSize(explodeTime).

-snip- Agreeing with guy below me

I think now it’s time for you to figure the rest out on your own.

I tried to add timer.Start(“CountDown”) in the ENT:Use() in init.lua, then I tried to change it to a simple timer(just to see if that would work) which made it count down super fast. Can I get a clue on what to do?

[editline]5th September 2013[/editline]

Okay, it starts to count down after it explodes now.

[editline]5th September 2013[/editline]

I just can’t figure this out. I’ve tried everything I can think of, cl_init.lua doesn’t read explodeTime from init.lua!

Finally fixed it with SetNWInt! Thank you for your help.

Stop creating timers in :Draw()