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

[LUA] Cant Detect Drive

Started by BabyCodder, 01 April 2013 - 04:26 AM
BabyCodder #1
Posted 01 April 2013 - 06:26 AM
I have a problem with the following code:


fs.makeDir("account")
fs.makeDir("cookies")
dd = fs.open("cookie/disk", "w")


function comp()
print("Detecting Disk Drives...")
print("Please Wait")
sleep(3)
  if disk.isPresent("right") == true then
    print("Disk Drive 'Right' Enabled")
    dd.write("right")
    dd.close()
  elseif disk.isPresent("left") == true then
    print("Disk Drive 'Left' Enabled")
    dd.write("left")
    dd.close()
  elseif disk.isPresent("back") == true then
    print("Disk Drive 'back' Enabled")
    dd.write("back")
    dd.close()
  elseif disk.isPresent("bottom") == true then
    print("Disk Drive 'Bottom' Enabled")
    dd.write("bottom")
    dd.close()
  elseif disk.isPresent("top") == true then
    print("Disk Drive 'Top' Enabled")
    dd.write("top")
    dd.close()
  elseif disk.isPresent("front") == true then
    print("Disk Drive Is Not Allowd On The")
    print("Front Of The Computer")
  else
    print("No Disk Drive Detected")
    print("Please Install One")
  end
end

comp()
I have the "attempt to call nil" error on line 3 and line 11 (–fs.setDir("cookies")) (if disk.isPresent("right") then)
LBPHacker #2
Posted 01 April 2013 - 06:29 AM
1: We have no function called fs.setDir, what do you want to accomplish by calling that?
2: Are you sure disk.isPresent gives you attempt to call nil?
BabyCodder #3
Posted 01 April 2013 - 06:37 AM
1: We have no function called fs.setDir, what do you want to accomplish by calling that?
2: Are you sure disk.isPresent gives you attempt to call nil?

1. I fixed this.
2. That is the line it says. Maybe it is something near it.
BabyCodder #4
Posted 01 April 2013 - 06:51 AM
1: We have no function called fs.setDir, what do you want to accomplish by calling that?
2: Are you sure disk.isPresent gives you attempt to call nil?

1. I fixed this.
2. That is the line it says. Maybe it is something near it.

Ok, now it seems to work, however its not detecting a disk drive, why is this? (Disk drive is on right side)

I have re-named the title so no-one thinks its a error; as that is gone.
LBPHacker #5
Posted 01 April 2013 - 07:10 AM
AH! Got it. You don't close "dd" (the file handle) when you are done. That's the problem I think.
BabyCodder #6
Posted 01 April 2013 - 07:29 AM
AH! Got it. You don't close "dd" (the file handle) when you are done. That's the problem I think.

I have updated the code to close it. But still; it doesn't recognize the disk-drive.
LBPHacker #7
Posted 01 April 2013 - 07:37 AM
OK, tested it in-game, works perfectly. BUT you should change
dd = fs.open("cookie/disk", "w")
to
local dd = fs.open("cookies/disk", "w")

local is for the sake of Lua, cookies is for you are using the folder "cookies", not "cookie".
BabyCodder #8
Posted 01 April 2013 - 07:48 AM
OK, tested it in-game, works perfectly. BUT you should change
dd = fs.open("cookie/disk", "w")
to
local dd = fs.open("cookies/disk", "w")

local is for the sake of Lua, cookies is for you are using the folder "cookies", not "cookie".

