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

Password Door Switch Stuck Between Logins

Started by Bosonic_Theory, 14 August 2013 - 06:56 PM
Bosonic_Theory #1
Posted 14 August 2013 - 08:56 PM
Greetings,

–CODE AT BOTTOM–

I recently began coding in LUA to automate aspects of Minecraft. I started with using someone else's door lock. After creating my own programs to automate my farms, I decided I wanted to write my own door lock.

The lock I had opened the door for a few seconds when you entered the right password. I started working on a program that would accept multiple inputs (one to open, close, reboot, etc.). I wanted it to act as a switch that requires a password. I play on a very small, personal server so leaving my door open when I'm working is quite a convenience and comes with little risk.

I thought I had it figured out until I started testing what would happen when I would log out and come back to the computer.

Here is what happens sequentially:
SpoilerLog In.

No Computer. Door Closed.
Placed Computer. Door Closed.
Input 123. Door Open.
Input qwe. Door Closed.
Input asd (Reboot). Door Closed.

Log Out.
Log In.

Input 123.
Door Open.
Input qwe. Door Closed.
Input asd (Reboot). Door Closed.
Input 123. Door Open.

Log Out.
Log In.


–Door is currently open–
Input qwe. Door Open.
–Door didn't close–
Input 123. Door Open.
Input qwe. Door Closed.

When I relog and try to close the door, it won't. When I relog (and its open) and try to reboot, it does reboot (not shown above). What gives? I am guessing it has something to do with the fact that the chunk gets unloaded when I leave, the redstone signal gets confused and doesn't reset properly. Unfortuantely, I can't tell from the API. I think this is an indicator to a bigger problem involving with the logic of my program.

I would also like a few pointers on lexical conventions. I learned to program on Java and have played with a few other languages, like C++, C#, HTTP, CSS. I am just applying what I already know to LUA; however, I'm sure there's a more accepted manner. I am assuming pastebin links are better for large amounts of code and simply copy/pastes are for small amounts in the forums. I'll just do both.

I have access to the server files (I own/run the server) so I edit my specific computer's program files directly with TextPad through FTP. I have also labeled my computer and labeled the program "startup".

If it is a matter of chunk loading and it can be corrected by keeping the chunk loaded, I can easily do this with the ChickenChunks.

Hopefully its not something small that I've missed! >.<


Thanks!

Brandon

Code:
Spoiler

--[[ Password Enabled Door Switch
Function:  Receives input to open or close door
and remains in resulting state
until nexy input.
  Also receives input to reboot computer.
  No local variables.
  ]]--

os.pullEvent = os.pullEventRaw --Avoid termination

while true do --Keep it running always

term.clear() --Clear the screen

term.setCursorPos(1, 1) --Reset cursor to top left
print("Password:") --Prompt user

input = read("*") --Hide input with stars

if input == "123" then
redstone.setOutput("right", true) --Open Door

elseif input == "qwe" then
redstone.setOutput("right", false) --Close Door

elseif input == "asd" then --For easy code updating
term.clear()
term.setCursorPos(1, 1)
print("Rebooting...")
sleep(2)
os.reboot() --Reboot OS

end --End if statement

end --End while loop
http://pastebin.com/4JCBnxGk
Lyqyd #2
Posted 14 August 2013 - 09:28 PM
Split into new topic.
MxHn #3
Posted 14 August 2013 - 09:41 PM
Redstone.setOutput("side",true)keeps outputing the signal untill the bool is set to false. You need another line to set to false.
Oops i derped!
MR_nesquick #4
Posted 14 August 2013 - 09:57 PM
why not add redstone.setOutput("right", false) before your loop?
Bosonic_Theory #5
Posted 14 August 2013 - 10:23 PM
That was my first thought. But I have tried that and it operated with the exact same problem.

What about restarting the program each time it passes through the loop? I'm not sure how to do that, though, because just a simple reboot won't cut it.

I have also tried reversing the open and close if statements, as so:
Spoiler

--[[ Password Enabled Door Switch
Function:  Receives input to open or close door
and remains in resulting state
until next input.
  Also receives input to reboot computer.
  No local variables.
  ]]--

os.pullEvent = os.pullEventRaw --Avoid termination

while true do --Keep it running always

term.clear() --Clear the screen

term.setCursorPos(1, 1) --Reset cursor to top left
print("Code:") --Prompt user

input = read("*") --Hide input with stars

if input == "qwe" then
redstone.setOutput("right", false) --Close Door

elseif input == "123" then
redstone.setOutput("right", true) --Open Door

elseif input == "asd" then --For easy code updating
term.clear()
term.setCursorPos(1, 1)
print("Rebooting...")
sleep(2)
os.reboot() --Reboot OS

end --End if statement

end --End while loop
immibis #6
Posted 14 August 2013 - 10:54 PM
You could try opening and closing the door at the start of the program, but then other people could log out and in again and quickly run through the door while it's open.
Best solution is probably just to not leave the door open when you log off. You could make it close automatically after a few seconds.
Bosonic_Theory #7
Posted 14 August 2013 - 11:07 PM
It seems that I'm just going to have to close it before I log, or cycle it when I get back. I just simplified my code and altered my logic slightly (although the same problem persists).
Here it is now:
Spoiler

while true do

term.clear()
term.setCursorPos(1, 1)
print("Code:")
input = read("*")

if input == "1" then
redstone.setOutput("right", true)
end

if input == "2" then
redstone.setOutput("right", false)
end

if input == "3" then
os.reboot()
end

end

It doesn't look pretty but it works all the same. Hopefully I don't run into this again with later programs >.<

Thanks for all your responses, everyone.
MxHn #8
Posted 15 August 2013 - 02:36 PM
Maybe set the rs output at false before the loop?
Lyqyd #9
Posted 15 August 2013 - 02:46 PM
Maybe set the rs output at false before the loop?

Please read the rest of the topic; that was previously suggested and OP already responded to that suggestion.