• timer.Simple, broken?
    8 replies, posted
I'm either doing something completely wrong or the wiki entry is broken I just got into coding a few hours ago, and I'm toying around with the most simple of commands trying to make different bits of code, and I decided to try out the timer, but I can't get it to work. It doesn't time, it just displays the message as soon as it can, instead of waiting 5 (or whatever delay is set) seconds. [PHP]Msg("Hello World! \n") myname="Ben" Msg(myname .. "\n") Msg("Hello, "..myname..". How are you today?\n") timer.Simple(5, Msg("This message will display after 5 seconds!"))[/PHP]So then i tried copying the code directly from the wiki page on it: [URL]http://wiki.garrysmod.com/?title=Timer.Simple[/URL] but, that wasn't correct either, the sample says [PHP]timer.Simple(5, Msg, "This message will display after 5 seconds!")[/PHP]The message doesn't work in the sample. I've also tried setting the delay to 5000 in case it was measured in milliseconds, it still printed the text to the console instantly. So, Lua Gurus, what am I doing wrong here?
The wiki example seems to work fine. Where are you running the code? Are you on singleplayer or multiplayer?
Singleplayer.
The timer will only run in singleplayer while you are not in the ESC-menu, could that be the problem?
Wait now it's working I'm having trouble making it repeat though [php]local Counter = 100 repeat timer.Simple(5, Msg, "This message will display after 5 seconds!\n") Counter = Counter-1 until(Counter==1)[/php] it just waits 5 seconds, and then prints the message 100 times. what's wrong here? [editline]04:33AM[/editline] [QUOTE=DarKSunrise;24056031]The timer will only run in singleplayer while you are not in the ESC-menu, could that be the problem?[/QUOTE] Yeah I'm pretty sure I just forgot to unpause it :v: but then again that wouldn't cause it to display the message instantly, right?
You are basically telling it to print it 100 times after five seconds have passed. If you want to print it 100 times every five seconds, you need to use timer.Create. [lua]timer.Create("Some unique name for the timer", 5, 100, MsgN, "Test")[/lua] MsgN is the same as Msg, but it automatically adds "\n" to the end.
I was actually hoping to get something to print once every 5 seconds, would i just use this? [php]timer.Create("Some unique name for the timer", 5, 1, MsgN, "Test" )[/php]also could I use this for functions? like this: [php]timer.Create("Some unique name for the timer", 5, 1, myFunction)[/php]also, thanks for that MsgN tip (: Nevermind, after toying around with it I found out that your example was actually what I wanted I can't get it to work with a function though [php]local Counter = 100 repeat timer.Create("Timer1", 5, 100, sayings() ) Counter = Counter-1 until(Counter==1)[/php]It just runs the sayings function 100 times instantly and then 100 times after that it shows "Timer Error: attempt to call a nil value" every 5 seconds. Wait why am i using a repeat command? Okay I got rid of the repeat command, and now it runs my "sayings" function once, and then gives a timer error every 5 seconds. Maybe I shouldn't be learning Lua at 5am :v: Okay I looked at the Wiki (like i should've :3) and then i found out I was supposed to put the function inbetween the timer and the timers end. so here's my finished code. doesn't server a purpose, I just wanted to see if I could make something annoying. [php]timer.Create("Timer1", 3, 0, function() number=math.random(1,37) if number == 1 then Msg("Frostbite Canyon\n") elseif number == 2 then Msg("Rinsecloud Iceveins\n") elseif number == 3 then Msg("Washboard Righter Vonsugarpill\n") elseif number == 4 then Msg("Tom Araya\n") elseif number == 5 then Msg("Kerry King\n") elseif number == 6 then Msg("Jeff Hanneman\n") elseif number == 7 then Msg("Drummer of Slayer\n") elseif number == 8 then Msg("Jeremy Mckinnon\n") elseif number == 9 then Msg("Lydia Thomson\n") elseif number == 10 then Msg("Roy Mayorga\n") elseif number == 11 then Msg("James Michael Root\n") elseif number == 12 then Msg("Chris Elliot\n") elseif number == 13 then Msg("Jim Morrison\n") elseif number == 14 then Msg("Rick Moranis\n") elseif number == 15 then Msg("Vinny Paul\n") elseif number == 16 then Msg("Metal Mike\n") elseif number == 17 then Msg("Hebrew Jimi\n") elseif number == 18 then Msg("Reign Reign Go Away\n") elseif number == 19 then Msg("Shura 9x000\n") elseif number == 20 then Msg("Bowel Movement\n") elseif number == 21 then Msg("When the Jizz Hits The Fan\n") elseif number == 22 then Msg("Jeremiah the Urban Fecal Horder\n") elseif number == 23 then Msg("Antis Gilamigo\n") elseif number == 24 then Msg("Siesta Fineass\n") elseif number == 25 then Msg("Sultan Anticuous Shrubcutter\n") elseif number == 26 then Msg("Righthand Feral Sunkiss\n") elseif number == 27 then Msg("Schlep Funstack\n") elseif number == 28 then Msg("Erectus Lampikatius\n") elseif number == 29 then Msg("Gorland Napalmeranian\n") elseif number == 30 then Msg("Rathepon Ourselves\n") elseif number == 31 then Msg("Arbison Skyflake\n") elseif number == 32 then Msg("Holly Longride\n") elseif number == 33 then Msg("Venetian Clitoris\n") elseif number == 34 then Msg("Antagonistic\n") elseif number == 35 then Msg("Jimi Hertrod\n") elseif number == 36 then Msg("Corporal Seedweather\n") else Msg("James' Ambiguous Rectalfire\n") end end )[/php] thanks for your help DarKSunrise! also is there a better way to do my code, besides the massive if then else statement? I googled it and I read somwhere that Lua doesn't have the visual basic equivalent of a case statement. is that true? and if so, are there any less 'messy' alternatives?
You could do something like this: [lua]local sayings = { "Lorem ipsum dolor sit amet", "Another sentence", "And third" }; local function myfunction() MsgN(table.Random(sayings)); end timer.Create("My timer", 5, 0, myfunction);[/lua] Or this. It's actually a better way unless you need to call the function elsewhere as well: [lua]local sayings = { "Lorem ipsum dolor sit amet", "Another sentence", "And third" }; timer.Create("My timer", 5, 0, function() MsgN(table.Random(sayings)); end);[/lua] [QUOTE=Hell Strike;24056117]I googled it and I read somwhere that Lua doesn't have the visual basic equivalent of a case statement. is that true? and if so, are there any less 'messy' alternatives?[/QUOTE] That's true. It always depends on the situation, but if you need to print a random message it would be better to get a random value from a table. If you need to do something more complex, you'll probably have to use if/else-statements.
Thank you! You've been so helpful.:keke:
Sorry, you need to Log In to post a reply to this thread.