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

Need help with coding

Started by technosword22, 25 November 2012 - 12:58 AM
technosword22 #1
Posted 25 November 2012 - 01:58 AM
hey guys, i was playing with computercraft a bit on a server with a few friends and i need some help…i wanted
to create a user interface for, by means of wireless redstone, turning my machines (quarries, automatic farms etc.) on
and off from a central terminal in my home. Now, when booting the computer it asks for a password and if entered incorrectly 3 times,
the computer shuts down, if its correct you get to the main menu. you have a few options in a list and in the bottom line
you can type in what section you want to go to. that works fine as well, and if you for example go to the quarries section, it tells you which quarries are on and which are off. you can turn them on or off by typing "turn quarry 1 on" for example, and the white wire in the bundled cable lights up and starts the quarry. i have set the UI to then return to the home screen, but when i go to the quarry section again, it still tells me that quarry 1 is off. so basically i need help with coding that the computer reads the BUNDLED input and if a certain color combination is on, it should print the correct UI page. i have tried it with

-if redstone.getBundledInput("back") == "1" then
-if redstone.getBundledOutput("back") == "1" then
-if redstone.getBundledInput("back", 1) == "true" then
-if redstone.getBundledOutput("back", 1) == "true" then

and i have set it so that if none of the combinations (1,2,3 or 0) is true, it should print a page of the interface saying "could not retrieve data" and it always puts that, meaning that the code is incorrect…so guys please if anybody knows how to put bundled cables into an "if" command please help me, i can also post the whole code if someone wanted me to, its like 260 lines though
Goof #2
Posted 25 November 2012 - 02:07 AM
Post full code, please :D/>/>
technosword22 #3
Posted 25 November 2012 - 02:26 AM

term.clear()
term.setCursorPos(1,1)
write("+-------------------------------------------+")
term.setCursorPos(1,2)
write("|                                           |")
term.setCursorPos(1,3)
write("|    ==HOME==                               |")
term.setCursorPos(1,4)
write("|                                           |")
term.setCursorPos(1,5)
write("|     -Quarries                             |")
term.setCursorPos(1,6)
write("|     -Pumpkin Farm                         |")
term.setCursorPos(1,7)
write("|     -Turtles                              |")
term.setCursorPos(1,8)
write("|                                           |")
term.setCursorPos(1,9)
write("+-------------------------------------------+")
term.setCursorPos(1,10)



input = read()
if input == "quarries" then
term.clear()

if redstone.getBundledOutput("back", 1) == "true" then  --THIS LINE IS THE ONE I NEED HELP WITH--
term.setCursorPos(1,1)
write("+-------------------------------------------+")
term.setCursorPos(1,2)
write("|                                           |")
term.setCursorPos(1,3)
write("|    ==QUARRIES==                           |")
term.setCursorPos(1,4)
write("|                                           |")
term.setCursorPos(1,5)
write("|     -Quarries are currently ON            |")
term.setCursorPos(1,6)
write("|     -Ore Refining is currently OFF        |")
term.setCursorPos(1,7)
write("|                                           |")
term.setCursorPos(1,8)
write("|                                           |")
term.setCursorPos(1,9)
write("+-------------------------------------------+")
term.setCursorPos(1,10)







elseif redstone.getBundledOutput("back", 2) == "true" then
term.clear()
term.setCursorPos(1,1)
write("+-------------------------------------------+")
term.setCursorPos(1,2)
write("|                                           |")
term.setCursorPos(1,3)
write("|    ==QUARRIES==                           |")
term.setCursorPos(1,4)
write("|                                           |")
term.setCursorPos(1,5)
write("|     -Quarries are currently OFF           |")
term.setCursorPos(1,6)
write("|     -Ore Refinig is ON                    |")
term.setCursorPos(1,7)
write("|                                           |")
term.setCursorPos(1,8)
write("|                                           |")
term.setCursorPos(1,9)
write("+-------------------------------------------+")
term.setCursorPos(1,10)




