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

Big Reactors Controll

Started by kaschunuss, 16 April 2015 - 04:18 PM
kaschunuss #1
Posted 16 April 2015 - 06:18 PM
Hi, i'm just writing a script to turn on and off my Reactor.

I get the status from the reactor but it doesn't refresh automatically on the monitor.
It is only refreshing when i press any key in the computer.

Here is my code


local mon = peripheral.wrap("monitor_3")
local rec1 = peripheral.wrap("BigReactors-Reactor_1")
local cell = peripheral.wrap("cofh_thermalexpansion_energycell_1")

mon.clear()
function display()
mon.setTextScale(1)
mon.setCursorPos(1,1)
mon.write("Status:")
mon.setCursorPos(1,2)
mon.write("[ ] An")
mon.setCursorPos(1,3)
mon.write("[ ] Aus")
end
display()

function status()
if rec1.getActive() == true then
  mon.setCursorPos(2,3)
  mon.write(" ")
  mon.setCursorPos(2,2)
  mon.write("x")
elseif rec1.getActive() == false then
  mon.setCursorPos(2,3)
  mon.write("x")
  mon.setCursorPos(2,2)
  mon.write(" ")
end
end
function getClick()
local event, side, x, y = os.pullEvent()
status()
if event == "monitor_touch" then
  status()
  if x == 2 and y == 2 then
   rec1.setActive(true)
   status()
  elseif x == 2 and y == 3 then
   rec1.setActive(false)
   status()
  end
end
end
status()

while true do
  status()
  getClick()
  print(rec1.getActive())
	
end

Please help.

Thanks :)/>
Laserswald #2
Posted 16 April 2015 - 07:25 PM
I was just working on something like this.

What is happening is that os.pullEvent is waiting until it gets an event. That's why it stops and starts when you touch the screen - an event is generated, the event handler code does it's thing, and then hangs again when it listens for another event.

In order to display things and listen for events too, you need to use coroutines. A great tutorial for them is this one.
KingofGamesYami #3
Posted 16 April 2015 - 07:47 PM
Instead of messing with coroutines, use a simple event loop

Something like, say, this:

local timer = os.startTimer( 1 )
while true do
  local event = { os.pullEvent() }
  if event[ 1 ] == "monitor_touch" then
    --#handle monitor events here
  elseif event[ 1 ] == "timer" and event[ 2 ] == timer then
    --#render screen here
    timer = os.startTimer( 1 )
  end
end