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

updating while capturing events

Started by Hydrotronics, 29 May 2016 - 07:29 PM
Hydrotronics #1
Posted 29 May 2016 - 09:29 PM
So, I'm working on another reactor control program and I would like to update the screen while also being able to capture the mouse click event. I looked this up, found a result but came to no avail. it did nothing :/
I'm afraid I can't provide a pastebin as I am playing on a server in which they have disabled https.

Thanks In Advance - Hydro
KingofGamesYami #2
Posted 29 May 2016 - 09:38 PM
Use parallel. I'm fairly certain we've told you how it works before.
Dog #3
Posted 30 May 2016 - 12:13 AM
You can use parallel or use a loop that filters by event type, for example…
Using a loop…

while true do --# start and infinite loop
  local event, data, mX, mY = os.pullEvent() --# capture events
  if event == "mouse_click" then --# if the event is a mouse click then...
    --# do mouse event stuff, data = mouse button, mX & mY = mouse x & y position
  elseif event == "char" and data:lower() == "q" then --# if the letter 'q' is pressed...
    return --# return from the loop, thus exiting it
  elseif event == "reactorUpdate" then --# if the event is a reactor update or whatever then...
    --# do screen update stuff
  end
end
Using parallel…

local function inputMouse()
  while true do --# start an infinite loop that never exits
    local _, mButton, mX, mY = os.pullEvent("mouse_click") --# capture mouse click events
    --# do mouse click stuff here
  end
end

local function inputChar()
  while true do --# start in an infinite loop that never exits
    local _, char = os.pullEvent("char") --# capture character events
    if char:lower() == "q" then return end --# if 'q' is pressed, 'return' from this function, thus exiting it and allowing the program to exit
  end
end

local function reactorUpdate()
  while true do --# start an infinite loop that never exits
    local _, data1, data2, data3 = os.pullEvent("reactorUpdate") --# capture reactor update events
    --# do screen update stuff here
  end
end

parallel.waitForAny(inputMouse, inputChar, reactorUpdate) --# parallel.waitForAny allows all functions to run together, it will terminate when any function exits
Edited on 30 May 2016 - 01:26 AM
Hydrotronics #4
Posted 30 May 2016 - 06:35 PM
ta! i've decided upon using Parallel! thx tho!
LezChap #5
Posted 31 May 2016 - 06:59 AM
I tend to use the loop method in ALL my programs that require acting on multiple stimulus (redstone, mouseclicks, etc)…I also find you'll need to insert a os.startTimer(x) before the loop, and a if event == "timer" os.startTimer(x) end in the event handling…in this case, x is the time (in seconds) in which the controller program runs it's periodic updates (checks reactor/turbine statuses and changes the control rods, turbine coils engaged, etc).