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

Easy Fix Turtle Movement

Started by klikko, 12 June 2012 - 08:21 PM
klikko #1
Posted 12 June 2012 - 10:21 PM
Hi, this is probably a fundamental error, but I am unable to see what I am doing wrong.
Purpose of this code is for the turtle to simply move the entered number of blocks.

Instead of stopping when it is supposed to break, it continues forever.

Code I have tried using:

ToWalk = 0
Walked = 0
print("How far should the turtle walk?")
ToWalk = (io.read())
while true do
if Walked == ToWalk then
break
else
Walked = Walked + 1
turtle.forward()
end
end
OmegaVest #2
Posted 12 June 2012 - 10:53 PM
This might be easier done with a For loop. So, instead of what you have, use something like this:


tArgs = {...}
toWalk = tArgs[1]

for x = 1, toWalk do
   if not turtle.forward() then
	  print ("Error, cannot move forward")
	  break
   end
end


Also, this allows you to just type the program name and a number, and it goes. Simpler than having a read call. If you wanted a read call, then you would need to dump the first two lines and use this:


term.clear()
x1, y1 = term.getSize()
x1 = x1 / 2
y1 = y1 / 2

term.setCursorPos(x1-16, y1-1)
term.write("How far should the turtle walk?")
term.setCursorPos(x1-5, y1)
toWalk = tonumber(io.read())

And that should give you a clean UI to work with, as well. I would, however, suggest putting term.clear() and term.setCursorPos(1,1) at the end of that script, just to clear the screen.
klikko #3
Posted 12 June 2012 - 11:53 PM
Ah thank you for your response, I will try this out soon :(/>/>
Bossman201 #4
Posted 13 June 2012 - 03:52 AM

x1, y1 = term.getSize()
Not sure why everyone is so obsessed with this command. Screen size is 50 x 18. Isn't that enough to make a clean UI?

EDIT: I guess it would be useful for attached monitors, but I usually just write programs for a standalone computer. (with a modem and possibly a disk drive.)
archit #5
Posted 13 June 2012 - 01:03 PM
Hi, this is probably a fundamental error, but I am unable to see what I am doing wrong.
Purpose of this code is for the turtle to simply move the entered number of blocks.

Instead of stopping when it is supposed to break, it continues forever.

Code I have tried using:

ToWalk = 0
Walked = 0
print("How far should the turtle walk?")
ToWalk = (io.read()) – should be ToWalk = tonumber(io.read())
while true do
if Walked == ToWalk then
break
else
Walked = Walked + 1
turtle.forward()
end
end

edit's are listed as comments in above quote
Cloudy #6
Posted 13 June 2012 - 02:10 PM

x1, y1 = term.getSize()
Not sure why everyone is so obsessed with this command. Screen size is 50 x 18. Isn't that enough to make a clean UI?

EDIT: I guess it would be useful for attached monitors, but I usually just write programs for a standalone computer. (with a modem and possibly a disk drive.)

SSP supports custom terminal sizes - so its always easier and more accurate to do term.getSize().
Lyqyd #7
Posted 13 June 2012 - 06:54 PM

x1, y1 = term.getSize()
Not sure why everyone is so obsessed with this command. Screen size is 50 x 18. Isn't that enough to make a clean UI?

EDIT: I guess it would be useful for attached monitors, but I usually just write programs for a standalone computer. (with a modem and possibly a disk drive.)

SSP supports custom terminal sizes - so its always easier and more accurate to do term.getSize().

In addition, turtles (which this program is written for!), computers and monitors of various sizes all have different dimensions by default.
OmegaVest #8
Posted 13 June 2012 - 07:11 PM
Why is one of my quirks being repetitiously scrutinized for merit?

Bossman: Taking anything you did not write for granted in coding is folly. I found that out when I tried to use a teacher's keyboard listener and had to reprogram a part of it to make it work. Found out later that was part of the test, though.

Cloudy: Keep doing awesome.

Lyqyd: You have a really weird name, man.

klikko My code had one error. archit actually commented on it.

There, I think I mentioned everyone in this thread.