Variable Name in Timer Callback

Why can’t I pass a variable name into a timer callback?



	local timer_name = "MyTimer"
	print(timer_name) -- exists
	timer.Create( timer_name, 1, 10, function( timer_name )
		print(timer_name) -- doesn't exist - this prints nil
                timer.RepsLeft(timer_name) -- this errors with string expected got nil
        end )


I assumed that timer_name in the function wouldn’t work - as the function arguments would be in the timer.Create function if there were any - but is there any way to do this?

[editline]15th January 2017[/editline]

Note; I see on the wiki you can do timer.Create( timer_name, 1, 10, function() MyFunction(variable) end ) however is it possible to pass a variable into the inline anonymous function?

[editline]15th January 2017[/editline]

Note again:

I’ve just seen on the old wiki you can do:



timer.Create( "TimerName", 1, 1, function() end, variable )


This isn’t noted on the new wiki, but does still work.

https://wiki.garrysmod.com/page/timer/Create - 4 args to timer.Create
https://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/index9643.html - 5 args, includes var arguments as 5th.

Might be worth adding if someone sees this that can.

[editline]15th January 2017[/editline]

Ok, I’ve edited it and added varargs as parameter, didn’t know we could do that.

Can you show an example of where this works? The old wiki is outdated and as such many of the function definitions are probably incorrect.

I’ve just tested



timer.Create("lol", 1, 1, function(...) print("ayy", ...) end, 1, 2, 3)


and the only thing that gets printed to console is “ayy”

This was my initial code, and timer_name in the callback was printing as nil



	local timer_name = "MyTimer"
	print(timer_name) -- exists
	timer.Create( timer_name, 1, 10, function( timer_name )
		print(timer_name) -- doesn't exist - this prints nil
                timer.RepsLeft(timer_name) -- this errors with string expected got nil
        end )


I then changed it to this:



	local timer_name = "MyTimer"
	print(timer_name) -- exists
	timer.Create( timer_name, 1, 10, function( timer_name )
		print(timer_name) -- exists - prints "MyTimer"
                timer.RepsLeft(timer_name) -- works as expected
        end, timer_name )


And it then worked fine - I have actually just assumed that the 5th argument of timer.Create takes varargs however haven’t tested (will do in a minute and update wiki if needed) although it would make no sense if it only took one.

[editline]15th January 2017[/editline]

Try this:

the callback anonymous function() takes no arguments, as these would have to have been specified in the timer.Create function itself. Also, as far as I’m aware you can’t just put … in and let it print the varargs, as it would be a table.



timer.Create("lol", 1, 1, function()
    PrintTable({...})
end, "one", 2, "three")


[editline]15th January 2017[/editline]

Ok, well I’ve just tested this on command line and it prints fine :confused:



local four = "four" local five = "five" local six = "six" timer.Create("lol2", 1, 1, function() print(four) print(five) print(six) end )


Did you not see this at the top of that website?

Funnily enough - I did - however the current gmod wiki is not perfect and has some missing information still.

I may have got this wrong, and would appreciate someone confirming, however I’m not stupid.

And of course ran my own test to check this first - I’m not just blindly moving information over from the old wiki - and if you’d like to help, rather then just criticising, maybe you could do your own tests and give some input.

Okay, well you can’t use varargs in timer.Create
[lua]timer.Create(“lol”, 1, 1, function(…)
PrintTable({…})
end, “one”, 2, “three”)[/lua]

Scopes, there’s no reason to even pass it.

Well thanks - that would have more a better test - shouldn’t have just assumed - however it still doesn’t explain why in my original post timer_name wasn’t available in the callback function. I never really use varargs - and realise now that accessing like I have isn’t right anyway, but still confused as to why it works now when it didn’t before.

[editline]15th January 2017[/editline]

Well exactly, that’s what I thought - however again, still doesn’t explain the original post and why it was coming out as nil. The reason why I jumped to the conclusion on varargs is because when I used sql callback functions I know that as they took a second to complete, the parent function was ending before the callback was finished, and then the local variables were no longer available. So thought something similar happened here.


function( timer_name )

is your problem

You are shadowing the timer_name variable by creating a new variable with that same name as the parameter to the callback.

Although that’s in my example above, my original test was done without that as a parameter in the callback. Let me try again now.

[editline]15th January 2017[/editline]

Ok well my bad, it’s working now - must have just screwed up somehow and jumped to a conclusion when it started working - easily done. I guess that’s what you get for not taking breaks.

Thanks for the help.