Jump to content




While Loop Repeating Only Once

turtle computer peripheral

4 replies to this topic

#1 DARKHAWX

  • Members
  • 23 posts

Posted 24 February 2014 - 07:14 AM

I have no idea why this happens or how it happens but my while loop will no repeat itself. For some reason it will only repeat itself once and once only. Here is the code I am using:
--Variables
local x = 1
local y = 1
local dir = 270
local monitor = peripheral.wrap("bottom")
local modem = peripheral.wrap("back")
local completed = false
local x1, x2, x3, x4, x5, y1, y2, y3, y4, y5, dir1, dir2, dir3, dir4, dir5 = 1

--Print Title
monitor.clear()
monitor.setTextScale(2)
monitor.setCursorPos(10, 1)
monitor.write("Turtle Maze")
monitor.setCursorPos(1, 2)
monitor.write("==============================")
monitor.setCursorPos(1, 3)
monitor.write("Coords:")
monitor.setCursorPos(11, 3)
monitor.write("Direction:")
monitor.setCursorPos(24, 3)
monitor.write("Fuel:")

--Print Coords and Direction
while true do
  modem.open(1)
  local event1, modemSide1, senderChannel1, replyChannel1, message1, senderDistance1 = os.pullEvent("modem_message")
  x = message1
  modem.open(2)
  local event2, modemSide2, senderChannel2, replyChannel2, message2, senderDistance2 = os.pullEvent("modem_message")
  y = message2
  monitor.setCursorPos(1, 4)
  monitor.write("X:"..x.." Y:"..y)

  modem.open(3)
  local event3, modemSide3, senderChannel3, replyChannel3, message3, senderDistance3 = os.pullEvent("modem_message")
  dir = message3
  monitor.setCursorPos(11, 4)
  if dir == 0 then
	dir = "Right"
  elseif dir == 90 then
	dir = "Up"
  elseif dir == 180 then
	dir = "Left"
  else
	dir = "Down"
  end
  monitor.write(dir)

  --Print Previous Coords and Directions
	--Dir
  dir5 = dir4
  dir4 = dir3
  dir3 = dir2
  dir2 = dir1
  dir1 = dir

  monitor.setCursorPos(11, 5)
  monitor.write(dir1)
  monitor.setCursorPos(11, 6)
  monitor.write(dir2)
  monitor.setCursorPos(11, 7)
  monitor.write(dir3)
  monitor.setCursorPos(11, 8)
  monitor.write(dir4)
  montior.setCursorPos(11, 9)
  monitor.write(dir5)

	--X, Y
  x5 = x4
  x4 = x3
  x3 = x2
  x2 = x1
  x1 = x

  y5 = y4
  y4 = y3
  y3 = y2
  y2 = y1
  y1 = y

  monitor.setCursorPos(1, 5)
  monitor.write("X: "..x1.."  Y: "..y1)
  monitor.setCursorPos(1, 6)
  monitor.write("X: "..x2.."  Y: "..y2)
  monitor.setCursorPos(1, 7)
  monitor.write("X: "..x3.."  Y: "..y3)
  monitor.setCursorPos(1, 8)
  monitor.write("X: "..x4.."  Y: "..y4)
  monitor.setCursorPos(1, 9)
  monitor.write("X: "..x5.."  Y: "..y5)

  completed = false
end
See if you guys can figure it out.

#2 Thib0704

  • Members
  • 93 posts
  • LocationgetServer().getPlayer("Thib0704").getLocation();

Posted 24 February 2014 - 10:07 AM

This topic belongs to ask a pro.

Ontopic:
Error
montior.write() -> monitor.write()

Can you please explain what you are trying to do?


View PostDARKHAWX, on 24 February 2014 - 07:14 AM, said:

I have no idea why this happens or how it happens but my while loop will no repeat itself. For some reason it will only repeat itself once and once only. Here is the code I am using:
--Variables
local x = 1
local y = 1
local dir = 270
local monitor = peripheral.wrap("bottom")
local modem = peripheral.wrap("back")
local completed = false
local x1, x2, x3, x4, x5, y1, y2, y3, y4, y5, dir1, dir2, dir3, dir4, dir5 = 1

