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

need help with making errors output a print string

Started by cheekycharlie101, 21 October 2012 - 09:45 AM
cheekycharlie101 #1
Posted 21 October 2012 - 11:45 AM
ok, so im making a simple program that writes a label to a disk for you.

here is my code:

write("Drive Side: ")
local a = io.read()
write("Disk Name: ")
local B = io.read()
print("writing label to disk")
sleep(2)
disk.setLabel(a, :)/>/>
if disk.setLabel == "false" then
print("Failed to find disk/drive")
else
print("Disk Label Set Successfully")
end
ok, so it will set the label to the disk and everything and will print Disk label set successfully. but when i put in a invalid drive side it just says "disk:3: Invalid Side."
but what i want it to say is that Failed to find disk/drive.
i also noticed on the disk api on the wiki is that the function

disk.setlabel()
returns nil.
so i tried putting

if disk.setLabel == "nil" then
and that did not work either.
can anyone help me out here?
thanks -Cheeky
remiX #2
Posted 21 October 2012 - 11:56 AM
Try this (not sure if it will work :)/>/>), Other people would probably suggest using a table with the valid sides


write("Drive Side: ")
local a = io.read()
if a == "right" or a == "left" or a == "bottom" or a == "above" or a == "front" or a == "back" then else print("Invalid side") sleep (2) os.reboot() end
write("Disk Name: ")
local B = io.read()
print("writing label to disk")
sleep(2)
disk.setLabel(a, :)/>/>
if disk.setLabel == "false" then
print("Failed to find disk/drive")
else
print("Disk Label Set Successfully")
end
ChunLing #3
Posted 21 October 2012 - 12:08 PM
"if disk.setLabel == "nil" then"
will always return false because disk.setLabel contains a function, not nil, and not the string, "nil"

You want "if not disk.setLabel() then"

But it will still give the "disk:3: Invalid Side." message, the function does that when you call it with an invalid side.
casr144 #4
Posted 21 October 2012 - 03:21 PM
Can you not check if there is a disk present? I'm not entirely sure about this but wont "if disk.isPresent()" work? Somebody please correct me if I am wrong here.
Lettuce #5
Posted 21 October 2012 - 03:55 PM
That's part of the parser. You can't fix it. If a command doesn't work, it errors, and tells you the line it errored on. The program never runs, so there is no way to make it execute a function. This is also why if you do this:

print(x)
x = "3"
It errors, even though x is declared later.
Lyqyd #6
Posted 21 October 2012 - 05:55 PM
Lettuce's example may just print the word nil, actually.

Look at the peripheral API. It will have functions you can use to see what's on the side of the computer, and if it's a disk drive, what sort of disk (if any) is present.
cheekycharlie101 #7
Posted 21 October 2012 - 05:56 PM
Thanks, il use all the tips given and try and make my code better and fully working. if i manage it il post a link here to the fully working program.
ChunLing #8
Posted 21 October 2012 - 08:26 PM
Use the disk.isPresent() function. You should be able to combine this with the commonplace rs.getSides() peripheral checking loop to find whether (and where) a disk is present.
function finddisk()
    for i,v in pairs(rs.getSides()) do    
        if disk.isPresent(v) then return v end
    end
return nil end
This function should check all sides of the computer for a disk, if it finds one it returns the side, if not it returns nil. You could also have it build a table containing all the sides that have a disk, but probably don't need that.