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

Making an Access Keycard Program

Started by Thoradal, 25 October 2012 - 12:12 PM
Thoradal #1
Posted 25 October 2012 - 02:12 PM
I have this code for my Access Card doors in my Lab facilities, though on chunk load/unload, the computer reboots. This causes the program to be removed somehow. I am not a good Lua programmer yet, and would really appreciate the help. Thanks!
The code to review:

password = "99999"
debug_char = "0"

term.clear()

function mainLoop()
while(true) do
event, driveSide = os.pullEventRaw()
if(event=="disk" and driveSide) then
path = disk.getMountPath(driveSide)
if(path) then
path = path.."/access"
file = fs.exists(path) and io.open(path, "r") or nil
end
disk.eject(driveSide)
if(file and file:read()==password) then
rs.setOutput("left", true)
sleep(3)
rs.setOutput("left", false)
end
if(file) then file:close() file = nil end
elseif(debug_char and event=="char" and driveSide==debug_char) then return("break") end
end
end

rs.setOutput("left", false)
sfile = io.open("/startup", "w")
sfile:write('shell.run("keycard")')
sfile:close()

repeat
ok, err, val = pcall(mainLoop)
if(not ok and err) then
if(err=="Terminated") then print ("Access denied.")
else
print(err)
end
end
until(ok and err=="break")

Any help would be greatly appreciated.
remiX #2
Posted 25 October 2012 - 02:47 PM
name the file startup and put it into the computer's ID folder
Thoradal #3
Posted 25 October 2012 - 04:11 PM
I did, that's not the problem. It's when the computer reboots, (everytime) that the file disappears. When I check the ID folder after the computer reboots both are gone, empty folder.
remiX #4
Posted 25 October 2012 - 04:21 PM
Disappears? O_o There must be something causing all your computer folders to reset or something. Put multiple computers down open them so they get an ID and then just make a few edits then save, reboot. Does it delete everything then?
Thoradal #5
Posted 25 October 2012 - 05:15 PM
I'll try really quickly, thanks again for so much help. ^_^/>/>
I'm glad people on the official site are nice and unlike other mod sites, some of the IRC…. EEEEK.
remiX #6
Posted 25 October 2012 - 05:25 PM
I'll try really quickly, thanks again for so much help. ^_^/>/>
I'm glad people on the official site are nice and unlike other mod sites, some of the IRC…. EEEEK.

Lawl xD It's a weird problem you're having tho lol o.O
ChunLing #7
Posted 25 October 2012 - 05:53 PM
You're not using the CC fs API in your file writing, this means that CC can't save the files properly through it's interface.

I can see why this makes startup disappear (or never get written), not quite clear on why it makes the running program disappear. But try using the fs API rather than trying to open the files directly through io.open().