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

Reactor on/off + display of state

Started by rian, 04 August 2012 - 08:11 AM
rian #1
Posted 04 August 2012 - 10:11 AM
I've been beating my head with this for quite a bit now but I can't seem to figure it out.

I'm basically trying to have a small display of information with the reactor's state (on or off) and by inputting On or Off, to turn it on or off, obviously.

This is the code. When I run it it returns "reactor:1: attempt to call nil".

What do?
Spoilerreactor=rs.getoutput("bottom")

if reactor == false then

reactor="Reactor is Online"

end



if reactor==true then

reactor="Reactor is Offline"

end



shell.run("clear")

local function centertext(text)

local x,y=term.getsize()

local x2,y2=term.getCursorpos()

term.setcursorpos(math.ceil((x/2)-(text:len())))

print(text)

end



centertext("VaultTech205 Control Panel")

centertext("V10.5")

centertext(" ")

centertext("Available Commands")

print(" ")

centertext("Reactor is online : Reactor is Offline")

print(" ")

centertext("Current States")

print(" ")

centertext("Power",reactor)

centertext(reactor)

write("Console: ")



input=read()



if input=="On" then

rs.setoutput("bottom",false)

print("Reactor is now Online.")

end



if input=="Off" then

rs.setoutput("bottom", true)

print("Reactor is now Offline.")

end



sleep(2)

shell.run("clear")
Luanub #2
Posted 04 August 2012 - 10:16 AM
Change line 1 to

local reactor = rs.getOutput("bottom")

You've made this same mistake throughout the code and on multiple functions. Remember Lua is case sensitive.

Check the api's on the wiki for proper case.

It's also better to make your vars local. Globals can get messy.
Edited on 04 August 2012 - 08:18 AM
rian #3
Posted 04 August 2012 - 10:28 AM
Ah, damn. Got it.

Thanks for the help.
rian #4
Posted 04 August 2012 - 10:35 AM
Spoilerlocal reactor=rs.getOutput("bottom")

if reactor == false then

reactor="Reactor is Online"

end



if reactor==true then

reactor="Reactor is Offline"

end



shell.run("clear")

local function centerText(text)

local x,y=term.getSize()

local x2,y2=term.getCursorPos()

term.setCursorPos(math.ceil((x/2)-(text:len())))

print(text)

end



centerText("VaultTech205 Control Panel")

centerText("V10.5")

centerText(" ")

centerText("Available Commands")

print(" ")

centerText("Reactor is online : Reactor is Offline")

print(" ")

centerText("Current States")

print(" ")

centerText("Power",reactor)

centerText(reactor)

write("Console: ")



input=read()



if input=="On" then

rs.setOutput("bottom",false)

print("Reactor is now Online.")

end



if input=="Off" then

rs.setOutput("bottom", true)

print("Reactor is now Offline.")

end



sleep(2)

shell.run("clear")

This is what I have now.
I'm getting "reactor:27: bad argument: int expected, got nil"
Luanub #5
Posted 04 August 2012 - 11:00 AM
Change your centerText() function to this

local function centerText(text)
local x,y=term.getSize()
local x2,y2=term.getCursorPos()
term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2) -- you missed the y, only had x
print(text)
end
rian #6
Posted 04 August 2012 - 11:04 AM
It works now, many thanks again. :]