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

How to make a turtle move more than 1 space?

Started by calmilamsy, 20 April 2013 - 08:26 AM
calmilamsy #1
Posted 20 April 2013 - 10:26 AM
i started making a prompt for how many blocks you go forward so i made this:-


x=io.read()
if x == "w" then
n=io.read()
turtle.forward(tonumber(n))
end

problem:- it only goes forward once instead of the told amount of times

help would be appreciated :)/>
SuicidalSTDz #2
Posted 20 April 2013 - 10:28 AM
CC reveals its own read function, use it. Also, Lua is case sensitive: I'll give you the benefit of the doubt and say the editor made is capitalized.

x = read()
if x == "w" then
 b = read()
 turtle.forward(tonumber(B)/>/>)
end

As for looping:

x = read() if x == "w" then  
 num = read()
 for i = 1,tonumber(num) do
  turtle.forward()
 end
end
Kingdaro #3
Posted 20 April 2013 - 10:29 AM
The turtle.forward command does not take a number. You have to use a for loop to repeat it that number of times.


x = read()
if x == "w" then
  n = read()
  for i=1, tonumber(n) do
    turtle.forward()
  end
end
SuicidalSTDz #4
Posted 20 April 2013 - 10:32 AM
Yeah, I realized that after thinking about it. *noob of turtles here :P/>*

Of course, you could overwrite the old turtle.forward function and use your own
remiX #5
Posted 20 April 2013 - 10:32 AM
Lol SuicidalSTDz, nice edit there ;)/> Was just about to quote you :[

If you want n to have a default value if the user does not input a valid number, do this:
n = tonumber(read()) or 1 -- default will be 1 move
for i = 1, n do turtle.forward() end
-- etc..
SuicidalSTDz #6
Posted 20 April 2013 - 10:34 AM
Lol SuicidalSTDz, nice edit there ;)/> Was just about to quote you :[
I am what one may call, a 'ninja' :ph34r:/>
calmilamsy #7
Posted 21 April 2013 - 01:08 AM
none of these won't work, if you want the code look HERE
theoriginalbit #8
Posted 21 April 2013 - 01:11 AM
none of these won't work, if you want the code look HERE
Has the turtle got fuel in it?
calmilamsy #9
Posted 21 April 2013 - 02:43 AM
yes it has 21200 fuel
Skampi #10
Posted 21 April 2013 - 03:19 AM
i started making a prompt for how many blocks you go forward so i made this:-


x=io.read()
if x == "w" then
n=io.read()
turtle.forward(tonumber(n))
end

problem:- it only goes forward once instead of the told amount of times

help would be appreciated :)/>


x=io.read()
if x == "w" then
n=io.read()
for n = n, 0, -1 do
turtle.forward()
end
else
sleep(2)
end

This is probably the way i would do it. :)/>
theoriginalbit #11
Posted 21 April 2013 - 03:23 AM

x=io.read()
if x == "w" then
n=io.read()
for n = n, 0, -1 do
turtle.forward()
end
else
sleep(2)
end
This is probably the way i would do it. :)/>
Why not just

local n = tonumber(read())
for i = 1, n do
  turtle.forward()
end
instead of your odd and un-required backwards for loop.
Skampi #12
Posted 21 April 2013 - 03:30 AM

x=io.read()
if x == "w" then
n=io.read()
for n = n, 0, -1 do
turtle.forward()
end
else
sleep(2)
end
This is probably the way i would do it. :)/>
Why not just

local n = tonumber(read())
for i = 1, n do
  turtle.forward()
end
instead of your odd and un-required backwards for loop.

That's also a possibilty, i don't think it would change anything. :D/>
theoriginalbit #13
Posted 21 April 2013 - 03:33 AM
That's also a possibilty, i don't think it would change anything. :D/>
Well considering you're also missing the tonumber, mine would actually work ;)/> :P/>
Skampi #14
Posted 21 April 2013 - 03:34 AM
That's also a possibilty, i don't think it would change anything. :D/>
Well considering you're also missing the tonumber, mine would actually work ;)/> :P/>

I don't know why the tonumber is required, explanation please. :)/>
theoriginalbit #15
Posted 21 April 2013 - 03:37 AM
I don't know why the tonumber is required, explanation please. :)/>
Getting input from the user is returned as a string of characters. For loops require numbers. So using the input of a user at any point in something that requires a number, means you have to call the tonumber function, which converts a string to a number.
Skampi #16
Posted 21 April 2013 - 03:40 AM
I don't know why the tonumber is required, explanation please. :)/>
Getting input from the user is returned as a string of characters. For loops require numbers. So using the input of a user at any point in something that requires a number, means you have to call the tonumber function, which converts a string to a number.

Well, ok, understood, thanks then \o/