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

I need help with my code

Started by Chrisdaman99, 26 September 2012 - 01:41 AM
Chrisdaman99 #1
Posted 26 September 2012 - 03:41 AM
Hello people i was trying to make my program but i have run into an error when i try to execute my code i get the following… Bios:26: [string "dig"]:37: '=' expected
this is my code:

local x –Current pos on x axis
local y –Current pos on the y axis
local z –Current pos on the z axis
local startX –position that the robot will begin to mine(on the x coordinate.)
local startY –height that the robot will begin to mine.
local startZ –same as startX but for the z position.
local xDiff –How many blocks in the X direction
local yDiff –How many blocks in the y direction
local zDiff –How many blocks in the z direction
local i = 1

term.clear()
term.setCursorPos(1,1)


write("What Is My x Position? ")
x = read()
write("What Is My y Position? ")
y = read()
write("What Is My z Position? ")
z = read()

write("Where Do You Want Me To Begin?(X) ")
startX = read()
write("Where Do You Want Me To Begin?(Y) ")
startY = read()
write("Where Do You Want Me To Begin?(Z) ")
startZ = read()

xDiff = startX - x
yDiff = startY - y
zDiff = startZ - z

repeat
turtle.forward()
xDiff = xDiff + 1
untill xDiff == startX
ChaddJackson12 #2
Posted 26 September 2012 - 04:00 AM
Well for one thing. You have spelled "Until" wrong at the bottom line, perhaps that is the problem.
stilldabomb #3
Posted 26 September 2012 - 04:40 AM
Can you put your code in a
 
BBCode?
ChaddJackson12 #4
Posted 26 September 2012 - 04:52 AM
Can you put your code in a
 
BBCode?
Try out this code: It is your code, edited.

local x --Current pos on x axis
local y --Current pos on the y axis
local z --Current pos on the z axis
local startX --position that the robot will begin to mine(on the x coordinate.)
local startY --height that the robot will begin to mine.
local startZ --same as startX but for the z position.
local xDiff --How many blocks in the X direction
local yDiff --How many blocks in the y direction
local zDiff --How many blocks in the z direction
local i = 1

term.clear()
term.setCursorPos(1,1)


write("What Is My x Position? ")
x = read()
write("What Is My y Position? ")
y = read()
write("What Is My z Position? ")
z = read()

write("Where Do You Want Me To Begin?(X) ")
startX = read()
write("Where Do You Want Me To Begin?(Y) ")
startY = read()
write("Where Do You Want Me To Begin?(Z) ")
startZ = read()

xDiff = startX - x
yDiff = startY - y
zDiff = startZ - z

repeat
turtle.forward()
xDiff = xDiff + 1
until xDiff == startX
MrBarry #5
Posted 27 September 2012 - 01:44 AM
This topic might be better suited for the Ask a Pro forum, but Chadd's right. Your error is coming from misspelling until at the end.

I took the liberty of adding a couple things you might not have known about to your code.

local x, y, z --Current pos
local startX, startY, startZ --position that the robot will begin to mine
local xDiff, yDiff, zDiff --How many blocks
local i = 1

term.clear()
term.setCursorPos(1,1)

x, y, z = gps.locate(5) -- get current location with timeout 5
while not (tonumber(x) and tonumber(y) and tonumber(z)) do
	print("Please enter valid coordinates.")
	write("What Is My x Position? ")
	x = read()
	write("What Is My y Position? ")
	y = read()
	write("What Is My z Position? ")
	z = read()
end

repeat
	print("Where do you want me to begin?")
	write("(X) ")
	startX = read()
	write("(Y) ")
	startY = read()
	write("(Z) ")
	startZ = read()
until tonumber(startX) and tonumber(startY) and tonumber(startZ)

xDiff = startX - x
yDiff = startY - y
zDiff = startZ - z

repeat
turtle.forward()
xDiff = xDiff + 1
until xDiff == startX

First thing, you can define and assign multiple variables on one line. For example.

local x, y, z = 1, 2, 3

Second, the gps api will give you coordinates if your turtle is wireless and you have the 4 gps servers listening on rednet. I'm sure you don't have those set up right now, so the function will timeout after 5 seconds. The next block makes it so that it only asks for starting position if the gps call fails.

I know this is early in your development process, but sanity checking user input is always important. When you use read(), what the user types is stored as a string. When you do xDiff = startX - x, lua has to try to convert those strings into numbers. If the user typed something stupid like "left" for x position, your program would crash when xDiff is calculated. The built-in tonumber() function tries to convert a string to a number and returns nil if it is unsuccessful. The expression (tonumber(x) and tonumber(y) and tonumber(z)) will return false if any of those variables are not numbers, and the program will ask the user for input again.

If you don't want to use gps, you may want to consider just using relative coordinates. i.e. define your starting position as (0,0,0).
stilldabomb #6
Posted 05 October 2012 - 03:06 AM
Can you put your code in a
 
BBCode?
Try out this code: It is your code, edited.

local x --Current pos on x axis
local y --Current pos on the y axis
local z --Current pos on the z axis
local startX --position that the robot will begin to mine(on the x coordinate.)
local startY --height that the robot will begin to mine.
local startZ --same as startX but for the z position.
local xDiff --How many blocks in the X direction
local yDiff --How many blocks in the y direction
local zDiff --How many blocks in the z direction
local i = 1

term.clear()
term.setCursorPos(1,1)


write("What Is My x Position? ")
x = read()
write("What Is My y Position? ")
y = read()
write("What Is My z Position? ")
z = read()

write("Where Do You Want Me To Begin?(X) ")
startX = read()
write("Where Do You Want Me To Begin?(Y) ")
startY = read()
write("Where Do You Want Me To Begin?(Z) ")
startZ = read()

xDiff = startX - x
yDiff = startY - y
zDiff = startZ - z

repeat
turtle.forward()
xDiff = xDiff + 1
until xDiff == startX
I hope you know, I wasn't the one who asked the question, I was asking chris to put HIS code in a BBCode…
ChaddJackson12 #7
Posted 06 October 2012 - 04:42 PM
Can you put your code in a
 
BBCode?
Try out this code: It is your code, edited.

local x --Current pos on x axis
local y --Current pos on the y axis
local z --Current pos on the z axis
local startX --position that the robot will begin to mine(on the x coordinate.)
local startY --height that the robot will begin to mine.
local startZ --same as startX but for the z position.
local xDiff --How many blocks in the X direction
local yDiff --How many blocks in the y direction
local zDiff --How many blocks in the z direction
local i = 1

term.clear()
term.setCursorPos(1,1)


write("What Is My x Position? ")
x = read()
write("What Is My y Position? ")
y = read()
write("What Is My z Position? ")
z = read()

write("Where Do You Want Me To Begin?(X) ")
startX = read()
write("Where Do You Want Me To Begin?(Y) ")
startY = read()
write("Where Do You Want Me To Begin?(Z) ")
startZ = read()

xDiff = startX - x
yDiff = startY - y
zDiff = startZ - z

repeat
turtle.forward()
xDiff = xDiff + 1
until xDiff == startX
I hope you know, I wasn't the one who asked the question, I was asking chris to put HIS code in a BBCode…
Lol, I did not notice XD