Are you using the newest version of computercraft? I am not so that may be causing the problem.
LBPHacker #9
Posted 01 April 2013 - 07:51 AM
Nope (I'm on CC 1.5 for MC 1.4.7), but disk API has been working the same for a long time now (except .getDiskID which is now .getID), I don't think that's the problem.
LBPHacker #10
Posted 01 April 2013 - 07:56 AM
Oh for goodness sake… I've just realized: you want to detect the disk drive itself, don't you? In this case:

Use
peripheral.getType(side) == "drive"
instead of
disk.isPresent(side)

That's because disk.isPresent checks the drive for a disk :D/>
BabyCodder #11
Posted 01 April 2013 - 07:56 AM
Nope (I'm on CC 1.5 for MC 1.4.7), but disk API has been working the same for a long time now (except .getDiskID which is now .getID), I don't think that's the problem.

I'm going to upgrade my version just for the sake of it. I'll post soon if it's either still broke or fixed.
BabyCodder #12
Posted 01 April 2013 - 08:00 AM
Oh for goodness sake… I've just realized: you want to detect the disk drive itself, don't you? In this case:

Use
peripheral.getType(side) == "drive"
instead of
disk.isPresent(side)

That's because disk.isPresent checks the drive for a disk :D/>

OH! Well, no harm done. Also, one quick question. Do you know how to set the background color of the WHOLE screen instead of the background color of what you type?
LBPHacker #13
Posted 01 April 2013 - 08:00 AM
Nope (I'm on CC 1.5 for MC 1.4.7), but disk API has been working the same for a long time now (except .getDiskID which is now .getID), I don't think that's the problem.

I'm going to upgrade my version just for the sake of it. I'll post soon if it's either still broke or fixed.

Then I'm late with the reply already.


Oh for goodness sake… I've just realized: you want to detect the disk drive itself, don't you? In this case:

Use
peripheral.getType(side) == "drive"
instead of
disk.isPresent(side)

That's because disk.isPresent checks the drive for a disk :D/>

EDIT: OKAY, you saw it.
LBPHacker #14
Posted 01 April 2013 - 08:02 AM
The background color of the whole screen?



You mean how to wipe all the text and make the terminal full of one color? Just set the background color you want to use and then term.clear()
BabyCodder #15
Posted 01 April 2013 - 08:06 AM
The background color of the whole screen?



You mean how to wipe all the text and make the terminal full of one color? Just set the background color you want to use and then term.clear()

Thank You! I would like to add, i liked all of your replies :)/>
BabyCodder #16
Posted 01 April 2013 - 08:58 AM
Oh for goodness sake… I've just realized: you want to detect the disk drive itself, don't you? In this case:

Use
peripheral.getType(side) == "drive"
instead of
disk.isPresent(side)

That's because disk.isPresent checks the drive for a disk :D/>

I did this, it still doesn't detect it.
LBPHacker #17
Posted 01 April 2013 - 09:00 AM
Did you use exactly "side" or the proper sides? Because by "side" I meant those…
LBPHacker #18
Posted 01 April 2013 - 09:03 AM
You know what? Take this:

Spoiler


function comp()
    fs.makeDir("account")
    fs.makeDir("cookies")
    dd = fs.open("cookies/disk", "w")
    print("Detecting Disk Drives...")
    print("Please Wait")
    sleep(3)
    if peripheral.getType("right") == "drive" then
        print("Disk Drive 'Right' Enabled")
        dd.write("right")
        dd.close()
    elseif peripheral.getType("left") == "drive" then
        print("Disk Drive 'Left' Enabled")
        dd.write("left")
        dd.close()
    elseif peripheral.getType("back") == "drive" then
        print("Disk Drive 'back' Enabled")
        dd.write("back")
        dd.close()
    elseif peripheral.getType("bottom") == "drive" then
        print("Disk Drive 'Bottom' Enabled")
        dd.write("bottom")
        dd.close()
    elseif peripheral.getType("top") == "drive" then
        print("Disk Drive 'Top' Enabled")
        dd.write("top")
        dd.close()
    elseif peripheral.getType("front") == "drive" then
        print("Disk Drive Is Not Allowd On The")
        print("Front Of The Computer")
    else
        print("No Disk Drive Detected")
        print("Please Install One")
    end
end

comp()
BabyCodder #19
Posted 01 April 2013 - 09:07 AM
You know what? Take this:

Spoiler


function comp()
	fs.makeDir("account")
	fs.makeDir("cookies")
	dd = fs.open("cookies/disk", "w")
	print("Detecting Disk Drives...")
	print("Please Wait")
	sleep(3)
  if peripheral.getType("right") == "drive" then
	print("Disk Drive 'Right' Enabled")
	dd.write("right")
	dd.close()
  elseif peripheral.getType("left") == "drive" then
	print("Disk Drive 'Left' Enabled")
	dd.write("left")
	dd.close()
  elseif peripheral.getType("back") == "drive" then
	print("Disk Drive 'back' Enabled")
	dd.write("back")
	dd.close()
  elseif peripheral.getType("bottom") == "drive" then
	print("Disk Drive 'Bottom' Enabled")
	dd.write("bottom")
	dd.close()
  elseif peripheral.getType("top") == "drive" then
	print("Disk Drive 'Top' Enabled")
	dd.write("top")
	dd.close()
  elseif peripheral.getType("front") == "drive" then
	print("Disk Drive Is Not Allowd On The")
	print("Front Of The Computer")
  else
	print("No Disk Drive Detected")
	print("Please Install One")
  end
end

comp()

Thank you. I will not you as giving alot of support towards the OS this will be creating. I checked, the code your using is EXACTLY the same. But mine does not work. Also; could you please pastebin this?
LBPHacker #20
Posted 01 April 2013 - 09:15 AM
OK, it's on pastebin now (XEyGpuyt), but if it really doesn't work, I don't see the point in uploading it.
remiX #21
Posted 01 April 2013 - 09:48 AM
function comp()
    if not fs.isDir( 'account' ) then fs.makeDir( 'account' ) end
    if not fs.isDir( 'cookies' ) then fs.makeDir( 'cookies' ) end
	
	local side
    print("Detecting Disk Drives...")
    print("Please Wait")
    sleep(3)
	
	for _, s in pairs( rs.getSides() ) do
		if peripheral.getType( s ) == 'drive' then
			side = s
			break
		end
	end
	
	if side then
		if side ~= 'front' then
			local file = fs.open("cookies/disk", "w")
			file.write( side )
			file.close()
		else
			print( 'Disk Drive is not allowed on the\nfront of the computer' )
		end
	else
		print( 'No disk drive detected.\nPlease install one' )
	end
end

comp()
BabyCodder #22
Posted 01 April 2013 - 09:55 AM
function comp()
	if not fs.isDir( 'account' ) then fs.makeDir( 'account' ) end
	if not fs.isDir( 'cookies' ) then fs.makeDir( 'cookies' ) end
	
	local side
	print("Detecting Disk Drives...")
	print("Please Wait")
	sleep(3)
	
	for _, s in pairs( rs.getSides() ) do
		if peripheral.getType( s ) == 'drive' then
			side = s
			break
		end
	end
	
	if side then
		if side ~= 'front' then
			local file = fs.open("cookies/disk", "w")
			file.write( side )
			file.close()
		else
			print( 'Disk Drive is not allowed on the\nfront of the computer' )
		end
	else
		print( 'No disk drive detected.\nPlease install one' )
	end
end

comp()
Thank you for this. However this is beyond me and I dont like implementing code I don't understand.

OK, it's on pastebin now (XEyGpuyt), but if it really doesn't work, I don't see the point in uploading it.

I used this code however it does not work for me. This problem must be client side. Also, the sleep(3) is useless but creates a real computer feeling. It also gives the users a chance to read what it is doing :)/>
remiX #23
Posted 01 April 2013 - 10:04 AM
function comp()
	-- Use fs.isDir to check if the two directories exist
	-- If not, then only make the directories.
	if not fs.isDir( 'account' ) then fs.makeDir( 'account' ) end
	if not fs.isDir( 'cookies' ) then fs.makeDir( 'cookies' ) end
	
	-- Set a local variable, side, which is nil
	local side
	print("Detecting Disk Drives...")
	print("Please Wait")
	sleep(3)
	
	-- rs.getSides() returns this:
	-- { 'right', 'left', 'top', 'bottom', 'back', 'front' }
	-- basic table with all available sides
	-- this for loop iterates through all the sides and checks
	-- if there is a drive on a side, if there is, it sets side
	-- to the corresponding side, and breaking the loop
	for _, s in pairs( rs.getSides() ) do
		if peripheral.getType( s ) == 'drive' then
			side = s
			break
		end
	end
	
	-- if side then checks to see if side is not nil (which means
	-- that there is a drive on some side)
	-- then it checks if that side is NOT on the front, only then
	-- it will open the cookies/disk file and write the correct side
	if side then
		if side ~= 'front' then
			local file = fs.open("cookies/disk", "w")
			file.write( side )
			file.close()
		else
			print( 'Disk Drive is not allowed on the\nfront of the computer' )
		end
	else
		print( 'No disk drive detected.\nPlease install one' )
	end
end

comp()

:)/>
BabyCodder #24
Posted 01 April 2013 - 10:11 AM
function comp()
	-- Use fs.isDir to check if the two directories exist
	-- If not, then only make the directories.
	if not fs.isDir( 'account' ) then fs.makeDir( 'account' ) end
	if not fs.isDir( 'cookies' ) then fs.makeDir( 'cookies' ) end
	
	-- Set a local variable, side, which is nil
	local side
	print("Detecting Disk Drives...")
	print("Please Wait")
	sleep(3)
	
	-- rs.getSides() returns this:
	-- { 'right', 'left', 'top', 'bottom', 'back', 'front' }
	-- basic table with all available sides
	-- this for loop iterates through all the sides and checks
	-- if there is a drive on a side, if there is, it sets side
	-- to the corresponding side, and breaking the loop
	for _, s in pairs( rs.getSides() ) do
		if peripheral.getType( s ) == 'drive' then
			side = s
			break
		end
	end
	
	-- if side then checks to see if side is not nil (which means
	-- that there is a drive on some side)
	-- then it checks if that side is NOT on the front, only then
	-- it will open the cookies/disk file and write the correct side
	if side then
		if side ~= 'front' then
			local file = fs.open("cookies/disk", "w")
			file.write( side )
			file.close()
		else
			print( 'Disk Drive is not allowed on the\nfront of the computer' )
		end
	else
		print( 'No disk drive detected.\nPlease install one' )
	end
end

comp()

:)/>

Could you pastebin this?
==================================
BEWARE: If you pastebin this and give me the code it will be used for a OS that I am creating.
remiX #25
Posted 01 April 2013 - 10:33 AM
Why can't you?
Just copy paste?

Spoilero.0
BabyCodder #26
Posted 01 April 2013 - 10:35 AM
Why can't you?
Just copy paste?

Spoilero.0

I cant Copy and paste ingot a program, cause CTRL is used to save and exit
remiX #27
Posted 01 April 2013 - 10:43 AM
I meant why don't you copy and paste it into pastebin :Q

http://pastebin.com/rg9YitR1
BabyCodder #28
Posted 01 April 2013 - 10:56 AM
I meant why don't you copy and paste it into pastebin :Q

http://pastebin.com/rg9YitR1

Done, you are okay with this being in a OS?
remiX #29
Posted 01 April 2013 - 10:57 AM
Yes
BabyCodder #30
Posted 01 April 2013 - 11:02 AM
Yes
Thank you.