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

How to make optionals?

Started by Azhf, 09 March 2013 - 09:58 AM
Azhf #1
Posted 09 March 2013 - 10:58 AM
So I was making a disk that opens a door to the right and ejects itself. I want the ejection to be optional. How do I do this? Also, I want the user to input the "Yes" "No" for eject.
remiX #2
Posted 09 March 2013 - 11:05 AM

print( "Would you like to eject the disk? (Yes/No)" )
local opt
local cx, cy = term.getCursorPos()
repeat term.setCursorPos( cx, cy ) term.clearLine() opt = read():lower() until opt == "yes" or opt == "no"
if opt == "yes" then
    disk.eject( 'right' )
else
    print( 'okay :o/>' )
end

Use an if statement … the repeat loop is just for so it only continues until the user types yes or no
Azhf #3
Posted 09 March 2013 - 11:12 AM
Thanks. This will be for the first AzhfTech: Keycard xP I will give some credit.
Engineer #4
Posted 09 March 2013 - 11:20 AM
If you want it to be optional to wich side the redstone emits you have to put on your disk the program "mydoorside" and run this code ( or implement it in a way )

--first lets open the file we want to read > opening in reading mode, "r"
local file = fs.open("/disk/mydoorside", "r")
-- lets read the file:
local fileReader = file.readAll()
--And what you must never forget, close the file.
file.close()

-- Determine wich side to set the redstone output
if fileReader == "right" then
   redstone.setOutput("right", true)
   -- etc.
elseif fileReader == "left" then
   redstone.setOutput("left", true)
   -- etc
else
   print("The file is invalid to me")
end

Edit: wow.. I derped hard. I didnt read your message properly I guess, but anyway now you learned hopefully something about files..
TheOddByte #5
Posted 09 March 2013 - 11:48 AM
Have posts above and you could do this
if you just want them to press a button


while true do
term.clear()
term.setCursorPos(1,1)
print("Would you like to eject the disk? (y/n)")
evt, p1 = os.pullEvent()
if evt == "char" and
p1 == "y" then
  disk.eject("right")
  print("Disk ejected!")
  sleep(2)
elseif p1 == "n" then
  print("Disk was not ejected!")
  sleep(2)
  end
   end