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

1 terminal multiple disks

Started by --Kaos--, 28 February 2018 - 02:08 PM
--Kaos-- #1
Posted 28 February 2018 - 03:08 PM
Hi alll

so i have tried to figurie this out myself and look online for about a week before finally giving in and asking for help.

what i am trying to do is the following

I have 1 terminal computer and 1 disk drive. The disk drive is on the top of the computer
back , front left and right have redstone connected to them

What i want to do is when i insert a disk named "level2" then it activates for example the left side redstone for a time and then turn off , however, if i put in a disk named "basement" then the right redstone is activated

the labels of the names are redundant tbh as i was looking for the computer to read the disk with the following

ie.

disk labeled BASEMENT has data written of
key = "basement"

disk labeled "level2" has data written of
key = "level2"


So instead of having loads of disks i can have 4 disks to 1 computer and 1 drive. I want them to be used as access keys to open up different places per floor so having loads of terminals is something that wouldnt work

can someone pleaaasee help me as this is driving me nuts

thanks

Kaos
Edited on 28 February 2018 - 02:14 PM
KingofGamesYami #2
Posted 28 February 2018 - 03:55 PM
Have the computer listen for disk events. When it receives one, check the label, then set the appropriate output.

If you want to read the data on the disk, you'll need to open a file in text read mode ("r").
Edited on 28 February 2018 - 02:57 PM
--Kaos-- #3
Posted 28 February 2018 - 04:43 PM
wow thats great thank you

i understand the idea of that but not sure how to put this into context.

i found someone elses code online who wrote

os.pullEvent = os.pullEventRaw
while true do
		if disk.isPresent("top") then
				if fs.exists("disk/.security/key") then
						shell.run("disk/.security/key")
						if key == "lemmein" then
								disk.eject("top")
								rs.setOutput("left", true)
								sleep(3)
								rs.setOutput("left", false)
						else
								disk.eject("top")
						end
				else
						disk.eject("top")
				end
		end
		sleep(0.1)
end
but this only works for one floppy disk

this is actually perfect if this worked for say 4 different floppy disks
Edited on 28 February 2018 - 03:46 PM
KingofGamesYami #4
Posted 28 February 2018 - 05:24 PM

local tKeys = {
	["basement"] = "right",
	["level2"] = "left",
	--# add additional entries here to pair more sides and phrases
}
while true do
	local event, side = os.pullEvent( "disk" )
	--#note: instead of checking 10 times a second,
	--#this only checks when a disk was inserted
	if disk.hasData( side ) and fs.exists( "/disk/.security/key", "r" ) then
		--#I check if the disk has data first, because technically
		--#it's possible to insert music disks which don't have a file system.
		local file = fs.open( "/disk/.security/key" )
		local key = file.readAll()
		file.close()
		--#running a program on a disk is a major security flaw of the progrm you posted
		--#that program could do anything, such as deleting all your files, or opening all the doors
		--#instead, here I read the file, the contents of which should simply be basement or level2
		if tKeys[ key ] then --#modify this to be "basement" or whatever you want
			--#eject the disk
			disk.eject( side )
			rs.setOutput(tKeys[key], true)
			sleep(3)
			rs.setOutput(tKeys[key], false)
		else --#add additional elseifs before the else to add different sides.
			disk.eject( side )
		end
	end
end
Edited on 28 February 2018 - 05:13 PM
--Kaos-- #5
Posted 28 February 2018 - 05:25 PM
omg you are amazing!!!

i cannot thank you enough!!!!

what a legend :)/>
Edited on 28 February 2018 - 04:25 PM
--Kaos-- #6
Posted 28 February 2018 - 05:48 PM
Hi again

for some reason the programme seems to stop

i set the programe as startup
i get "startup:9: expected String, string

i assume the part its point to is line
if tKeys[ key ] then –#modify this to be "basement" or whatever you want"

ive put it in as
[basement]
["basement"]
"basement"
but stil not playing

this stuff is hard man hahahaha
Edited on 28 February 2018 - 05:19 PM
KingofGamesYami #7
Posted 28 February 2018 - 06:15 PM
I think I've fixed it now. Writing code without actually testing it is difficult and I've been doing too much Java / C++ lately.
--Kaos-- #8
Posted 28 February 2018 - 06:20 PM
you doing this without testing it is better than me with the entire internet and testing it!

your skills are superb
--Kaos-- #9
Posted 28 February 2018 - 06:34 PM
am i correct in saying this based on your expert knowledge


local tKeys = {
		["basement"] = "back",
		["level2"] = "left",
}
while true do
		local event, side = os.pullEvent("disk")
		if disk.hasData("top") and fs.exists( "/disk/.security/key", "r") then
				local file = fs.open( "/disk/.security/key" )
				local key = file.readAll()
				file.close()
				if tKeys["basement"] then
						disk.eject("top")
						rs.setOutput(tKeys[key], true)
						sleep(3)
						rs.setOutput(tKeys[key], false)
				elseif
		tKeys["level2"] then
						disk.eject("top")
						rs.setOutput(tKeys[key], true)
						sleep(3)
						rs.setOutput(tKeys[key], false)
	else
						disk.eject("top")
				end
		end
