简体   繁体   中英

Computercraft Lua code not working as expected

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!!

I know exactly how to help you, first of all, in the first block of code in the function test you used "while true do" which gives it no way to escape the loop (except using "break"), so its constantly checking for the first one and cannot escape to check for the others.

Try this (untested):

local monitor = peripheral.wrap( "top" )
monitor.clear()

function screen()
  monitor.setTextColor( colors.white )
  for i = 1, 6 do
    monitor.setCursorPos( 1, i*2-1 )
    monitor.write( "Reactor " .. i .. ": " )
  end
end

function test( color, x, y )
  if colors.test( rs.getBundledInput( "right" ), color ) then
    monitor.setCursorPos( x, y )
    monitor.setTextColor( colors.green )
    monitor.write("Active  ")
  else
    monitor.setCursorPos( x, y )
    monitor.setTextColor( colors.red )
    monitor.write( "Inactive" )
  end
end

local rscolors = {
  colors.red = 1,
  colors.orange = 3,
  colors.yellow = 5,
  colors.green = 7,
  colors.blue = 9,
  colors.purple = 11
}

while true do
  for k, v in pairs( rscolors ) do
    test( k, 12, v )
  end
  sleep( 0.1 )
end

PS: Direwolf20 already did a reactor program explaining it in a video .

  • button (XBbMUYNn)
  • reactor (4qNyaPav)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM