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

Doorstuck !

Started by sirchichi, 03 June 2012 - 03:15 AM
sirchichi #1
Posted 03 June 2012 - 05:15 AM
Ok, ive never coded in my life, and ive been able to run/make a few programs since ive started to use computer craft, yet ive only had it for about an hour. I want to make it so when i insert a floppy disk in the computer, it will open a door. itd be better if i could get different floppy disks so open seperate doors,if anyone could explain this, and give me a VERY dumbed down code explanaiton that would be great :)/>/>
Grim Reaper #2
Posted 03 June 2012 - 06:21 AM
If you mean something like disk keys to open only certain doors, I may be able to help! The great thing about computer-craft is that it's a great way to get into coding and computer science in a basic and fun way!

Now, for the code!

A very simple way to accomplish this task is to either have the disks have specific labels that open certain doors or have a file which has a password of some sort to be read by the computer that will then open the door.

Here is a solution for the label checking idea:


-- Let uss check if there is a disk present! --
tSides = rs.getSides() -- 'This table holds all sides available, we will use it to check if the disk exists'
sSide = nil -- 'If the disk is present this is where we will store the side that it is in'
bExists = false -- 'Variable to check if there is actually a disk present'

sDiskName = nil --'This is what the disk name in the drive will be called if it even exists'
sCorrectDiskName = "password" -- 'This will be what the disk should be named if it is to open the door'
sDoorSide = "right" -- 'This is the side of the door adjacent to the computer'

function checkDiskSide( _bExists, _sSide)
for i=1, #tSides do -- 'Iterate through all of the sides starting with 1'
if disk.isPresent(tSides[i]) then -- 'If there is in fact a disk present, return _bExists as true and the side it is present'
_bExists = true
_sSide = tSides[i]

return _bExists, _sSide
end
end

_bExists = false
_sSide = nil
return _bExists, _sSide -- 'If there is no disk then return _bExists as false and leaving the side as nil'
end

while true do -- 'This will create an infinite loop until broken by a "Terminate", "Shutdown", or "Reboot" (Ctrl+T is terminate!)'
bExists, sSide = checkDiskSide() -- 'Check the sides for a disk'

if bExists then -- 'If there is in fact a disk then run this code...'
sDiskName = disk.getLable(sSide) -- 'Get the name of the disk in the drive on sSide'

if sDiskName == sCorrectDiskName then -- 'If the disk label is the same as our set password then run this code...'
rs.setOutput(sDoorSide, true) -- 'Set a redstone ouput to the door side we set in sDoorSide'
disk.eject(sSide) -- 'Eject the disk for the player to have back'

sleep(2) -- 'Wait or "sleep" for 2 seconds so our player can get throught the door'

rs.setOutput(sDoorSide, false) -- 'After 2 seconds then close the door by terminating the redstone signal on the door side'
end
end
end
D3matt #3
Posted 03 June 2012 - 06:27 AM
Basically, you'd want to have a program that checks (Using the FS API, look on the wiki or type "help fs" for more information on it) for a file on the floppy disk with a certain name and if the contents of the file matches a certain string. You could use os.pullEvent checking for a disk event to check when the disk is inserted. Finished code might look something like this:


passfile = "disk/cake"
password = "cookies"
side = "back"
while true do
os.pullEvent(disk)
if fs.exists(passfile) then
  h = fs.open(passfile, r)
  string = h.read()
  if string = password then
   rs.setOutput(side, true)
   os.sleep(5)
   rs.setOutput(side, false)
  end
end
end
I just through that together and I've no idea if it actually works, but it should give you at least an idea of what it should look like.

@PayMentionIption: Your code is fancy, but not really effective. Your infinite loop has nothing to stop it from just wasting CPU cycles doing nothing. It's much better to use events, and the disk event gives the side the disk is in as an argument if you wanted to check it, which makes it even easier.
Pinkishu #4
Posted 03 June 2012 - 01:14 PM
Add a diskEject for easy retrieval :)/>/>