--Print Title
monitor.clear()
monitor.setTextScale(2)
monitor.setCursorPos(10, 1)
monitor.write("Turtle Maze")
monitor.setCursorPos(1, 2)
monitor.write("==============================")
monitor.setCursorPos(1, 3)
monitor.write("Coords:")
monitor.setCursorPos(11, 3)
monitor.write("Direction:")
monitor.setCursorPos(24, 3)
monitor.write("Fuel:")

--Print Coords and Direction
while true do
  modem.open(1)
  local event1, modemSide1, senderChannel1, replyChannel1, message1, senderDistance1 = os.pullEvent("modem_message")
  x = message1
  modem.open(2)
  local event2, modemSide2, senderChannel2, replyChannel2, message2, senderDistance2 = os.pullEvent("modem_message")
  y = message2
  monitor.setCursorPos(1, 4)
  monitor.write("X:"..x.." Y:"..y)

  modem.open(3)
  local event3, modemSide3, senderChannel3, replyChannel3, message3, senderDistance3 = os.pullEvent("modem_message")
  dir = message3
  monitor.setCursorPos(11, 4)
  if dir == 0 then
	dir = "Right"
  elseif dir == 90 then
	dir = "Up"
  elseif dir == 180 then
	dir = "Left"
  else
	dir = "Down"
  end
  monitor.write(dir)

  --Print Previous Coords and Directions
	--Dir
  dir5 = dir4
  dir4 = dir3
  dir3 = dir2
  dir2 = dir1
  dir1 = dir

  monitor.setCursorPos(11, 5)
  monitor.write(dir1)
  monitor.setCursorPos(11, 6)
  monitor.write(dir2)
  monitor.setCursorPos(11, 7)
  monitor.write(dir3)
  monitor.setCursorPos(11, 8)
  monitor.write(dir4)
  montior.setCursorPos(11, 9)
  monitor.write(dir5)

	--X, Y
  x5 = x4
  x4 = x3
  x3 = x2
  x2 = x1
  x1 = x

  y5 = y4
  y4 = y3
  y3 = y2
  y2 = y1
  y1 = y

  monitor.setCursorPos(1, 5)
  monitor.write("X: "..x1.."  Y: "..y1)
  monitor.setCursorPos(1, 6)
  monitor.write("X: "..x2.."  Y: "..y2)
  monitor.setCursorPos(1, 7)
  monitor.write("X: "..x3.."  Y: "..y3)
  monitor.setCursorPos(1, 8)
  monitor.write("X: "..x4.."  Y: "..y4)
  monitor.setCursorPos(1, 9)
  monitor.write("X: "..x5.."  Y: "..y5)

  completed = false
end
See if you guys can figure it out.


#3 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 24 February 2014 - 10:22 AM

Moved to Ask a Pro.

#4 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 24 February 2014 - 05:00 PM

You're likely better off opening all relevant channels before starting your "while" loop.

Where's the other script?

What does THIS script do instead of repeating? Does it eg get stuck waiting for a modem message to come in?

You could re-write chunks like this:

  local event1, modemSide1, senderChannel1, replyChannel1, message1, senderDistance1 = os.pullEvent("modem_message")
  x = message1
  modem.open(2)
  local event2, modemSide2, senderChannel2, replyChannel2, message2, senderDistance2 = os.pullEvent("modem_message")
  y = message2

... as just this:

  local event, modemSide, senderChannel, replyChannel, x, senderDistance = os.pullEvent("modem_message")
  modem.open(2)
  local event, modemSide, senderChannel, replyChannel, y, senderDistance = os.pullEvent("modem_message")


#5 ElvishJerricco

  • Members
  • 803 posts

Posted 28 February 2014 - 07:33 PM

The only thing that makes sense is that it's stuck waiting for a modem message. Try printing some debug info to the terminal. For example, before each os.pullEvent call, use print("Waiting for modem message on channel 1") so that you can tell where execution is stopping.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users