First attempt at turtle code.......did not work ??

  • Please make sure you are posting in the correct place. Server ads go here and modpack bugs go here
  • The FTB Forum is now read-only, and is here as an archive. To participate in our community discussions, please join our Discord! https://ftb.team/discord

Calibra

New Member
Jul 29, 2019
38
0
0
Hi
This is my firstbattempt at some simole turtle code.....but didnt work, cant see why...help please


local Length=20

local Width=30


For a,1,Length do
turtle.forward()

End

turtle.turnRight()

For a = 1, Width do
turtle.forward()

End


turtle.turnRight()

For a = 1,Length do
turtle.forward()

End


turtle.turnRight()

For a = 1, Width do

turtle.forward()

End

Thanks
 

PeggleFrank

New Member
Jul 29, 2019
928
0
0
(Coming from somebody who barely understands print("hello world"))

I'm pretty sure you don't capitalize. Might be wrong though.

EDIT: The variables are fine. Everything else, uncapitalize. Except for the second word in a (insert code term here), that needs to be capitalized.

Example:
for a = 1, Length do
turtle.forward()

end

turtle.turnRight()

You get the idea.
 

Calibra

New Member
Jul 29, 2019
38
0
0
Re error 338 string move = expected

i changed the first line, as i missed out the `=` sign, but still no go

local Length=20
local Width=30
For a = 1,Length do
turtle.forward()
End
turtle.turnRight()
For a = 1, Width do
turtle.forward()
End
turtle.turnRight()
For a = 1,length do
turtle.forward()
End
turtle.turnRight()
For a = 1, width do
turtle.forward()
End

P.S. Is it important to have spaces each side of the = sign ? i tried both ways with same error ???
 

gattsuru

Well-Known Member
May 25, 2013
364
103
68
Also, note that turtles -- at least by default settings -- require fuel to move, and you must tell them to consume which slot holds fuel and to consume that fuel. It's also better for code cleanliness to do avoid repeating sections of code, even if this means using nested loops. For example, and assuming you put fuel like coal or wood into Slot 1:
Code:
local fuelSlot = 1
local fuelToConsume = 10
local Length = 20
local Width = 30
local halfCirclesToDo = 2

local fuelNeeded = (Length + Width) * halfCirclesToDo

turtle.select(fuelSlot)
turtle.refuel(fuelToConsume)
print("Will be moving "..fuelNeeded.." squares.")
if(turtle.getFuelLevel() < fuelNeeded) then
  print("No Fuel!")
  end

for i = 1, halfCirclesToDo do

  for a = 1, Length do
    turtle.forward()
    end
  
  turtle.turnRight()

  for a = 1, Width do
    turtle.forward()
    end

  turtle.turnRight()
end

P.S. Is it important to have spaces each side of the = sign ? i tried both ways with same error ???
No. LUA does not care about spaces, paragraphs, and most typographical matters. It's a good typographical habit to get into, as is indenting loops or if statements, but I'm hard-pressed to think of any situation where it could cause a serious error on its own. Linebreaks or spaces in the middle of variables or method names, obviously, will cause problems, however.

It is very, very sensitive to case (aka capitalization), and specifically lower camelCase in most default APIs.
 
  • Like
Reactions: KirinDave

whd23

New Member
Jul 29, 2019
111
0
1
Re error 338 string move = expected

i changed the first line, as i missed out the `=` sign, but still no go

local Length=20
local Width=30
For a = 1,Length do
turtle.forward()
End
turtle.turnRight()
For a = 1, Width do
turtle.forward()
End
turtle.turnRight()
For a = 1,length do
turtle.forward()
End
turtle.turnRight()
For a = 1, width do
turtle.forward()
End

P.S. Is it important to have spaces each side of the = sign ? i tried both ways with same error ???


1) You've got a mis-capitalized use of "Length" in the code shown, but that could be due to transcription between Minecraft and this forum
Note: If you're playing on your local PC, your program is under .../saves/<world name>/computer/<turtle ID>/ so you can open it with your favorite editor and copy & paste the actual code.

