Jump to content




Airship control panel- Exiting functions too early


5 replies to this topic

#1 icecube45

  • Members
  • 38 posts

Posted 20 May 2013 - 06: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!

#2 danny_delmax1

  • Members
  • 18 posts

Posted 20 May 2013 - 07: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 by danny_delmax1, 21 May 2013 - 03:25 PM.


#3 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 20 May 2013 - 07:33 PM

View Postdanny_delmax1, on 20 May 2013 - 07:11 PM, said:

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!

#4 icecube45

  • Members
  • 38 posts

Posted 20 May 2013 - 07:41 PM

Thank you both! Did not see my mistake there, my brain must have been auto correcting it, trying your fix now!

#5 icecube45

  • Members
  • 38 posts

Posted 20 May 2013 - 07:47 PM

Thanks for the improvement,but my code still fails! Now it will print everything normally, but fails to send the signal...

#6 Molinko

  • Members
  • 54 posts

Posted 21 May 2013 - 11:03 AM

View Posticecube45, on 20 May 2013 - 07:47 PM, said:


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






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users