Computercraft Lua code not working as expected -
i quite new lua feel have decent grasp on basics. in computercraft, tried design own monitor display whether or not reactors on or not. came 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 if colors.test(rs.getbundledinput("right"), color) == true monitor.setcursorpos(cursor1,cursor2) monitor.settextcolor(colors.green) monitor.write("active ") elseif colors.test(rs.getbundledinput("right"), color) == false 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, did not give me desired result. instead of showing each reactor name , whether or not active, showed reactor names, showed whether or not first reactor active. other 5 reactors had blank spaces next names.
this image shows occurs on monitor
this came work around. works, longer first:
function test(color,cursor1,cursor2) while true if colors.test(rs.getbundledinput("right"), color) == true monitor.setcursorpos(cursor1,cursor2) monitor.settextcolor(colors.green) monitor.write("active ") elseif colors.test(rs.getbundledinput("right"), color) == false 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 if colors.test(rs.getbundledinput("right"), colors.red) == true monitor.setcursorpos(12,1) monitor.settextcolor(colors.green) monitor.write("active ") elseif colors.test(rs.getbundledinput("right"), colors.red) == false monitor.setcursorpos(12,1) monitor.settextcolor(colors.red) monitor.write("inactive") end if colors.test(rs.getbundledinput("right"), colors.orange) == true monitor.setcursorpos(12,3) monitor.settextcolor(colors.green) monitor.write("active ") elseif colors.test(rs.getbundledinput("right"), colors.orange) == false monitor.setcursorpos(12,3) monitor.settextcolor(colors.red) monitor.write("inactive") end if colors.test(rs.getbundledinput("right"), colors.yellow) == true monitor.setcursorpos(12,5) monitor.settextcolor(colors.green) monitor.write("active ") elseif colors.test(rs.getbundledinput("right"), colors.yellow) == false monitor.setcursorpos(12,5) monitor.settextcolor(colors.red) monitor.write("inactive") end if colors.test(rs.getbundledinput("right"), colors.green) == true monitor.setcursorpos(12,7) monitor.settextcolor(colors.green) monitor.write("active ") elseif colors.test(rs.getbundledinput("right"), colors.green) == false monitor.setcursorpos(12,7) monitor.settextcolor(colors.red) monitor.write("inactive") end if colors.test(rs.getbundledinput("right"), colors.blue) == true monitor.setcursorpos(12,9) monitor.settextcolor(colors.green) monitor.write("active ") elseif colors.test(rs.getbundledinput("right"), colors.blue) == false monitor.setcursorpos(12,9) monitor.settextcolor(colors.red) monitor.write("inactive") end if colors.test(rs.getbundledinput("right"), colors.purple) == true monitor.setcursorpos(12,11) monitor.settextcolor(colors.green) monitor.write("active ") elseif colors.test(rs.getbundledinput("right"), colors.purple) == false 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 implement similar code other systems prefer similar first code rather second one, if possible.
i still quite new coding, sincerely apologize if obvious or stupid error. i've kind of learned looking @ code , trying different things. sincerely appreciate problem!
also, suggestions streamline or simplify @ appreciated! thank you!!
i know how you, first of all, in first block of code in function test used "while true do" gives no way escape loop (except using "break"), checking first 1 , cannot escape check others.
try (untested):
local monitor = peripheral.wrap( "top" ) monitor.clear() function screen() monitor.settextcolor( colors.white ) = 1, 6 monitor.setcursorpos( 1, i*2-1 ) monitor.write( "reactor " .. .. ": " ) end end function test( color, x, y ) if colors.test( rs.getbundledinput( "right" ), color ) 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 k, v in pairs( rscolors ) test( k, 12, v ) end sleep( 0.1 ) end
ps: direwolf20 did reactor program explaining in video.
- button (xbbmuynn)
- reactor (4qnyapav)
Comments
Post a Comment