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

parallel help

Started by etopsirhc, 05 March 2013 - 12:30 PM
etopsirhc #1
Posted 05 March 2013 - 01:30 PM
can parallel.waitForAny call functions that sleeps for 5 min while the other waits for an event to trigger ?
theoriginalbit #2
Posted 05 March 2013 - 01:39 PM
yes it can


local function listener()
  local event = { os.pullEvent() }
  -- do something
end

local function sleeper()
  while true do
    sleep(300)
    -- do something
  end
end

parallel.waitForAny(sleeper, listener)
etopsirhc #3
Posted 05 March 2013 - 01:45 PM
its kinda messy but heres what mine looks like ( and it isnt working )

http://pastebin.com/L6uf6PiD

i can get the program to run , but when after parallel startes it doesnt seem to get the event
theoriginalbit #4
Posted 05 March 2013 - 01:53 PM
what is meant to be causing the player event? is it a peripheral?
etopsirhc #5
Posted 05 March 2013 - 01:57 PM
its the player detector from misc peripherals
theoriginalbit #6
Posted 05 March 2013 - 02:00 PM
ahh ok.

so if I'm understanding correctly, this program is meant to turn the mob farm on when a player arrives, and then turn off after 5 minutes? or when the player leaves?
etopsirhc #7
Posted 05 March 2013 - 02:02 PM
pretty much yes , player activates the detector , it turns on the farm for 5 min , but the player can then turn it off by hitting the detector again.
the hitting it again part ( and posibly the time out , havent tested that part yet ) doesnt work
theoriginalbit #8
Posted 05 March 2013 - 02:11 PM
Ok so for something this simple you don't really need to use the parallel api, you can instead just use a pull event loop with timers, where you can check if the event is 'player' or if it is 'timer'

something like this
Spoiler


local h = io.open( "Log", "a")

local function writeToFile(msg)
  h:write(msg..'\r\n')
  h:flush()
end

print("now controlling the mob grinder")

writeToFile("=== Program activated. day: "..os.day().." time: "..os.time().." ===")

local timeout = os.startTimer(0)
local output = false

while true do
  local event = { os.pullEvent() }

  if event[1] == 'timer' and event[2] == timeout then
    writeToFile("- deactivated via timeout on "..os.day().." at "..os.time())
    timeout = os.startTimer(3)
    output = false
  elseif event[1] == 'player' then
    output = not output
    writeToFile(event[2]..' '..(output and 'activated' or 'deactivated')..' on '..os.day()..' at '..os.time())
  end

  rs.setOutput('bottom', output)
end
etopsirhc #9
Posted 05 March 2013 - 02:17 PM
so the timeout = os.startTimer(3) is where i'd put the 5 min delay (300) and then the rest looks like it'd work perfictly

[edit] though not sure if i get the ( output and "activated" or "deactivated" ) part, is that just a simple inline if to return one of the strings?
theoriginalbit #10
Posted 05 March 2013 - 02:20 PM
yes exactly. sorry i was using 3 seconds to test the code, forgot to change it for you :P/>
etopsirhc #11
Posted 05 March 2013 - 02:21 PM
lol ok ^^ ty
theoriginalbit #12
Posted 05 March 2013 - 02:35 PM
no problems.

something additional you could also do is change this

if event[1] == 'timer' and event[2] == timeout then
to this

if event[1] == 'timer' and event[2] == timeout and output then
that way your Log doesn't get spammed with timeout disabling.