This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
drdoom30's profile picture

Time running

Started by drdoom30, 28 May 2012 - 11:22 PM
drdoom30 #1
Posted 29 May 2012 - 01:22 AM
How would I make a time running program? This is what I have so faer

os.startTimer(1) – start5 second timer
X = 0
local, event, p1 == os.pullEvent("timer") – wait for a timer event
if p1 == "timer" then – check to make sure the timer went off
x = x+1
end
Noodle #2
Posted 29 May 2012 - 05:30 AM
I don't think you can pull an event, set a variable for the timer then do an if command.
Example:
v = os.startTimer(1)
x = 0
if v == 0 then
x = x + 1
end

I'm not sure on this but when you find out please tell me because i think this could be useful.
Luanub #3
Posted 29 May 2012 - 05:45 AM
I don't think you can pull an event, set a variable for the timer then do an if command.
Example:
v = os.startTimer(1)
x = 0
if v == 0 then
x = x + 1
end

I'm not sure on this but when you find out please tell me because i think this could be useful.

wrong you can pull event. You will want to set it to 5 instead of 1 and the first x should be lowercase since lua is case sensitive. What you have should work


os.startTimer(5) -- changed to 5, this works the same as a sleep, enter the number of seconds you want to the timer to run for.
local x = 0 -- set as local and changed to lowercase to match the math below
local, event, p1 == os.pullEvent("timer") -- wait for a timer event
if p1 == "timer" then -- check to make sure the timer went off
x = x+1
end
Edited on 29 May 2012 - 03:53 AM
my_hat_stinks #4
Posted 29 May 2012 - 08:27 AM
Line 3, you have a comma immediately after local

Line 3, you're using "==" (Comparison) rather than "=" (Assignment)

Line 4, your "p1" should be "event" (You're checking the event, not the arguments)
I think it's also unnecessary, passing "timer" to os.pullEvent should filter it properly
Luanub #5
Posted 29 May 2012 - 08:58 AM
the p1 var will also capture "timer" when the timer goes off, most people check against it to ensure they got the right event. Also having it filter timer will help prevent key strokes and other events from setting it off, hopefully preventing a timer event being missed. Without the filter you would want your if statement to look like


if event == "timer" and p1 == "timer" then
my_hat_stinks #6
Posted 29 May 2012 - 09:10 AM
write( "n"..tostring(e).." - "..tostring(p1) )



Not sure, but that might a table, not a string :)/>/>
Cloudy #7
Posted 29 May 2012 - 09:23 AM
The timer returned by os.startTimer is just an empty table - it is done this way as a table is guaranteed to be unique. You will just need to check that param1 returned by os.pullEvent is the same table object.