2) If you're still having trouble once you add fueling as suggested by gattsuru, could you describe your errors as more than "no go" ?
 

Calibra

New Member
Jul 29, 2019
38
0
0
Hi
I really appreciate the code example, however i still want to understand why my simple code doesnt work....i can then move on..

the error i am getting is bios :338: [string "move" ] :3: `=` expected ( best i can do, dont know how to copy and paste errors in the turtle !!! )

local length = 30
local width = 20

For a = 1, length do
turtle.forward()
End

turtle.turnRight()
For a = 1, width do
turtle.forward()
End

turtle.turnRight()
For a = 1, length do
turtle.forward()
End

turtle.turnRight()
For a = 1, width do
turtle.forward()
End
 

whd23

New Member
Jul 29, 2019
111
0
1
Hi
I really appreciate the code example, however i still want to understand why my simple code doesnt work....i can then move on..

the error i am getting is bios :338: [string "move" ] :3: `=` expected ( best i can do, dont know how to copy and paste errors in the turtle !!! )

local length = 30
local width = 20

For a = 1, length do
turtle.forward()
End

turtle.turnRight()
For a = 1, width do
turtle.forward()
End

turtle.turnRight()
For a = 1, length do
turtle.forward()
End

turtle.turnRight()
For a = 1, width do
turtle.forward()
End


Is that a direct copy & paste of your code? Because you're capitalizing words that should not be.

Lua Manual: http://www.lua.org/manual/5.1/manual.html

"Lua is a case-sensitive language: and is a reserved word, but And and AND are two different, valid names."

So using "For" and "End" instead of "for" and "end" is going to give you problems.
 

gattsuru

Well-Known Member
May 25, 2013
364
103
68
LUA is case-sensitive. Use "for", all lower-case, not "For", and "end" rather than "End".

Conventions are to always use lower camelCase (first word uncapitalized, all other words first letter capitalized) for local variables and all methods. Only globals and some reserved names should be capitalized, and those should be in MAX_CAPS (all letters capitalized, words separated by an underscore). _MAX_CAPS (all letters capitalized, words separated by an underscore, starting with an underscore) is allowed, but may interfere with reserved words used for special internal purposes in LUA itself.

Never start a variable name with a number : it will not work.
 

Fuzzlewhumper

New Member
Jul 29, 2019
500
0
0
local Length=20 // uppercase L - noted
local Width=30 // uppercase W - noted
For a = 1,Length do // For should be for
turtle.forward() // no errors
End // should be end not End
turtle.turnRight() // no errors
For a = 1, Width do // For should be for
turtle.forward() // no errors
End // should be end not End
turtle.turnRight() // no errors
For a = 1,length do // length should be Length, For should be for
turtle.forward() // no errors
End // End should be end
turtle.turnRight() // no errors
For a = 1, width do // For should be for, width should be Width
turtle.forward() // no errors
End // End should be end

comments after // on each line.
 

Calibra

New Member
Jul 29, 2019
38
0
0
Hi
Thanks everyone......cant believe how simole the solution was. Changed it all ( mostly ) to lower case and off went my turtle.....
Now i am on my way, will try some more constructive programs...

I know there a lots of them ' out there ' but i want to build my own tree farm, will be a god learning experience...

Thanks again

Incidently some of problems arose from copying and pasting from web pages, some them had obviously auto capitilized and led me down the wrong path....
 

namiasdf

New Member
Jul 29, 2019
2,183
0
0
I am not sure if you are allowed capitalize the ends, but it depends on where the program is saying the error is first. Lua works top bottom, so I assume it hits your first syntax error and spits something out.

Aside from that, I think variables are allowed to be capitalized.

Is this program just making the turtle go around in circles? (squares, whatever.)
 

Calibra

