I also don't know a lot about lua so excuse me if I don't understand something.
Greetings
function functionThatsRun()
--code you want to repeat here
print("it has been 60 seconds")
end
while true do
functionThatsRun()
sleep(60)
end
try learning a bit more the reference manual and PIL are great but here's a piece of code you can usefunction functionThatsRun() --code you want to repeat here print("it has been 60 seconds") end while true do functionThatsRun() sleep(60) end
function functionThatsRun()
-- code you want to repeat here
print("it has been 60 seconds")
end
function functionThatKeepsRunning()
-- Your code
print("I'm also running")
end
local function F1()
while true do
functionThatsRun()
sleep(60)
end
end
parallel.waitForAll(F1, functionThatKeepsRunning)
I believe you meantAnd if you want to also run something else at the same time, you can do something like this:function functionThatsRun() --code you want to repeat here print("it has been 60 seconds") end function functionThatKeepsRunning() -- Your code print("I'm also running") end local function F1() while true do functionThatsRun() sleep(60) end end parallel.waitForAll(F1, functionThatRuns)
function functionThatsRun()
--# code you want to repeat here
print("it has been 60 seconds")
end
function functionThatKeepsRunning()
--# Your code
print("I'm also running")
end
local function F1()
while true do
functionThatsRun()
sleep(60)
end
end
parallel.waitForAll(F1, functionThatKeepsRunning)
And if you want to also run something else at the same time, you can do something like this:try learning a bit more the reference manual and PIL are great but here's a piece of code you can usefunction functionThatsRun() --code you want to repeat here print("it has been 60 seconds") end while true do functionThatsRun() sleep(60) end
function functionThatsRun() --code you want to repeat here print("it has been 60 seconds") end function functionThatKeepsRunning() -- Your code print("I'm also running") end local function F1() while true do functionThatsRun() sleep(60) end end parallel.waitForAll(F1, functionThatRuns)
while true do
function_you_want_to_repeat()
sleep(60)
end
It was, several times. Here in Ask a Pro we aim to provide high quality answers, in order to this it is always recommended to read all subsequent replies in a topic as quite often the OP will add more information and/or clarify their needs further, just jumping directly to the end and replying is both disrespectful to the OP and anyone else that has already replied to the topic, in the future please make sure to read the replies in a topic before even considering typing up your answer.Scrolled to bottom immediately, so sorry if this was already answered.while true do function_you_want_to_repeat() sleep(60) end
Did you even read the post above yours?!-snip-
function func()
--code
timer = os.startTimer(60)
end
func()
while true do
event = os.pullEvent()
if event == "timer" then
func()
end
end
you can also use os.startTimer if you want the computer to "multitask" without using parallel…
local function func()
--code
end
local period = 60
local timer = os.startTimer(period)
while true do
local event, p1 = os.pullEvent()
if event == "timer" and p1 == timer then
func()
timer = os.startTimer(period)
end
end
The one of the problems with global naming is the potential of being run by another program that uses the same global variable. The variable can change to an unexpected value and even an unexpected value type, and can cause errors through conflict. It can be fair to assume that any programs you're using are created by you and are run under your systems, but keeping track of variable names to avoid conflict can become a massive headache.#1. ofc, but none of my naming is used globaly
#2[1]. you can, but in your case you need to wait 1 min for the program to start
#2[2]. you can do that, but the OP asked about 1 min
#3. ??? param is used if you're running multiple timers
However I really do agree with you, I want to state something that is not always seen.The one of the problems with global naming is the potential of being run by another program that uses the same global variable. The variable can change to an unexpected value and even an unexpected value type, and can cause errors through conflict. It can be fair to assume that any programs you're using are created by you and are run under your systems, but keeping track of variable names to avoid conflict can become a massive headache.
function shell.run( sPath, ... )
local sPath = ( type( sPath ) == "string" and sPath or error( "String expected, got " .. type( sPath ), 2 )
if fs.exists( sPath ) then
local file = fs.open( sPath, "r" )
local content = file.readAll()
file.close()
local func, compileError = loadstring( content, fs.getName( sPath ) )
if func then
setfenv( func, getfenv(3) )
local ok, runtimeError = pcall( func, ... )
if not ok then
error( runtimeError, 2 )
end
return true
else
error( compileError, 2 )
end
else
error( "File not found", 0 )
end
end
local function run( _sCommand, ... )
local sPath = shell.resolveProgram( _sCommand )
if sPath ~= nil then
tProgramStack[#tProgramStack + 1] = sPath
local result = os.run( tEnv, sPath, ... )
tProgramStack[#tProgramStack] = nil
return result
else
printError( "No such program" )
return false
end
end
local function runLine( _sLine )
local tWords = {}
for match in string.gmatch( _sLine, "[^ \t]+" ) do
table.insert( tWords, match )
end
local sCommand = tWords[1]
if sCommand then
return run( sCommand, unpack( tWords, 2 ) )
end
return false
end
function shell.run( ... )
return runLine( table.concat( { ... }, " " ) )
end
Okay, I really did miss how it should look. But okay, as you can see, it still uses one environment :P/>/>/>If you arent using local, your variables are "global". See above why I put that into quotes.#1. ofc, but none of my naming is used globaly
You should always add the second check. What if other programs, or functions, queues timers and they are ran parallel wih your program? Then I can say, you are getting irreliable events.#3. ??? param is used if you're running multiple timers
In all cases.It's almost always better to use local variables as to not pollute the global namespace. It can be speedier in some cases as well.
It is good programming style to use local variables whenever possible. Local variables help you avoid cluttering the global environment with unnecessary names. Moreover, the access to local variables is faster than to global ones.
Reference: http://www.lua.org/pil/4.2.html
Given that large number of registers, the Lua precompiler is able to store all local variables in registers. The result is that access to local variables is very fast in Lua. For instance, if a and b are local variables, a Lua statement like a = a + b generates one single instruction: ADD 0 0 1 (assuming that a and b are in registers 0 and 1, respectively). For comparison, if both a and b were globals, the code for that addition would be like this:GETGLOBAL 0 0 ; a GETGLOBAL 1 1 ; b ADD 0 0 1 SETGLOBAL 0 0 ; a
So, it is easy to justify one of the most important rules to improve the performance of Lua programs: use locals!
Reference: http://www.lua.org/gems/sample.pdf (p.17)