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

Open a door from the outside with a password and from the inside with a button! HELP!

Started by AChineseBuffet, 04 November 2017 - 10:28 AM
AChineseBuffet #1
Posted 04 November 2017 - 11:28 AM
After hours of useless attempts i am completely stuck. I have no idea where to start. The password portion of the program has to be running incase im outside of the building. But at the same time it has to check for the redstone signal coming from the button. Help.
KingofGamesYami #2
Posted 04 November 2017 - 12:55 PM
What you need is the parallel API.
Edited on 04 November 2017 - 01:17 PM
AChineseBuffet #3
Posted 04 November 2017 - 05:52 PM
Before going to bed last night i made a Parallel Loop but im having a couple of issues. First off, from the outside the print = ("passcode") shoes up on the computer, but i am not able to input any thing. After a couple of seconds I get a Startup:17: too long without yielding. Then whatever password i tried to type would then show up under that. I am able to use a button to open the door if i hit it before the program crashes. Here's my code:

local function outside()
while true do
term.clear()
term.setCursorPos(1 ,1)
print("Passcode")
input = read("*")
if input == "46678" then
redstone.setOutput("top", true)
sleep(4)
redstone.setOutput("top", false)
end
end
end

local function inside()
while true do
if redstone.getInput("back") == true then
redstone.setOutput("top", true)
sleep(4)
redstone.setOutput("top"', False)
end
end
end

parallel.waitForAny(outside, inside)
Dog #4
Posted 04 November 2017 - 08:46 PM
OK, I believe the problem is in your inside function. You're constantly polling the redstone input on the back without yielding. In CC, even with parallel, everything is running in serial; basically one thing at a time (it just happens so fast that it seems like it's happening at the same time). Each program is given a slice of time to run some of its code then it must yield so the next program can run, etc. etc.. Since each program has to wait for the previous program before it can run, a timer is placed on each program - if that program doesn't 'yield' after a set amount of time, allowing the next program a slice of time, CC will crash the program with a failure to yield error - this allows all the other programs on all the other computers to continue running.

Yielding can be accomplished in several different ways. In your case, a simple os.pullEvent("redstone") call before you check the back input would solve your problem. This will 'pause' the inside function (allowing it to yield) until a change in redstone occurs. Then it'll run through the rest of its code and return to yielding until the next redstone event.
Luca_S #5
Posted 04 November 2017 - 08:47 PM
Before going to bed last night i made a Parallel Loop but im having a couple of issues. First off, from the outside the print = ("passcode") shoes up on the computer, but i am not able to input any thing. After a couple of seconds I get a Startup:17: too long without yielding. Then whatever password i tried to type would then show up under that. I am able to use a button to open the door if i hit it before the program crashes. Here's my code:

-snip-
Please use code blocks when posting code.
Your problem is that your inside() function only sleeps when there is an input from the back, otherwise it doesn't sleep.
To fix your problem you could add a sleep() behind the end of the if in your inside() function.

Before going to bed last night i made a Parallel Loop but im having a couple of issues. First off, from the outside the print = ("passcode") shoes up on the computer, but i am not able to input any thing. After a couple of seconds I get a Startup:17: too long without yielding. Then whatever password i tried to type would then show up under that. I am able to use a button to open the door if i hit it before the program crashes. Here's my code:

-snip-
Please use code blocks when posting code.
Your problem is that your inside() function only sleeps when there is an input from the back, otherwise it doesn't sleep.
To fix your problem you could add a sleep() behind the end of the if in your inside() function.
AChineseBuffet #6
Posted 04 November 2017 - 09:20 PM
It worked. Thank you!