end

currently fails
startup:7: Expected String
Edited on 28 February 2018 - 05:44 PM
SquidDev #10
Posted 28 February 2018 - 06:46 PM
You've got the "r" slightly mixed up between lines 6 and 7. It should actually be:


if disk.hasData("top") and fs.exists( "/disk/.security/key" ) then
  local file = fs.open( "/disk/.security/key", "r" )
Edited on 28 February 2018 - 05:46 PM
--Kaos-- #11
Posted 28 February 2018 - 06:55 PM
im so glad knowledgable people like yourself exist as this is more complicated than i first thought ! haha

honestly, its appreciated like so much!

Startup:13: Expected string Boolean

:S

figured it out

the Boolean was because the part
rs.setOutput(tKeys[key], true)

should be
rs.setOutput(tKeys["basement"], true)

testing more as we speak but so grateful guys
--Kaos-- #12
Posted 28 February 2018 - 07:16 PM
so update

basement works fine

the code works as it should. the disk ejects, the redstone at the back lights up and then turns off

however, if i do the exact same thing with a floppy disk with level2 it does nothing ,no ejection or redstone

its like its being ignored :s
KingofGamesYami #13
Posted 28 February 2018 - 10:09 PM
I'm terribly sorry I've been messing this up so much, thought this would be super easy to write. Had a bunch of classes to take but I'm back now :)/>/>
Spoiler

local tKeys = {
        ["basement"] = "right",
        ["level2"] = "left",
        --# add additional entries here to pair more sides and phrases
}
while true do
        local event, side = os.pullEvent( "disk" )
        --#note: instead of checking 10 times a second,
        --#this only checks when a disk was inserted
        if disk.hasData( side ) and fs.exists( "/disk/.security/key" ) then
                --#I check if the disk has data first, because technically
                --#it's possible to insert music disks which don't have a file system.
                local file = fs.open( "/disk/.security/key", "r" )
                local key = file.readAll()
                file.close()
                --#running a program on a disk is a major security flaw of the progrm you posted
                --#that program could do anything, such as deleting all your files, or opening all the doors
                --#instead, here I read the file, the contents of which should simply be basement or level2
                if tKeys[ key ] then
                	--#if the key given is in tKeys ("basement" or "level2")
                    --#eject the disk
                    disk.eject( side )
                    --# set output on the side paired in tKeys (for "basement", that is "right")
                    rs.setOutput(tKeys[key], true)
                    sleep(3)
                    rs.setOutput(tKeys[key], false)
                else
                    disk.eject( side )
                end
        end
end
Don't actually modify the part that was marked as "modify this" with a comment earlier – that was accedentially left in there from an earlier design. The only part you should need to change is the table at the top.
Edited on 28 February 2018 - 09:11 PM
--Kaos-- #14
Posted 28 February 2018 - 10:19 PM
I'm terribly sorry I've been messing this up so much, thought this would be super easy to write. Had a bunch of classes to take but I'm back now :)/>/>
Spoiler

local tKeys = {
		["basement"] = "right",
		["level2"] = "left",
		--# add additional entries here to pair more sides and phrases
}
while true do
		local event, side = os.pullEvent( "disk" )
		--#note: instead of checking 10 times a second,
		--#this only checks when a disk was inserted
		if disk.hasData( side ) and fs.exists( "/disk/.security/key" ) then
				--#I check if the disk has data first, because technically
				--#it's possible to insert music disks which don't have a file system.
				local file = fs.open( "/disk/.security/key", "r" )
				local key = file.readAll()
				file.close()
				--#running a program on a disk is a major security flaw of the progrm you posted
				--#that program could do anything, such as deleting all your files, or opening all the doors
				--#instead, here I read the file, the contents of which should simply be basement or level2
				if tKeys[ key ] then
					--#if the key given is in tKeys ("basement" or "level2")
					--#eject the disk
					disk.eject( side )
					--# set output on the side paired in tKeys (for "basement", that is "right")
					rs.setOutput(tKeys[key], true)
					sleep(3)
					rs.setOutput(tKeys[key], false)
				else
					disk.eject( side )
				end
		end
end
Don't actually modify the part that was marked as "modify this" with a comment earlier – that was accedentially left in there from an earlier design. The only part you should need to change is the table at the top.

hehe its okay.

your time on this is very kind of you

i really appreciate it honestly, forever in your debt :)/>
--Kaos-- #15
Posted 28 February 2018 - 10:32 PM
So the disks eject as per the terms of basement and level2 but the redstone doesnt light up from the direction

im sorry i am not much use of this, im learning stacks of information from what you are giving so im learning as we go along
!

i fixed it!

it was the path in the end that was causing the major issues, i change the path just to /disk/disk/ in the code and now reads like a dream!!!!!

thanks so much!!!!
Edited on 28 February 2018 - 09:44 PM