elseif redstone.getBundledOutput("back", 3) == "true" then
term.setCursorPos(1,1)
write("+-------------------------------------------+")
term.setCursorPos(1,2)
write("|                                           |")
term.setCursorPos(1,3)
write("|    ==QUARRIES==                           |")
term.setCursorPos(1,4)
write("|                                           |")
term.setCursorPos(1,5)
write("|     -Quarries are currently ON            |")
term.setCursorPos(1,6)
write("|     -Ore Refining is currently ON         |")
term.setCursorPos(1,7)
write("|                                           |")
term.setCursorPos(1,8)
write("|                                           |")
term.setCursorPos(1,9)
write("+-------------------------------------------+")
term.setCursorPos(1,10)



else
term.setCursorPos(1,1)
write("+-------------------------------------------+")
term.setCursorPos(1,2)
write("|                                           |")
term.setCursorPos(1,3)
write("|    ==QUARRIES==                           |")
term.setCursorPos(1,4)
write("|                                           |")
term.setCursorPos(1,5)
write("|     -Could not retrieve data              |")
term.setCursorPos(1,6)
write("|                                           |")
term.setCursorPos(1,7)
write("|                                           |")
term.setCursorPos(1,8)
write("|                                           |")
term.setCursorPos(1,9)
write("+-------------------------------------------+")
term.setCursorPos(1,10)

end

quarry = read()
if quarry == "turn quarries on" then
redstone.getBundledOutput("back" + colors.white)
shell.run("monitor", "top", "disk/menu")
elseif quarry == "turn quarries off" then
redstone.getBundledOutput("back" - colors.white) --BTW IS THERE I MINUS COLOR COMMAND?--
shell.run("monitor", "top", "disk/menu")
elseif quarry == "turn refining off" then
redstone.getBundledOutput("back" - colors.orange)
shell.run("monitor", "top", "disk/menu")
elseif quarry == "turn refining on" then
redstone.getBundledOutput("back" + colors.orange)
shell.run("monitor", "top", "disk/menu")
elseif quarry == "turn all on" then
redstone.setBundledOutput("back", 3)
shell.run("monitor", "top", "disk/menu")
elseif quarry == "home" then
shell.run("monitor", "top", "disk/menu")
end







elseif input == "pumpkin farm" then
term.clear()
if redstone.testBundledInput("right", 1) == "true" then
term.setCursorPos(1,1)
write("+-------------------------------------------+")
term.setCursorPos(1,2)
write("|                                           |")
term.setCursorPos(1,3)
write("|    ==PUMPKIN FARM==                       |")
term.setCursorPos(1,4)
write("|                                           |")
term.setCursorPos(1,5)
write("|     -Pumpkin Farm is currently ON         |")
term.setCursorPos(1,6)
write("|                                           |")
term.setCursorPos(1,7)
write("|                                           |")
term.setCursorPos(1,8)
write("|                                           |")
term.setCursorPos(1,9)
write("+-------------------------------------------+")
term.setCursorPos(1,10)
else
term.clear()
term.setCursorPos(1,1)
write("+-------------------------------------------+")
term.setCursorPos(1,2)
write("|                                           |")
term.setCursorPos(1,3)
write("|    ==PUMPKIN FARM==                       |")
term.setCursorPos(1,4)
write("|                                           |")
term.setCursorPos(1,5)
write("|     -Pumpkin Farm is currently OFF        |")
term.setCursorPos(1,6)
write("|                                           |")
term.setCursorPos(1,7)
write("|                                           |")
term.setCursorPos(1,8)
write("|                                           |")
term.setCursorPos(1,9)
write("+-------------------------------------------+")
term.setCursorPos(1,10)
end
farm = read()
if farm == "turn farm on" then
redstone.getBundledOutput("back" + colors.magenta)
shell.run("monitor", "top", "disk/menu")
elseif farm == "turn farm off" then
redstone.getBundledOutput("back" - colors.magenta)
shell.run("monitor", "top", "disk/menu")
elseif farm == "turn pumpkin farm on" then
redstone.getBundledOutput("back" + colors.magenta)
shell.run("monitor", "top", "disk/menu")
elseif farm == "turn pumpkin farm off" then
redstone.getBundledOutput("back" - colors.magenta)
shell.run("monitor", "top", "disk/menu")
elseif farm == "home" then
shell.run("monitor", "top", "disk/menu")
end

elseif input == "turtles" then
term.clear()
term.setCursorPos(1,1)
write("+-------------------------------------------+")
term.setCursorPos(1,2)
write("|                                           |")
term.setCursorPos(1,3)
write("|    ==TURTLES==                            |")
term.setCursorPos(1,4)
write("|                                           |")
term.setCursorPos(1,5)
write("|     -No turtles connected to UnixNet      |")
term.setCursorPos(1,6)
write("|                                           |")
term.setCursorPos(1,7)
write("|                                           |")
term.setCursorPos(1,8)
write("|                                           |")
term.setCursorPos(1,9)
write("+-------------------------------------------+")
term.setCursorPos(1,10)
turtles = read()
if turtles == "home" then
shell.run("monitor", "top", "disk/menu")
end

elseif input == "refresh" then
shell.run("monitor", "top", "disk/menu")
elseif input == "home" then
shell.run("monitor", "top", "disk/menu")
elseif input == "command prompt" then
term.clear()
term.setCursorPos(1,1)
end
technosword22 #4
Posted 25 November 2012 - 02:37 AM
that whole code then gets shell.run ´ed on a monitor when the password was entered correctly
technosword22 #5
Posted 25 November 2012 - 07:10 AM
got it to work, using if redstone.testBundledOutput("back", 1) == true then
ChunLing #6
Posted 26 November 2012 - 08:40 AM
This kind of thing:

term.setCursorPos(1,1)
write("+-------------------------------------------+")
term.setCursorPos(1,2)
write("|										   |")
term.setCursorPos(1,3)
write("|    ==QUARRIES==						   |")
term.setCursorPos(1,4)
write("|										   |")
term.setCursorPos(1,5)
write("|	 -Could not retrieve data			  |")
term.setCursorPos(1,6)
write("|										   |")
term.setCursorPos(1,7)
write("|										   |")
term.setCursorPos(1,8)
write("|										   |")
term.setCursorPos(1,9)
write("+-------------------------------------------+")
term.setCursorPos(1,10)
can be replaced by something like:
for i = 2,8 do term.setCursorPos(1,i) write("|                                           |") end
term.setCursorPos(6,3) write("==QUARRIES==")
term.setCursorPos(7,5) write("-Could not retrieve data")
for i = 1,9,8 do term.setCursorPos(1,i) write("+-------------------------------------------+") end
print("")
If you go further and put the drawing of the box into a function, then you only have to define that once:
local function drawBox()
for i = 2,8 do term.setCursorPos(1,i) write("|                                           |") end
for i = 1,9,8 do term.setCursorPos(1,i) write("+-------------------------------------------+") end
end
And then you call that and then fill it in:
drawBox()
term.setCursorPos(6,3) write("==QUARRIES==")
term.setCursorPos(7,5) write("-Could not retrieve data")
term.setCursorPos(1,10)
This might seem like a minor point, smaller the actual code size the easier it is to hunt down errors and problems, and the more you use functions to do things the more consistent the program behavior will be.
technosword22 #7
Posted 27 November 2012 - 06:56 AM
yea…but i didnt go into functions at all yet and have no idea how they work -.-
ChunLing #8
Posted 29 November 2012 - 08:20 AM
For the purposes of the forgoing (a function with no arguments or returns) you can just think of it as a section of code that will run whenever you call it.
Dlcruz129 #9
Posted 29 November 2012 - 02:22 PM
You might want to look into the print function. It's basically the write function, but it automatically sets the cursor to the next line.
ChunLing #10
Posted 30 November 2012 - 10:08 AM
That's true, there are several places where that would be more appropriate (for drawing the box in the first place). But for going back and filling in the middle without disturbing the box, write is necessary.

I usually don't bother with fancying up the UI, I just want less of the code devoted to the task. I'm not going to seriously try to work on a program where more than two thirds of the code is just specifying UI.