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

what is wrong with my code

Started by FlammenWerfer35, 07 February 2014 - 10:08 PM
FlammenWerfer35 #1
Posted 07 February 2014 - 11:08 PM

for i = 4 do
  turtle.place(1)
  turtle.select(2)
  turtle.place(2)
  turtle.select(1)
end

while turtle.detect() do
  turtle.up()
if turtle.detectUp() == true do
   turtle.digUp()
end
else
  turtle.down()
  turtle.dig()
end

i'm trying to make a tree farm
Bomb Bloke #2
Posted 07 February 2014 - 11:53 PM
I recommend reading up on how "for" loops work, then about if/then/else.

After that, take another look at the turtle API - in particular, note that "turtle.place()" doesn't take any parameters (Edit: Well, unless you're trying to write a sign with it…). It simply tries to place one item from whatever slot you activated with "turtle.select()".
Edited on 07 February 2014 - 10:54 PM
theoriginalbit #3
Posted 08 February 2014 - 12:11 AM
I also suggest reading this, Read this before asking questions and the section below. Your post is a little lacking, in particular if there are error messages you should tell them, or if it is unexpected results you should state what you expect and what is actually happening. By doing this you'll get quicker and more reliable responses from us. However in this case luckily you have obvious mistakes and a short program, so it is not too much of a problem, please do keep this in mind for future posts however.
SkyRamon #4
Posted 08 February 2014 - 05:56 AM
Your loop is incorrect i is not set to 4 so the output will be false.

heres a code that might work.


while true do
  turtle.select(1)
  turtle.place()
  turtle.select(2)
  turtle.place()
  turtle.select(1)
  if turtle.detect() then
  turtle.up()
  else if turtle.detectUp() then
  turtle.digUp()
  else
  turtle.down()
  turtle.dig()
  end
end
end
TheOddByte #5
Posted 08 February 2014 - 07:31 AM
1: The for loop is incorrect, Here's an example of how to use it

for i = <start value>, <finish value>, <increment/decrement value> do
    -- Code
end
Note: The third parameter is often only needed when decrementing( See example below )

for i = 10, 1, -1 do
    print( i )
end

2: You're using if/elseif wrong, See example how to use it below

if 2 == 1 then --# If 2 is equal to 1
    -- Code

elseif 2 == 0 then --# If 2 is equal to 0 instead
    -- Code

else --# If both of the statements above is false
    -- Code
end

Also, A tip with booleans

if turtle.detect() == true then
    -- Code
end
Can be this instead

if turtle.detecUp() then --# If it's true
    -- Code
elseif not turtle.detectUp() then --# If it's false
    -- Code
end

So with that said you should try to fix the code yourself to see if you understood what I just wrote :P/>
theoriginalbit #6
Posted 08 February 2014 - 08:46 PM
Can be this instead

if turtle.detecUp() then --# If it's true
	-- Code
elseif not turtle.detectUp() then --# If it's false
	-- Code
end
No point doing the elseif, you should know that!

if turtle.detectUp() then --# if it's true
  --# code
else --# if it's false
  --# code
end
Edited on 08 February 2014 - 07:46 PM
TheOddByte #7
Posted 08 February 2014 - 09:59 PM
No point doing the elseif, you should know that!

if turtle.detectUp() then --# if it's true
  --# code
else --# if it's false
  --# code
end
Yeah I know that, But I did that because I wanted to show how to use booleans
I were trying to explain that he could use not to check if it's false :P/>
Edited on 08 February 2014 - 09:02 PM