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

[SOLVED] [Lua] Door toggle for mulitple doors -not working-

Started by zedalius, 25 March 2012 - 07:08 AM
zedalius #1
Posted 25 March 2012 - 09:08 AM
Hey, im trying to combine some codes with my very basic Lua skills.
Its simply a door toggle, i found a code in here somewhere i tried to modify so i could use arrows instead of haveing to write it.

but it keeps failing on me.

problem:
it start fine, but whenever i try to use my arrows its puts "Select a door to toggle: (using UP/down arrows)" in between "—Door Controller—" and "——"..
Here is the code:

shell.run("clear");
local x, y =term.getCursorPos()
term.clearLine()
s = {colors.white, colors.red, colors.yellow};
o = {"Closed", "Closed", "Closed"};
sn = {"Door 1", "Door 2", "Door 3"};
cs = 1;
co = "closed";
master = 0;
while true do
shell.run("clear");
-
master = 0;
print("--------Door Controller-------\n");
print("------------------------------\n");
for i=1, 3 do
status = o[i];
if(o[i] == "Open") then
status = status.." ";
master = master + s[i];
end
rs.setBundledOutput("left", master);
print(i.." - "..sn[i].."  :  "..status);
end
sValid = true;
while sValid do
write("\nSelect a door to toggle: (using UP/down arrows) ");
term.setCursorPos(x, y)
a, b=os.pullEvent()
while a~="key" do a, b=os.pullEvent()
end
if b == 200 and cs == 3 then
cs=2
else
if b == 200 and cs = 2 then
cs=1
else
if b == 208 and cs = 1 then
cs=2
else
if b == 208 and cs = 2 then
cs=3
if b == 57 then
co=o[cs];
if(co == "Open") then
o[cs] = "Closed";
else
o[cs] = "Open";
end
end
end
end
end
end
end
end
kamnxt #2
Posted 25 March 2012 - 10:47 AM
Fixed, i also added some ">" and "<" characters so you know what is selected. I also changed the if … else if to if .. elseif and "while b ~= "key" do a,b = os.pullEvent()" to "os.pullEvent("key")". :(/>/>


shell.run("clear");
local x, y =term.getCursorPos()
term.clearLine()
s = {colors.white, colors.red, colors.yellow};
o = {"Closed", "Closed", "Closed"};
sn = {"Door 1", "Door 2", "Door 3"};
cs = 1;
co = "closed";
master = 0;
while true do
shell.run("clear");
master = 0;
print("--------Door Controller-------n");
print("------------------------------n");
for i=1, 3 do
  status = o[i];
  if(o[i] == "Open") then
   status = status.."  ";
   master = master + s[i];
  end
rs.setBundledOutput("left", master);
if i == cs then
print(">"..i.." - "..sn[i].."  :  "..status.."<");
else
print(" "..i.." - "..sn[i].."  :  "..status.." ");
end
end
sValid = true;
--while sValid do
  write("nSelect a door to toggle: (using UP/down arrows) ");
  term.setCursorPos(x, y)
  a, b=os.pullEvent("key")
--end
if b == 200 and cs > 1 then
  cs = cs - 1
elseif b == 208 and cs < 3 then
  cs = cs + 1
elseif b == 57 then
  co=o[cs];
  if(co == "Open") then
   o[cs] = "Closed";
  else
   o[cs] = "Open";
  end
end
end
zedalius #3
Posted 25 March 2012 - 10:56 AM
Fixed, i also added some ">" and "<" characters so you know what is selected. I also changed the if … else if to if .. elseif and "while b ~= "key" do a,b = os.pullEvent()" to "os.pullEvent("key")". :(/>/>

Your code gives me: bios.206: [string "zdoorcontrol"]:55: 'end' expected (to close 'while' at line 13)
tried to add an extra end in the end of your code, but then i got the same problem as in the code i posted.


Edit:
Nvm, i overlooked something in your code, but know it dosent allow me to use the the arrow keys, it is just stuck on no. 1.
and after 10 sec or so it gives me:
bios:32: Too long without yielding-
kamnxt #4
Posted 25 March 2012 - 11:02 AM
It works on my computer… Did you edit something?
zedalius #5
Posted 25 March 2012 - 11:08 AM
It works on my computer… Did you edit something?
I just double checked, and i did not overlook anything. If so, im a big noob.
Will keep checking, but im 90% sure that i didnt.
kamnxt #6
Posted 25 March 2012 - 11:14 AM
I don't get any errors…
Can you try this code?


shell.run("clear");
local x, y =term.getCursorPos()
term.clearLine()
s = {colors.white, colors.red, colors.yellow};
o = {"Closed", "Closed", "Closed"};
sn = {"Door 1", "Door 2", "Door 3"};
cs = 1;
co = "closed";
master = 0;
while true do
shell.run("clear");
master = 0;
print("--------Door Controller-------n");
print("------------------------------n");
for i=1, 3 do
  status = o[i];
  if(o[i] == "Open") then
   status = status.."  ";
   master = master + s[i];
  end
rs.setBundledOutput("left", master);
if i == cs then
print(">"..i.." - "..sn[i].."  :  "..status.."<");
else
print(" "..i.." - "..sn[i].."  :  "..status.." ");
end
end
write("nSelect a door to toggle: (using UP/down arrows) ");
term.setCursorPos(x, y)
a, b=os.pullEvent("key")
if b == 200 and cs > 1 then
  cs = cs - 1
elseif b == 208 and cs < 3 then
  cs = cs + 1
elseif b == 57 then
  co=o[cs];
  if(co == "Open") then
   o[cs] = "Closed";
  else
   o[cs] = "Open";
  end
end
end
Btw, did you copy/paste the program?
Espen #7
Posted 25 March 2012 - 11:16 AM
@zedalius:
Are you playing on SMP without being an admin, or do you play on SSP when trying to use this code?
If it's the latter, are you manually typing the program into CC using the "edit" program?
zedalius #8
Posted 25 March 2012 - 11:22 AM
@zedalius:
Are you playing on SMP without being an admin, or do you play on SSP when trying to use this code?
If it's the latter, are you manually typing the program into CC using the "edit" program?
im playing on SMP without being admin.
and im using the in-game editing program.
zedalius #9
Posted 25 March 2012 - 11:24 AM
I don't get any errors…
Can you try this code?

...
Btw, did you copy/paste the program?
That works :(/>/>
- No i did not.

Thank you so much :)/>/>