• timer.Simple and printing help?
    5 replies, posted
I am extremely new to this (started maybe 2 hours ago). All I have been trying to do is create an autorun script that will print a message every 5 seconds. I have seen FROM THE WIKI that you can do "timer.Simple( 5, function() print( "Hello World" ) end )" and I have been able to print "Hello World" after 5 seconds, but, I have tried: [code] while true do timer.Simple( 5, function() print( "Hello World" ) end ) end[/code] and whenever I start a single player game (with all addons disabled except mine), the game tries to load but stops at "Loading Resources..." I have also tried: [code] for i = 1, 50 do timer.Simple( 5, function() print( "Hello World" ) end ) end[/code] but in this case, the script waits 5 seconds, then immediately prints all 50 "Hello World"'s at the same time. Can anyone help a LUA noob out?
[lua]timer.Create( "hello timer", 5, 0, function( ) print( "hello, world.." ); end );[/lua] "hello timer" = unique timer name 5 = seconds to wait 0 = times this timer should run (0 means forever)
timer.Simple is not accurate at all, nor are any timers, if my memory serves me properly. It is more accurate to use a think function and compare the value of "CurTime()" Standard timers will never be accurate in lag, however even this can fall to lag, especially extreme lag, so I would also use a boolean variable to check to see if it has been called. [lua]if (CurTime()>StartTime+5) && !SaidHello) then print( "Hello, world!" ) SaidHello = true end[/lua] [editline]25th May 2013[/editline] I suppose, though, if you don't need it to be exact then it does not matter.
[QUOTE=luavirusfree;40779909]timer.Simple is not accurate at all, nor are any timers, if my memory serves me properly. It is more accurate to use a think function and compare the value of "CurTime()"[/QUOTE] Good job describing exactly how the timer module works.. [url]http://glua.me/bin/?path=/lua/includes/modules/timer.lua[/url]
Since you are new to Garry's Mod Lua I'll explain to you why this happened. When you create a timer (in this case: timer.Simple), the code will not halt and wait for the timer to complete. This is done by design. Otherwise, you would end up with freezing code while you wait for your 60 second timer to complete. Because of this, the code is iterating 50 times, each time creating the timer. This is quite fast so it may appear to print 50 times after 5 seconds because all the timers were created quickly after each other. The code Banana Lord posted is what you should use instead: [LUA] timer.Create( "hello timer", 5, 50, function( ) print( "hello, world.." ); end ); [/LUA] This time it's just the one timer that repeats the amount of times you want. timer.Simple is mainly for delays as it only runs once and timer.Create is if you want the code to run again a few times or even forever (0). Hopefully you will now understand why this happened.
Thank you all! Banana Lord's script was exactly what I needed, and thanks for the explanation Bo98!
Sorry, you need to Log In to post a reply to this thread.