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

Detecting Disk?

Started by ebernerd, 10 January 2013 - 04:47 AM
ebernerd #1
Posted 10 January 2013 - 05:47 AM
So, I am new to Lua. I have used a tiny bit of it in ROBLOX, but that is it. I have kinda programmed my own operating system, if you could call it that. The only cool thing is the logon screen, and that is it.

So, I need help with mostly one thing at the moment: I know that you can make the computer detect if there is a disk in the drive, and if it is a floppy, but could someone give me an example of code, and I would give them credit, if I ever release my OS?

Thanks! You'd be the man/woman… fail.

Viextra
Zudo #2
Posted 10 January 2013 - 06:19 AM

disk.isPresent("[side]")
disk.containsData("[side]") or disk.containsAudio("[side]")
anonimo182 #3
Posted 10 January 2013 - 06:25 AM

for i = 1, v in pairs(rs.getSides()) do
 if disk.isPresent(v) and disk.containsData(v) then
  return v
 end
end
That will loop for every side and look for the first disk to appear
ebernerd #4
Posted 10 January 2013 - 06:30 AM
Thanks so much!
ebernerd #5
Posted 10 January 2013 - 06:42 AM
My code is stuck… could anyone help?
CODE:


if disk.isPresent("right") then
function check (attempt)

term.clear()
term.setCursorPos(1,1)
textutils.slowPrint("|-------------|")
textutils.slowPrint("| MineOS v1.0 |")
textutils.slowPrint("|-------------|")
print()
textutils.slowPrint("Enter your password")
input = read("*");
if input == "eric1123" then
  print("Correct. Logging on...");
  sleep(2)
  term.clear()
  term.setCursorPos(1,1)
else
  if attempt == 5 then
  write("You have enetered incorrectly too many times. Shutting off.");
  sleep(2);
  os.shutdown();
  else
	write("Username or password is incorrect. Please try again. ")
	sleep(1)
	check(attempt + 1);
end
end
end

else
print("NO DISK!!! D:")
check(0);
I know I am missing and end, but when I add it, it says <eof> expected.
zekesonxx #6
Posted 10 January 2013 - 06:46 AM
<eof> means you are missing at least one end.
ebernerd #7
Posted 10 January 2013 - 06:53 AM
So I try fixing it, and I put an end where it should go, and it tells me i need it on the next line.
bjaorndvinCoder #8
Posted 10 January 2013 - 07:06 AM
Did you put the end before

check(0): ?
Heracles421 #9
Posted 10 January 2013 - 07:09 AM

function check ()
  attempt = 0
  term.clear()
  term.setCursorPos(1,1)
  textutils.slowPrint("|-------------|")
  textutils.slowPrint("| MineOS v1.0 |")
  textutils.slowPrint("|-------------|")
  print("")
  while true do
    textutils.slowPrint("Enter your password")
    input = read("*")
    if input == "eric1123" then
      print("Correct. Logging on...")
      sleep(2)
      term.clear()
      term.setCursorPos(1,1)
     else
      if attempt == 5 then
        write("You have enetered incorrectly too many times. Shutting off.")
        sleep(2)
        os.shutdown()
      else
        write("Username or password is incorrect. Please try again. ")
        sleep(1)
        attempt = attempt + 1
      end
    end
  end
end

if disk.isPresent("right") then
  check()
else
  print("NO DISK!!! D:")
end

Some things were wrong: 1. You defined a function inside an if statement and the you tried to call it from the else, which can't be made since the condition to create the function hasn't been met. 2. In the function args you don't need to have the variable attempts, simple define it inside the function. 3. print() and write() cannot be left without args, if you want to skip a line without printing anything do print("")

Tips: 1. Always format your code, makes it easier to read and debug. 2. In lua you don't need ; at the end of everything.
remiX #10
Posted 10 January 2013 - 07:11 AM
Looks like you were missing an end right at the end of the code.

I see you're using a function, but that is not needed.

You can do something like this:

attempts = 5
tries = 0

while tries <= attempts
if disk.isPresent("right") then
term.clear()
term.setCursorPos(1,1)
textutils.slowPrint("|-------------|")
textutils.slowPrint("| MineOS v1.0 |")
textutils.slowPrint("|-------------|")
print()
textutils.slowPrint("Enter your password")
input = read("*");
if input == "eric1123" then
print("Correct. Logging on...");
sleep(2)
term.clear()
term.setCursorPos(1,1)
else
if tries == attempts then -- if it's equal to the max amount of attempt times.
write("You have enetered incorrectly too many times. Shutting off.");
sleep(2);
os.shutdown();
else
write("Username or password is incorrect. Please try again. ")
tries = tries + 1 -- increases tries if you enter wrong password
sleep(1)
end
end
else
print("NO DISK!!! D: Put a disk in then press enter")
read()
end
end
ebernerd #11
Posted 10 January 2013 - 09:06 AM
Looks like you were missing an end right at the end of the code.

I see you're using a function, but that is not needed.

You can do something like this:

attempts = 5
tries = 0

while tries <= attempts
if disk.isPresent("right") then
term.clear()
term.setCursorPos(1,1)
textutils.slowPrint("|-------------|")
textutils.slowPrint("| MineOS v1.0 |")
textutils.slowPrint("|-------------|")
print()
textutils.slowPrint("Enter your password")
input = read("*");
if input == "eric1123" then
print("Correct. Logging on...");
sleep(2)
term.clear()
term.setCursorPos(1,1)
else
if tries == attempts then -- if it's equal to the max amount of attempt times.
write("You have enetered incorrectly too many times. Shutting off.");
sleep(2);
os.shutdown();
else
write("Username or password is incorrect. Please try again. ")
tries = tries + 1 -- increases tries if you enter wrong password
sleep(1)
end
end
else
print("NO DISK!!! D: Put a disk in then press enter")
read()
end
end

thanks.. but now it will always loop in the ENTER YOUR PASSWORD phase… D:

I do appreciate you helping me and spending the time to recode my work!!! :D/> I will certainly give credit!
crazyguymgd #12
Posted 10 January 2013 - 09:59 AM
add a break when the password is correct after you reset the cursor position to get out of the while loop
ebernerd #13
Posted 10 January 2013 - 10:41 AM
add a break when the password is correct after you reset the cursor position to get out of the while loop
What is a break… just an empty line?
crazyguymgd #14
Posted 10 January 2013 - 10:45 AM
add a break when the password is correct after you reset the cursor position to get out of the while loop
What is a break… just an empty line?

just the word break. for example:


i = 1
while true do
  i = i + 1
  if i == 5 then
	break
  end
end

will break out of the while loop when the condition i == 5 is true
ebernerd #15
Posted 10 January 2013 - 11:00 AM
add a break when the password is correct after you reset the cursor position to get out of the while loop
What is a break… just an empty line?

just the word break. for example:


i = 1
while true do
  i = i + 1
  if i == 5 then
	break
  end
end

will break out of the while loop when the condition i == 5 is true
THANK YOU SOOOOO MUCH!!!

I have another question… is there a way to set the thing directly to disk?
so instead of manually typing "cd disk" can I make It automagically do that from the code?
crazyguymgd #16
Posted 10 January 2013 - 11:17 AM

shell.run("cd disk")

I think that's what you're looking for.
ebernerd #17
Posted 10 January 2013 - 11:20 AM

shell.run("cd disk")

I think that's what you're looking for.
Thanks!
crazyguymgd #18
Posted 10 January 2013 - 11:21 AM

shell.run("cd disk")

I think that's what you're looking for.
Thanks!

Good thing I'm bored today and like programming problems :P/>