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

Airship control panel- Exiting functions too early

Started by icecube45, 20 May 2013 - 05:43 PM
icecube45 #1
Posted 20 May 2013 - 07:43 PM

function menu()
term.clear()
term.setCursorPos(1,1)
print("Airship Control")
print("Options: Left, Right, Back, foward, Up, Down")
print("Choose a Direction:")
direction = read()
print("Choose an amount, leave nil for infinite")
value = read()
if direction == "left" or direction == "Left" then
  parallel.waitForAll(repeatcontrol, left)
end
if direction == "right" or direction == "Right" then
  parallel.waitForAll(repeatcontrol, right)
end
if direction == "foward" or direction == "Foward" then
  parallel.waitForAll(repeatcontrol, foward)
end
if direction == "back" or direction == "Back" then
  parallel.waitForAll(repeatcontrol, back)
end
if direction == "up" or direction == "Up" then
  parallel.waitForAll(repeatcontrol, up)
end
if direction == "down" or direction == "Down" then
  parallel.waitForAll(repeatcontrol, down)
end
end
left = function()
print("Press Any Button To Stop")
while dorepeat == true do
  c = colors.combine(c, colors.blue)
  rs.setBundledOutput("back", c)
  os.sleep(3)
  c = colors.subtract(c, colors.blue)
  rs.setBundledOutput("back", c)
end
end
right = function()
print("Press Any Button To Stop")
while dorepeat == true do
  c = colors.combine(c, colors.white)
  rs.setBundledOutput("back", c)
  os.sleep(3)
  c = colors.subtract(c, colors.white)
  rs.setBundledOutput("back", c)
end
end
back = function()
print("Press Any Button To Stop")
while dorepeat == true do
  c = colors.combine(c, colors.lime)
  rs.setBundledOutput("back", c)
  os.sleep(3)
  c = colors.subtract(c, colors.lime)
  rs.setBundledOutput("back", c)
end
end

foward = function()
print("Press Any Button To Stop")
while dorepeat == true do
  c = colors.combine(c, colors.yellow)
  rs.setBundledOutput("back", c)
  os.sleep(3)
  c = colors.subtract(c, colors.yellow)
  rs.setBundledOutput("back", c)
end
end

up = function()
print("Press Any Button To Stop")
while dorepeat == true do
  c = colors.combine(c, colors.red)
  rs.setBundledOutput("back", c)
  os.sleep(3)
  c = colors.subtract(c, colors.red)
  rs.setBundledOutput("back", c)
end
end
down = function()
print("Press Any Button To Stop")
while dorepeat == true do
  c = colors.combine(c, colors.orange)
  rs.setBundledOutput("back", c)
  os.sleep(3)
  c = colors.subtract(c, colors.orange)
  rs.setBundledOutput("back", c)
end
end
repeatcontrol = function()
dorepeat = "true"
while dorepeat == true do
  if os.pullEvent("key") then
   dorepeat = "false"
  end
end
end
 

while true do
menu()
end


Hello! My functions do not wait for a keypress before closing.. Ignore the amount value, I've not implemented it yet.
Thank you!
danny_delmax1 #2
Posted 20 May 2013 - 08:11 PM
Just by glancing at it, I see this:

dorepeat = "true"
while dorepeat == true do
  if os.pullEvent("key") then
   dorepeat = "false"
  end
end
This is incorrect because its assigning dorepeat to a string, then checking if its the Boolean value true
Here are two correct solutions:

Using strings

dorepeat = "true"
while dorepeat == "true" do
  if os.pullEvent("key") then
   dorepeat = "false"
  end
end

Boolean only

dorepeat = true
while dorepeat == true do
  if os.pullEvent("key") then
   dorepeat = false
  end
end

Also, a code improvement:


replace
direction = read()
with
direction = string.lower(read())
This way, it will always be a lowercase, meaning you can replace:

if direction == "left" or direction == "Left" then
  parallel.waitForAll(repeatcontrol, left)
end
if direction == "right" or direction == "Right" then
  parallel.waitForAll(repeatcontrol, right)
