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

Need help with elseif in a queue!

Started by Arne303, 31 October 2012 - 12:45 PM
Arne303 #1
Posted 31 October 2012 - 01:45 PM
Hello!
I have a problem with a program:
A friend and I built an elevator with 5 storeys.
We're using "Redpower" mod for wiring.

The program is calling "elevator".
On the left side of a computer are 10 receivers and we'll like to code a program with an endless queue in it.
If the first wire (white) is activated some stuff shall happen.
If the second wire(orange) ist activated some other stuff shall happen.
… and so on.
We tried it with the following code:


-- 1 is ID of the first (white) wire
-- 2 is ID of the second (orange) wire
-- wire IDs going on with binary structure (1, 2, 4, 8, 16, 32, 64,128, 256, 512)
shell.run("clear")
print("Welcome to the ElevatorControl!")
while true do
if rs.getBundledInput("left") == "1" then
  --some stuff
elseif rs.getBundledInput("left") == "2" then
  --some more stuff
elseif rs.getBundledInput("left") == "4" then
  --some more stuff
elseif rs.getBundledInput("left") == "8" then
  --some more stuff
elseif rs.getBundledInput("left") == "16" then
  --some more stuff
elseif rs.getBundledInput("left") == "32" then
  --some more stuff
elseif rs.getBundledInput("left") == "64" then
  --some more stuff
elseif rs.getBundledInput("left") == "128" then
  --some more stuff
elseif rs.getBundledInput("left") == "256" then
  --some more stuff
elseif rs.getBundledInput("left") == "512" then
  --some more stuff

end
end

When for example, the first wire is activated the stuff after the codeline

if rs.getBundledIput("left") == "1"
shall be run.
After that the big queue shall be going on until there is another redstone input.


The problem:
After starting the program there is an error:

elevator:6: Too long without yielding

Another time it is:

elevator:9: Too long without yielding

Hope you'll understand and can give me suggestions why this happens"
Greetz from Germany!
jag #2
Posted 31 October 2012 - 02:10 PM
Wouldn't it be easier to use a variable for this? Would be smaller I think:
Spoiler
local rsIn = rs.getBundledInput("left")
if rsIn == "1" then
  --some stuff
elseif rsIn == "2" then
  --some more stuff
elseif rsIn == "4" then
  --some more stuff
elseif rsIn == "8" then
  --some more stuff
elseif rsIn == "16" then
  --some more stuff
elseif rsIn == "32" then
  --some more stuff
elseif rsIn == "64" then
  --some more stuff
elseif rsIn == "128" then
  --some more stuff
elseif rsIn == "256" then
  --some more stuff
elseif rsIn == "512" then
  --some more stuff
end
Now to the error:
The error you are getting is because for some reason (I don't really understand this, but) the code needs some pauses.
So just add sleep(.1) and it will be fine:
Spoiler

shell.run("clear")
print("Welcome to the ElevatorControl!")
while true do
	local rsIn = rs.getBundledInput("left")
	if rsIn == "1" then
		--some stuff
	elseif rsIn == "2" then
		--some more stuff
	elseif rsIn == "4" then
		--some more stuff
	elseif rsIn == "8" then
		--some more stuff
	elseif rsIn == "16" then
		--some more stuff
	elseif rsIn == "32" then
		--some more stuff
	elseif rsIn == "64" then
		--some more stuff
	elseif rsIn == "128" then
		--some more stuff
	elseif rsIn == "256" then
		--some more stuff
	elseif rsIn == "512" then
		--some more stuff
	end
    sleep(.1)
end
Cruor #3
Posted 31 October 2012 - 02:17 PM
You need to do something that yields, IE add sleep(0) <- sleep until next tick or os.pullEvent("redstone") at the start of the loop.(after while true do :P/>/>) the later waits for redstone(also RP bundleds, wires etc) to change on any side before running the code.
jag #4
Posted 31 October 2012 - 02:19 PM
A smarter way is to add
os.pullEvent("redstone")
And so it would be much more efficient:
Spoiler

shell.run("clear")
print("Welcome to the ElevatorControl!")
while true do
	local rsIn = rs.getBundledInput("left")
	if rsIn == "1" then
		--some stuff
	elseif rsIn == "2" then
		--some more stuff
	elseif rsIn == "4" then
		--some more stuff
	elseif rsIn == "8" then
		--some more stuff
	elseif rsIn == "16" then
		--some more stuff
	elseif rsIn == "32" then
		--some more stuff
	elseif rsIn == "64" then
		--some more stuff
	elseif rsIn == "128" then
		--some more stuff
	elseif rsIn == "256" then
		--some more stuff
	elseif rsIn == "512" then
		--some more stuff
	end
    os.pullEvent("redstone")
end
Arne303 #5
Posted 31 October 2012 - 06:12 PM
Thank You!
Now it works very well.
I found another mistake: The numbers 1,2,4 and so on must written without the " ".
Thank You again! :P/>/>