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

Timers and file copying through a program

Started by Bossman201, 31 May 2012 - 12:28 AM
Bossman201 #1
Posted 31 May 2012 - 02:28 AM
That's everything. Please just dump info in this post, I can figure it out from there or ask questions if I don't understand.

Timers:
How to set one
How to check their event
When I do os.pullEvent("timer") it returns a table value, what is this and how do I use it?

File Copying: Just…just give me everything.
MysticT #2
Posted 31 May 2012 - 02:39 AM
Timers:
You start a timer with os.startTimer(<time>), being <time> the time in seconds you want for the timer. It returns an empty table used to identify the timer, then you have to wait for the "timer" event, and check if it's argument the same table you got from os.startTimer().
Example:

local timer = os.startTimer(5) -- start a 5 seconds timer
while true do -- infinite loop
  local evt, arg = os.pullEvent("timer") -- get a "timer" event
  if arg == timer then -- if it's the timer we started
    -- do something
  end
end

File copying:
There's a function that does this:
fs.copy(<src>, <dest>)
being <src> the path to the source file/directory and <dest> the destination path. (Yes, it copies entire directories too)
Example:

fs.copy("/bin/edit", "edit") -- copy the edit program to the root
Bossman201 #3
Posted 31 May 2012 - 02:49 AM
So for timers, we check to see if it's the same timer that we previously declared?

Say I wanted to have multiple timers, the code would look something like this?


local five = os.startTimer(5) -- start a 5 seconds timer
local one = os.startTimer(1)
while true do -- infinite loop
  local evt, arg = os.pullEvent("timer")
  if arg == five then
    print("The 5 second timer has expired!")
  elseif arg == one then
    print("The 1 secon timer has expired!")
  end
end

And for file copying, I wanted to add a file transfer to my program (to make future testing a simpler process). I saw a post maybe a week ago that had code that would read the file line by line and send it line by line, then the receiving computer would put it all back together, but I can't find it.
MysticT #4
Posted 31 May 2012 - 02:59 AM
So for timers, we check to see if it's the same timer that we previously declared?

Say I wanted to have multiple timers, the code would look something like this?


local five = os.startTimer(5) -- start a 5 seconds timer
local one = os.startTimer(1)
while true do -- infinite loop
  local evt, arg = os.pullEvent("timer")
  if arg == five then
	print("The 5 second timer has expired!")
  elseif arg == one then
	print("The 1 secon timer has expired!")
  end
end
Yes, that would work. You can have all the timers you want, and then check wich one fired the event and do somethnig for that, then keep checking for the rest.

And for file copying, I wanted to add a file transfer to my program (to make future testing a simpler process). I saw a post maybe a week ago that had code that would read the file line by line and send it line by line, then the receiving computer would put it all back together, but I can't find it.
If you want to send the file over rednet, you can open the file and read it's contents, then send the string with the whole file contents:

local file = fs.open("path/to/file", "r")
if file then
  local contents = file.readAll()
  file.close()
  rednet.send(id, contents)
end
Bossman201 #5
Posted 31 May 2012 - 03:36 AM
Wow I didn't know you could just send the entire file so simply.
Bossman201 #6
Posted 31 May 2012 - 08:12 PM
Okay so I don't have any new directories. How would I open file "test" using fs.open()?
MysticT #7
Posted 31 May 2012 - 08:16 PM

fs.open("test", "r")
Take a look at the fs api on the wiki or the in-game help to know how it works.