I am quite new to Lua but I feel I have a decent grasp on the basics. Recently in computercraft, I tried to design my own monitor to display whether or not my reactors were on or not. This is what I came up with:
function screen()
monitor = peripheral.wrap("top")
monitor.clear()
monitor.setCursorPos(1,1)
monitor.setTextColor(colors.white)
monitor.write("Reactor 1: ")
monitor.setCursorPos(1,3)
monitor.write("Reactor 2: ")
monitor.setCursorPos(1,5)
monitor.write("Reactor 3: ")
monitor.setCursorPos(1,7)
monitor.write("Reactor 4: ")
monitor.setCursorPos(1,9)
monitor.write("Reactor 5: ")
monitor.setCursorPos(1,11)
monitor.write("Reactor 6: ")
end
function test(color,cursor1,cursor2)
while true do
if colors.test(rs.getBundledInput("right"), color) == true then
monitor.setCursorPos(cursor1,cursor2)
monitor.setTextColor(colors.green)
monitor.write("Active ")
elseif colors.test(rs.getBundledInput("right"), color) == false then
monitor.setCursorPos(cursor1,cursor2)
monitor.setTextColor(colors.red)
monitor.write("Inactive")
end
sleep(0.1)
end
sleep(0.1)
end
sleep(0.1)
function status()
screen()
test(colors.red,12,1)
test(colors.orange,12,3)
test(colors.yellow,12,5)
test(colors.green,12,7)
test(colors.blue,12,9)
test(colors.purple,12,11)
sleep(0.1)
end
status()
Unfortunately, this did not give me the desired result. Instead of showing each reactor by name and whether or not it was active, it showed all reactor names, but only showed whether or not the first reactor was active. The other 5 reactors had blank spaces next to their names.
This image shows what occurs on the monitor
This is what I came up with as a work around. It works, but it is much longer than the first:
function test(color,cursor1,cursor2)
while true do
if colors.test(rs.getBundledInput("right"), color) == true then
monitor.setCursorPos(cursor1,cursor2)
monitor.setTextColor(colors.green)
monitor.write("Active ")
elseif colors.test(rs.getBundledInput("right"), color) == false then
monitor.setCursorPos(cursor1,cursor2)
monitor.setTextColor(colors.red)
monitor.write("Inactive")
end
sleep(0.1)
end
sleep(0.1)
end
sleep(0.1)
function status()
screen()
test(colors.red,12,1)
test(colors.orange,12,3)
test(colors.yellow,12,5)
test(colors.green,12,7)
test(colors.blue,12,9)
test(colors.purple,12,11)
sleep(0.1)
end
status()
function screen()
monitor = peripheral.wrap("top")
monitor.clear()
monitor.setCursorPos(1,1)
monitor.setTextColor(colors.white)
monitor.write("Reactor 1: ")
monitor.setCursorPos(1,3)
monitor.write("Reactor 2: ")
monitor.setCursorPos(1,5)
monitor.write("Reactor 3: ")
monitor.setCursorPos(1,7)
monitor.write("Reactor 4: ")
monitor.setCursorPos(1,9)
monitor.write("Reactor 5: ")
monitor.setCursorPos(1,11)
monitor.write("Reactor 6: ")
end
function test()
local monitor = peripheral.wrap("top")
while true do
if colors.test(rs.getBundledInput("right"), colors.red) == true then
monitor.setCursorPos(12,1)
monitor.setTextColor(colors.green)
monitor.write("Active ")
elseif colors.test(rs.getBundledInput("right"), colors.red) == false then
monitor.setCursorPos(12,1)
monitor.setTextColor(colors.red)
monitor.write("Inactive")
end
if colors.test(rs.getBundledInput("right"), colors.orange) == true then
monitor.setCursorPos(12,3)
monitor.setTextColor(colors.green)
monitor.write("Active ")
elseif colors.test(rs.getBundledInput("right"), colors.orange) == false then
monitor.setCursorPos(12,3)
monitor.setTextColor(colors.red)
monitor.write("Inactive")
end
if colors.test(rs.getBundledInput("right"), colors.yellow) == true then
monitor.setCursorPos(12,5)
monitor.setTextColor(colors.green)
monitor.write("Active ")
elseif colors.test(rs.getBundledInput("right"), colors.yellow) == false then
monitor.setCursorPos(12,5)
monitor.setTextColor(colors.red)
monitor.write("Inactive")
end
if colors.test(rs.getBundledInput("right"), colors.green) == true then
monitor.setCursorPos(12,7)
monitor.setTextColor(colors.green)
monitor.write("Active ")
elseif colors.test(rs.getBundledInput("right"), colors.green) == false then
monitor.setCursorPos(12,7)
monitor.setTextColor(colors.red)
monitor.write("Inactive")
end
if colors.test(rs.getBundledInput("right"), colors.blue) == true then
monitor.setCursorPos(12,9)
monitor.setTextColor(colors.green)
monitor.write("Active ")
elseif colors.test(rs.getBundledInput("right"), colors.blue) == false then
monitor.setCursorPos(12,9)
monitor.setTextColor(colors.red)
monitor.write("Inactive")
end
if colors.test(rs.getBundledInput("right"), colors.purple) == true then
monitor.setCursorPos(12,11)
monitor.setTextColor(colors.green)
monitor.write("Active ")
elseif colors.test(rs.getBundledInput("right"), colors.purple) == false then
monitor.setCursorPos(12,11)
monitor.setTextColor(colors.red)
monitor.write("Inactive")
end
sleep(0.1)
end
sleep(0.1)
end
sleep(0.1)
function run()
screen()
test()
end
run()
I would like to implement similar code for other systems but I would much prefer to do it similar to the first code rather than the second one, if possible.
I am still quite new to coding, so I sincerely apologize if this is an obvious or stupid error. I've kind of just learned by looking at code and trying different things. I would sincerely appreciate any help with my problem!
Also, any suggestions to streamline or simplify anything at all would also be most appreciated! Thank you!!