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

IC2 reactor as a peripheral.

Started by Kronix, 24 May 2016 - 12:29 AM
Kronix #1
Posted 24 May 2016 - 02:29 AM
I found an API on the FTB forums for IC2 reactors, but I'm having trouble even using a test program.

Code I used to test the reactor:
Spoiler

local reactor = peripheral.wrap("right")
local reactor.isActive() = active
print(active)

The program returns:


attempt to index ? (a nil value)
Bomb Bloke #2
Posted 24 May 2016 - 03:17 AM
You're attempting to treat a nil object as if it were a table. You're indexing into tables at two points - one where you try to get "wrap" from within the "peripheral" table, and one where you try to get "isActive" from within the "reactor" table.

Odds are if you read the full error you'd see it points you at the second line, as it's far more likely "reactor" is nil. And if "reactor" is nil, "wrap" failed to find a peripheral to the right of the computer.

Assuming you haven't simply gotten your sides mixed up, note that ComputerCraft and IC2 have no compatibility with each other. The OpenPeripheral mod adds that support.
Kronix #3
Posted 25 May 2016 - 01:17 AM
Thank you, I added OpenPeripherals and the program is working just fine.
Kronix #4
Posted 25 May 2016 - 05:15 AM
I've now made a program to monitor the reactor status. The program runs without errors, other than the stats of the reactor don't update. Only the time on the monitor updates.

The program:

SpoilerKeep in mind that the monitor is connected via cable and the reactor is to the right of the computer

local reactor = peripheral.wrap("right")
local mon = peripheral.wrap("monitor_0")
local active = reactor.isActive()
local heat = reactor.getHeat()
local maxheat = reactor.getMaxHeat()
local output = reactor.getEUOutput()
while true do
mon.clear()
mon.setCursorPos(1,1)
mon.setTextScale(0.5)
mon.setTextColor(colors.white)
mon.write("Status:")
if active then
  mon.setTextColor(colors.lime)
  mon.write (" Online")
else
  mon.setTextColor(colors.red)
  mon.write(" Offline")
end
mon.setCursorPos(1,2)
mon.setTextColor(colors.white)
mon.write("Heat: ")
if heat < 3000 then
  mon.setTextColor(colors.blue)
  mon.write(heat)
  mon.setTextColor(colors.white)
  mon.write("/" .. maxheat)
elseif heat > 3000 and heat < 6000 then
  mon.setTextColor(colors.yellow)
  mon.write(heat)
  mon.setTextColor(colors.white)
  mon.write("/" .. maxheat)
elseif heat > 6000 then
  mon.setTextColor(colors.red)
  mon.write(heat)
  mon.setTextColor(colors.white)
  mon.write("/" .. maxheat)
  redstone.setOutput("bottom", true)
else
  mon.setTextColor(colors.gray)
  mon.write("Error")
  mon.setTextColor(colors.white)
  mon.write("/" .. maxheat)
end
mon.setCursorPos(1,3)
mon.setTextColor(colors.white)
mon.write("*Output: ")
if output < 20 then
  mon.setTextColor(colors.red)
  mon.write(output)
elseif output > 20 then
  mon.setTextColor(colors.lime)
  mon.write(output)
end
mon.setCursorPos(1,4)
mon.setTextColor(colors.white)
mon.write("Last updated: ".. os.time())
mon.setCursorPos(1,24)
mon.write("*Power readings may not be correct. Still a WIP.")
term.clear()
term.setCursorPos(1,1)
print("Securian Corporations INC")
sleep(5)
end

I know the code is horrendous, I wrote it in game.
Edited on 25 May 2016 - 03:16 AM
Bomb Bloke #5
Posted 25 May 2016 - 05:56 AM
When you do this:

local active = reactor.isActive()
local heat = reactor.getHeat()
local maxheat = reactor.getMaxHeat()
local output = reactor.getEUOutput()

… the functions are called, and the variables are assigned the values those functions return. When you later refer to those variables - "heat" and so on - you're pulling the numbers that were assigned in the past; the functions aren't called again to get up to date values unless you specifically order it and perform fresh assignments.

That is to say, move those lines into your loop, just below the "while true do".