end
if direction == "foward" or direction == "Foward" then
  parallel.waitForAll(repeatcontrol, foward)
end
if direction == "back" or direction == "Back" then
  parallel.waitForAll(repeatcontrol, back)
end
if direction == "up" or direction == "Up" then
  parallel.waitForAll(repeatcontrol, up)
end
if direction == "down" or direction == "Down" then
  parallel.waitForAll(repeatcontrol, down)
end


with the slightly easier to read and code


if direction == "left" then
  parallel.waitForAll(repeatcontrol, left)

elseif direction == "right" then
  parallel.waitForAll(repeatcontrol, right)

elseif direction == "foward" then
  parallel.waitForAll(repeatcontrol, foward)

elseif direction == "back" then
  parallel.waitForAll(repeatcontrol, back)

elseif direction == "up" then
  parallel.waitForAll(repeatcontrol, up)

elseif direction == "down" then
  parallel.waitForAll(repeatcontrol, down)
else
  print("Unknown word")
end

Notice how instead of multiple if statements, I put 1 if statement with elseifs, allowing use of the "else" if they input something wrong

Edit: Thanks for pointing out the error
Edited on 21 May 2013 - 02:25 PM
H4X0RZ #3
Posted 20 May 2013 - 08:33 PM
Just by glancing at it, I see this:

dorepeat = "true"
while dorepeat == true do
  if os.pullEvent("key") then
   dorepeat = "false"
  end
end
This is incorrect because its assigning dorepeat to a string, then checking if its the Boolean value true
Here are two correct solutions:

Using strings

dorepeat = "true"
while dorepeat == "true" do
  if os.pullEvent("key") then
   dorepeat = "false"
  end
end

Boolean only

dorepeat = true
while dorepeat == true do
  if os.pullEvent("key") then
   dorepeat = false
  end
end

Also, a code improvement:


replace
direction = read()
with
string.lower(direction) = read()
This way, it will always be a lowercase, meaning you can replace:

if direction == "left" or direction == "Left" then
  parallel.waitForAll(repeatcontrol, left)
end
if direction == "right" or direction == "Right" then
  parallel.waitForAll(repeatcontrol, right)
end
if direction == "foward" or direction == "Foward" then
  parallel.waitForAll(repeatcontrol, foward)
end
if direction == "back" or direction == "Back" then
  parallel.waitForAll(repeatcontrol, back)
end
if direction == "up" or direction == "Up" then
  parallel.waitForAll(repeatcontrol, up)
end
if direction == "down" or direction == "Down" then
  parallel.waitForAll(repeatcontrol, down)
end


with the slightly easier to read and code


if direction == "left" then
  parallel.waitForAll(repeatcontrol, left)

elseif direction == "right" then
  parallel.waitForAll(repeatcontrol, right)

elseif direction == "foward" then
  parallel.waitForAll(repeatcontrol, foward)

elseif direction == "back" then
  parallel.waitForAll(repeatcontrol, back)

elseif direction == "up" then
  parallel.waitForAll(repeatcontrol, up)

elseif direction == "down" then
  parallel.waitForAll(repeatcontrol, down)
else
  print("Unknown word")
end

Notice how instead of multiple if statements, I put 1 if statement with elseifs, allowing use of the "else" if they input something wrong
You have a typo :)/>

It's

direction = string.lower(read())

If you do it like your improvement, the name of the variable is lower, not the content of the read function!
icecube45 #4
Posted 20 May 2013 - 08:41 PM
Thank you both! Did not see my mistake there, my brain must have been auto correcting it, trying your fix now!
icecube45 #5
Posted 20 May 2013 - 08:47 PM
Thanks for the improvement,but my code still fails! Now it will print everything normally, but fails to send the signal…
Molinko #6
Posted 21 May 2013 - 12:03 PM
while dorepeat == true do
if os.pullEvent("key") then
dorepeat = "false"
end
end
I dont think your supposed to use os.pullEvent like that.. try this

while dorepeat == true do
   local key = os.pullEvent("key")
	    dorepeat = false
end