New Member
Jul 29, 2019
38
0
0
Hi
Yes its just to help me get started ( everyone has to start somewhere i guess )
Now i realise the capitals issue, i can progress.
Couple of questions...
1 is it easier to construct a working tree farm, then write a program to get the turtle to make the same, but obviously automatically.....wondered if it might be easier to have a template first.
2 i hear there is a way of creating a 3D map of where the turtle is during a build....could this be used to send the turtle to a certain location within the construction .. Also is there a piece of code i could ' borrow' to see how this works....
Thanks
 

namiasdf

New Member
Jul 29, 2019
2,183
0
0
1. Depends on how your mind operates. If you need to see it first to build the program, do so. If you think your thought process is all-encompassing-enough- the meh. Building one allows you to test your program with something real...

2. I hear there is something called GPS in FTB. I haven't looked into it.
 

whd23

New Member
Jul 29, 2019
111
0
1
Hi
Yes its just to help me get started ( everyone has to start somewhere i guess )
Now i realise the capitals issue, i can progress.
Couple of questions...
1 is it easier to construct a working tree farm, then write a program to get the turtle to make the same, but obviously automatically.....wondered if it might be easier to have a template first.
2 i hear there is a way of creating a 3D map of where the turtle is during a build....could this be used to send the turtle to a certain location within the construction .. Also is there a piece of code i could ' borrow' to see how this works....
Thanks

I'd think writing a program to build a tree farm from nothing would be a lot easier than writing a program that attempts to scan an existing design. Remember that turtles can only "see" the block immediately in front, above, and below them. And to make matters even more difficult, a turtle can't "see" arbitrary blocks, it can only compare blocks around it to block types in its inventory. Because of this, a scanning program would only be able to identify 16 kinds of blocks; one block type for each turtle inventory slot.

You can use ComputerCraft GPS to help you keep track of where a turtle is, but for your tree farm build it's hardly necessary; relative positioning will do. At program start you'll want to initialize your X, Y, and Z co-ordinates and the direction you are facing. Remember, these will be relative to the start position, not actual Minecraft world position and direction values.
Code:
-- Program start
myX = 0
myY = 0
myZ = 0
facing = 0

As the turtle moves, you need to appropriately update the current relative position of the turtle. Remember that the turtle API will return "true" or "false" if the movement command was successful; don't update the relative positions unless the move was successful. The easiest is the Y co-ordinate since it is not dependent on the direction the turtle is facing.
Code:
...
if turtle.up() then myY = myY + 1 end
...
if turtle.down() then myY = myY - 1 end

Because you'll need to know the direction the turtle is facing to accurately update your relative X & Z co-ordinates, you'll want to write your own "turnLeft" and "turnRight" routines which will use the turtle API but also update the relative facing
Code:
function myTurnRight()
  turtle.turnRight()  -- According to the API doc, this can't fail
  facing = (facing + 1) % 4  -- facing will always be 0 to 3
end --myTurnRight()

Do something similar for turnLeft, but in that case you'll need to subtract 1 from facing and deal with the fact that it can go from 0 to -1.

As long as you know your facing you can update your X & Z:
Code:
if turtle.forward() then
  if facing == 0 then
    myX = myX + 1
  elseif facing == 2 then
    myX = myX - 1
  elseif facing == 1 then
    myZ = myZ + 1
  else -- facing must be 3
    myZ = myZ - 1
  end
end

You can use the relative co-ordinates and facing values to put your turtle exactly where you want it to be for your build. Let's say you want the turtle to be 10 blocks forward and 20 blocks right of the starting position. We decided that when "facing" is 0, the turtle is facing in the original placement direction and that X will increase as the turtle moves in that direction.
Code:
--  Note that this won't properly position the turtle if
-- it's already at an X that's more than 10
while myX <= 10 do
  myForward()
end
 
myTurnRight()
 
--  Note that this won't properly position the turtle if
-- it's already at an Z that's more than 20
while myZ <= 20 do
  myForward()
end

Most importantly: create functions for these various operations so your actual code is more compact, readable, and robust.

Edit: typo correction