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

[Big reactors] getConnected without reactor problems

Started by sacredrose, 01 February 2015 - 11:35 AM
sacredrose #1
Posted 01 February 2015 - 12:35 PM
Hello pro's,

I'm pretty much a noob in programming so i think it is just me. but i'm trying to make a status screen for my reactor. which has a nice startup screen in which i check if the reactor is connect. it seems logical that i use getConnected for this because it returns true if a reactor is connected. but if i disconnect the reactor i just keep getting the error: attempt to index? (a nil value)


this is the code i've created thus far and with the reactor connected is works.

local monitor3x1 = peripheral.wrap("monitor_1")
local reactor = peripheral.wrap("BigReactors-Reactor_0")
local conreact = 0
conreact = reactor.getConnected()
monitor3x1.clear()
monitor3x1.setCursorPos(7, 3)
monitor3x1.write("Finding reactors...")
if conreact==true then
monitor3x1.clear()
monitor3x1.setCursorPos(10, 2)
monitor3x1.write("Reactor found")
monitor3x1.setCursorPos(2, 3)
monitor3x1.write("initializing... Please wait")
monitor3x1.setCursorPos(5, 5)
monitor3x1.write("Reactor Status Program")
else
monitor3x1.clear()
monitor3x1.setCursorPos(8, 2)
monitor3x1.setTextColor(0x4000)
monitor3x1.write("WARNING!!")
monitor3x1.setTextColor(0x1)
monitor3x1.setCursorPos(4, 3)
monitor3x1.write("No reactor found")
monitor3x1.setCursorPos(1, 4)
monitor3x1.write("Please check your connection")
end

is there a way i can prevent that error message from showing ?
HPWebcamAble #2
Posted 01 February 2015 - 06:00 PM
is there a way i can prevent that error message from showing ?


function main()
  --#all of your code
end

local state,err = pcall(main)

If the function 'main' errors, 'state' will be 'false' and 'err' will be the error
The advantage is the error doesn't immediately stop your program, and you can have your program react to the error

if i disconnect the reactor i just keep getting the error: attempt to index? (a nil value)

This is what you should probably do:

local reactor = peripheral.wrap("BigReactors-Reactor_0")

if not reactor then
  --#its not connected
else
  --#its connected
end

'reactor' will be nil if peripheral.wrap can't find what you tell it
Edited on 01 February 2015 - 05:00 PM
sacredrose #3
Posted 01 February 2015 - 09:09 PM
thank you very much. i added your lines of code and now it is working perfectly :)/>

and i feel kinda stupid seeing how simple it was. i was just looking at it the wrong way.
Bomb Bloke #4
Posted 01 February 2015 - 09:45 PM
Remember that reactor.getConnected() isn't testing whether a reactor is connected to your computer - rather, it's testing whether a reactor is connected to the computer control block you've wrapped as a peripheral.