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

Loop Help With Nuke Reactor

Started by deathvondoom, 04 October 2013 - 08:52 PM
deathvondoom #1
Posted 04 October 2013 - 10:52 PM
hello. im having difficulty getting this loop to work. any help would be great.


print "reactor is now running"
local status, correct = "n"
while status~=correct do
hot = redstone.getBundledInput ("bottom", colors.blue)
realhot = redstone.getBundledInput ("bottom", colors.orange)
if hot == true then
print "the reactor is getting hot"
elseif realhot == true then
redstone.setBundledOutput ("bottom", false)
print "reactor is too hot!"
print "system has shut the reactor down to prevent damage"
print "would you like to restart the system?"
write "(Y/N): "
end
status = read()
if status == "y" then
shell.run "running"
else
print "shutting down"
sleep(1)
os.shutdown()
end
end
Grim Reaper #2
Posted 04 October 2013 - 11:03 PM
When you state

local status, correct = "n"
You're setting status to 'n' and correct to nil. Therefore, since you're assigning the return value of read to status, it can never be nil. Thus, when you compare status and correct, the values of the two variables will always be different and cause an infinite loop.

If you wanted to assign correct to "n" also, you could write

local status, correct = "n", "n"
However, this would cause your program to skip the loop entirely because status would equal correct when the loop makes its first check.

To avoid this, you could use a repeat … until loop:

local status, correct = "n", "n"

repeat
-- loop code
until status == correct
deathvondoom #3
Posted 04 October 2013 - 11:24 PM
When you state

local status, correct = "n"
You're setting status to 'n' and correct to nil. Therefore, since you're assigning the return value of read to status, it can never be nil. Thus, when you compare status and correct, the values of the two variables will always be different and cause an infinite loop.

If you wanted to assign correct to "n" also, you could write

local status, correct = "n", "n"
However, this would cause your program to skip the loop entirely because status would equal correct when the loop makes its first check.

To avoid this, you could use a repeat … until loop:

local status, correct = "n", "n"

repeat
-- loop code
until status == correct


thank you very much for your help…
it is now working but comes up with

"too long with out yielding"

how would i go about correcting this?
Grim Reaper #4
Posted 04 October 2013 - 11:55 PM
Would you mind posting your current code, please?
deathvondoom #5
Posted 05 October 2013 - 12:44 AM

print "reactor is now running"
repeat
hot = redstone.getBundledInput ("bottom", colors.blue)
realhot = redstone.getBundledInput ("bottom", colors.orange)
sleep(0.5)
until realhot == true
esd = realhot
if esd == true then
redstone.setBundledOutput ("bottom", false)
print "reactor is too hot!"
print "system has shut the reactor down"
print "to prevent damage"
print "would you like to restart the system?"
write "(Y/N): "
sleep(0.5)
else
print "reactor is hot"
end
restart = read()
if restart == "y" then
shell.run "running"
else
print "shutting down"
sleep(1)
os.shutdown()
end

ok so i fixed the yield error with a sleep(0.5) but now it does not read the redstone input.
so when the reactor is hot i have a red signal coming in and the computer needs to see that so that the computer can
stop the running of the reactor.

current issue. stuck in loop.

concept. loop until redstone signal is received then stop output of redstone signal to reactor

edit:

this is the most current code but will be trying to fi as this topic unfolds
Grim Reaper #6
Posted 05 October 2013 - 01:21 AM
I'm not entirely how to solve your redstone problem, but I do have another suggestion. You restart your program by running the same program again using shell.run, which is a problem; every time you run a new program using the shell from a program, you add another 'program' to the stack of programs that the shell is keeping track of. Eventually, you'll cause the computer to crash because of what is known as stack overflow (you have too many entries in the stack which causes a crash).
deathvondoom #7
Posted 28 November 2013 - 05:32 PM
ok so i just need to know what loop and how to do it.
it just needs to look at a redstone input from a colored cable and then
keep checking the cable until a redstone signal is found.
once found have it ready to do another command, any help would be great