5 posts
Posted 10 April 2015 - 10:42 PM
Hey Guys,
I am a new user of computercraft and tried to write my first program (to farm sugercane)
so I wrote my programm:
I wanted it to start at the highest point of sugercane and farming all of this lanes. Then the turtle should go one lane down and do the same, finally put all into a chest (Not implemented yet)
and then wait about 200 seconds.
This is my (not working) Programm:
function fforward()
for a=0,l do
turtle.dig()
turtle.forward()
end
end
function back()
turtle.down()
turtle.turnRight()
for c=0,b do
turtle.forward()
end
turtle.turnRight()
end
function rechts()
turtle.turnRight()
turtle.dig()
turtle.forward()
turtle.dig()
turtle.forward()
turtle.turnRight()
end
function links()
turtle.turnLeft()
turtle.dig()
turtle.forward()
turtle.turnLeft()
end
o=b/2
function half()
for d = 0,d do
fforward()
rechts()
fforeward()
links()
end
back()
end
write("laenge des Feldes:")
l=tonumer(read())
write("breite des Feldes:")
b=tonumber(read())
i=0
while (i<0) do
half()
end
I hope you are able to help me and sorry for my bad english. :D/>
1023 posts
Posted 11 April 2015 - 11:15 AM
In your half function the second time you call fforward you spell it fforeward, which is nil.
Also in the half function you have a for loop, where you use the same variable for both the counter and target variable. I would assume you meant to use the o variable you just created instead of the second d there.
function half()
for d = 0, d do --# should the second d here be the o variable you just created?
fforward()
rechts
fforeward() --# this line should be "fforward()"
links()
end
back()
end
Edited on 11 April 2015 - 09:15 AM
5 posts
Posted 11 April 2015 - 12:44 PM
now I get a error: farm:34: attempt to perform arithmetic __div on nil and number
I am to bad for this..xD
1023 posts
Posted 11 April 2015 - 07:21 PM
You need to move the o = b/2 to somewhere below the b = tonumber(read())
When the program is called it is ran from top to bottom, so b is not set when you try to use it on that line.
5 posts
Posted 12 April 2015 - 12:39 PM
That sounds logic. :D/> Finally i changed the places from all the functions:
function rechts()
turtle.turnRight()
turtle.dig()
turtle.forward()
turtle.dig()
turtle.forward()
turtle.turnRight()
end
function links()
turtle.turnLeft()
turtle.dig()
turtle.forward()
turtle.turnLeft()
end
write("laenge des Feldes:")
L = tonumer(read())
write("breite des Feldes:")
b=tonumber(read())
o=b/2
function fforward()
for a=0,L do
turtle.dig()
turtle.forward()
end
end
function back()
turtle.down()
turtle.turnRight()
for c=0,b do
turtle.forward()
end
turtle.turnRight()
end
function half()
for o=0,d do
fforward()
rechts()
fforward()
links()
end
back()
end
i=0
while (i<0) do
half()
I am now getting an error at line 20: attempt to call nil
It is where i ask about the length from the field (L= tonumber(read())
8543 posts
Posted 12 April 2015 - 05:03 PM
You forgot the B in tonumber on that line.
5 posts
Posted 15 April 2015 - 05:58 PM
When I start the program now my turtle is just turning right all the time.. nothing else.. what did I wrong?
function rechts()
turtle.turnRight()
turtle.dig()
turtle.forward()
turtle.dig()
turtle.forward()
turtle.turnRight()
end
function links()
turtle.turnLeft()
turtle.dig()
turtle.forward()
turtle.turnLeft()
end
write("laenge des Feldes:")
L = tonumber(read())
write("breite des Feldes:")
b=tonumber(read())
o=b/2
k = 0
function fforward()
while (k < L) do
turtle.dig()
turtle.forward()
k=k+1
end
end
c = 0
function back()
turtle.down()
turtle.turnRight()
while (c > 0) do
turtle.forward()
c=c+1
end
turtle.turnRight()
end
d=0
function half()
while (d > o) do
fforward()
rechts()
fforward()
links()
d=d+1
end
back()
end
i=0
while (i<5) do
half()
i=i+1
end
3057 posts
Location
United States of America
Posted 15 April 2015 - 06:11 PM
Did you remember to refuel your turtle?
5 posts
Posted 15 April 2015 - 06:49 PM
yeah i did.. my fuel Lvl is 19775 :/
656 posts
Posted 15 April 2015 - 09:22 PM
Your code is hard to read. Could you give your variables more descriptive names?
Try printing all your variables before calling half() in you main loop.
3057 posts
Location
United States of America
Posted 15 April 2015 - 11:32 PM
Your code is hard to read. Could you give your variables more descriptive names?
Try printing all your variables before calling half() in you main loop.
The variables are in german, rechts means "right", links means "left", etc.
656 posts
Posted 15 April 2015 - 11:58 PM
Your code is hard to read. Could you give your variables more descriptive names?
Try printing all your variables before calling half() in you main loop.
The variables are in german, rechts means "right", links means "left", etc.
Yea, I know that. I was talking about "o", "k", "c", "b", "L" etc.
Edited on 15 April 2015 - 09:59 PM