• timer.Simple format
    3 replies, posted
timer.Simple( number delay, function func ) [url]http://wiki.garrysmod.com/page/Libraries/timer/Simple[/url] Hi, Since GMod13 happened, timer functions don't seem to work right. Garry removed the 3rd optional argument, but even when you only use 2 arguments, timers still give an error. [ERROR] addons/gdcw/lua/weapons/gdcw_rpg7/shared.lua:80: bad argument #2 to 'Simple' (function expected, got no value) 1. Simple - [C]:-1 2. unknown - addons/gdcw/lua/weapons/gdcw_rpg7/shared.lua:80 This is the code that caused the error; timer.Simple( 2, self:Reload() ) It's running in a SWEP base. When the timer function is called, it should delay 2 seconds and then activate the reload function, right? But it doesn't wait at all, and it gives an error. Am I just doing something really dumb or is the timer function broken on Garry's side?
[QUOTE=Generic Default;38531331]timer.Simple( number delay, function func ) [url]http://wiki.garrysmod.com/page/Libraries/timer/Simple[/url] Hi, Since GMod13 happened, timer functions don't seem to work right. Garry removed the 3rd optional argument, but even when you only use 2 arguments, timers still give an error. [ERROR] addons/gdcw/lua/weapons/gdcw_rpg7/shared.lua:80: bad argument #2 to 'Simple' (function expected, got no value) 1. Simple - [C]:-1 2. unknown - addons/gdcw/lua/weapons/gdcw_rpg7/shared.lua:80 This is the code that caused the error; timer.Simple( 2, self:Reload() ) It's running in a SWEP base. When the timer function is called, it should delay 2 seconds and then activate the reload function, right? But it doesn't wait at all, and it gives an error. Am I just doing something really dumb or is the timer function broken on Garry's side?[/QUOTE] This is how do you it : [code]timer.Simple( 2, self.Reload )[/code] This is because you have to pass the timer a function while you were executing that function and passing whatever it returned (in this case nil).
Previously you'd have to use [lua]timer.Simple(2, self.Reload, self)[/lua] but since GM13's timer.Simple doesn't take varargs anymore, that won't work. Use a local function instead, although bear in mind the former's dramatically more efficient if you're doing it a lot: [lua]local function reload() self:Reload() end timer.Simple(2, reload) timer.Simple(2, function() self:Reload() end)[/lua] As it stands you're [i]calling[/i] the reload function and timer.Simple gets its return value instead, which is nil.
Instead of args just do [lua]timer.Simple(delay, function() (do stuff here) end)[/lua]
Sorry, you need to Log In to post a reply to this thread.