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

Attempt to call nil (Big reactor code)

Started by Weed_Craft, 01 July 2015 - 08:37 PM
Weed_Craft #1
Posted 01 July 2015 - 10:37 PM
I swear this code is perfect and I have no idea what else to do, so here I am.

Code is as follows:

local reactor = peripheral.wrap("left")
while true do
sleep(0)
if reactor.getEnergyStored() < 1000000 then
reactor.setActive (true)

end
if reactor.getEnergyStored() > 10000000 then
reactor.setActive (false)

end
end
Lyqyd #2
Posted 02 July 2015 - 01:05 AM
Moved to Ask a Pro.
HPWebcamAble #3
Posted 02 July 2015 - 01:38 AM
It would really help to know what the exact error is, since it has the line number

But without the line…

Is there actually a reactor to the left of the computer?
I think you need to use the Reactor Computer Port to interface with the reactor in any way, is the computer touching that?


Side notes:
Use code tages [code.] code here without the period
Instead of sleep(0), why not sleep(5), or higher? Does it really need to run every few ticks?

Here is the code, cleaned up a bit:
Spoiler

local reactor = peripheral.wrap("left")

while true do
  if reactor.getEnergyStored() < 1000000 then
	reactor.setActive(true)
  else --# If the energey isn't less than 1000000, then it must be greater
	reactor.setActive(false)
  end
  sleep(5)
end
Edited on 01 July 2015 - 11:41 PM
Bomb Bloke #4
Posted 02 July 2015 - 02:51 AM
--# If the energey isn't less than 1000000, then it must be greater

Not so - it could be equal! :P/> Though that's still, of course, a better way to do it.

Another option is to forget about the "if" block, simply resolving the check and passing the result directly to reactor.setActive():

reactor.setActive(reactor.getEnergyStored() < 1000000)