Help: timer.Simple?

When I use this it doesn’t wait 5 seconds at all. What am I doing wrong?


timer.Simple( 5, function() a[0] = a[1] end)
timer.Simple( 5, function() a[1] = a[2] end)

EDIT: I’m trying to assign a new value to table ‘a’.

Well, you could, for instance, show us your whole code so we can put it into context and point out your obvious flaws.

I’m only trying to assign a variable within a timer. Is there a better way?

where is the full code, if that is all you have so far it’s going to throw errors back

Well for one, I don’t see why you’re splitting your operations into 2 functions. There’s not much more to say.

So far we only know that what you’re doing “doesn’t work”, but there’s nothing from about the 2 lines in the OP apart from the variable ‘a’ that is missing, but I suppose you defined it in your full code which is again why we need to see your full code.

It’s for a custom chatbox. All it’s doing is sliding the variable from different “slots” on the screen. I’m using a PaintHUD hook for surface.drawtext. I only want to know is how to redefine a table within a timer.Simple() function.



local i = 0
a = {}
a[0] = ""
a[1] = ""
a[2] = ""

a[3] = "player died" -- Test

while i > 1 do

timer.Simple( 5, function() a[0] = a[1] end)
timer.Simple( 5, function() a[1] = a[2] end)
timer.Simple( 5, function() a[2] = a[3] end)

end

You’re running a loop based on the condition if the variable “i” is bigger than 1, which it isn’t because you just set it to 0.

This is why you always post the full code…

The code isn’t the problem. I know that it’s a endless loop. I’m only trying to define a table within the timer.Simple() function.

It isn’t an endless loop… the loop never runs because i is never greater than 1.

Ok, that’s my bad.


local i = 0
a = {}
a[0] = ""
a[1] = ""
a[2] = ""

a[3] = "player died" -- Test

while i < 1 do

timer.Simple( 5, function() a[0] = a[1] end)
timer.Simple( 5, function() a[1] = a[2] end)
timer.Simple( 5, function() a[2] = a[3] end)

end

What is actually happening? Are the timers running at all? How are you checking if they are running?

Basically, it displays a text on the screen.


local IsAlive = 1
a = {}
a[0] = ""
a[1] = ""
a[2] = ""
a[3] = ""
a[4] = ""
a[5] = ""

local i = 0
local x = (ScrW() / 2) - 500
local y = (ScrH() / 2) + 200

function UpdateList()

	if !LocalPlayer():Alive() and IsAlive == 1 then

		IsAlive = 0
		a[5] = "player died"

	elseif LocalPlayer():Alive() then

		IsAlive = 1

	end


	timer.Simple( 5, function() a[0] = a[1] end)
	timer.Simple( 5, function() a[1] = a[2] end)
	timer.Simple( 5, function() a[2] = a[3] end)
	timer.Simple( 5, function() a[3] = a[4] end)
	timer.Simple( 5, function() a[4] = a[5] end)
	timer.Simple( 5, function() a[5] = "" end)
end

hook.Add( "HUDPaint", "Display_custom", function()
    	draw.DrawText( a[5], "Trebuchet24", x, y, Color( 255, 100, 100, 255 ), 1 )
	draw.DrawText( a[4], "Trebuchet24", x, y+20, Color( 255, 100, 100, 255 ), 1 )
	draw.DrawText( a[3], "Trebuchet24", x, y+40, Color( 255, 100, 100, 255 ), 1 )
	draw.DrawText( a[2], "Trebuchet24", x, y+60, Color( 255, 100, 100, 255 ), 1 )
	draw.DrawText( a[1], "Trebuchet24", x, y+80, Color( 255, 100, 100, 255 ), 1 )
	draw.DrawText( a[0], "Trebuchet24", x, y+100, Color( 255, 100, 100, 255 ), 1 )
end)

hook.Add( "Think", "AddList",UpdateList)

my eyes

I’d take a screenshot, but the text won’t stay more than half a second.

First of all you’re failing to localize the function “UpdateList” and the table “a”.

Second, your logic is terribly flawed. Every “Think” you’re creating 6 timers that last 5 seconds each (something like a hundred times a second-ish).

So basically, when 5 seconds has passed from that script being run, you’ll have 6 timer functions running a hundred-ish times per second.

Alright, thanks. I see what you mean.

Use this instead http://wiki.garrysmod.com/page/timer/Create
You can have a 5 second wait that loops endlessly.