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

Trying to get turtle to return to start pos when done.

Started by Blueknight1758, 20 November 2012 - 08:29 AM
Blueknight1758 #1
Posted 20 November 2012 - 09:29 AM
I used a program on this website called 'streetbuilder' and have edited it.
I am trying to make the turtle return to where it started at the end of the program using the last bit of this code below.

http://pastebin.com/9s120wXN

This generates the error

bios:338: [string "platform"] :24: '=' expected.
line 24 is "when x==a do"
what I would like to know is where i am going wrong or is there a better way to do it?
bjornir90 #2
Posted 20 November 2012 - 09:42 AM
Thanks for recreate a new topic :(/>/>. It's because
When … then does not exist. You should use if … then instead.
Blueknight1758 #3
Posted 20 November 2012 - 09:56 AM
Ok, i changed 'when' to 'if' but it now says it expects 'then' so I tried that and got "error unexpected symbol" I'm still a bit of a novice but any ideas?
So sorry I was being a div, now I have

if x==a then
etc. etc.
however it chooses to ignore this area of code completely, presumably because x doesn't equal a but i can't think what to do.
yet another edit: after changing x==a to x<=a it registers this code and turns right twice but only goes forward once. How do i make the value of 'a' stay around?
Edited on 20 November 2012 - 09:20 AM
remiX #4
Posted 20 November 2012 - 10:15 AM
try
while a == x do
    -- code
end
Blueknight1758 #5
Posted 20 November 2012 - 10:32 AM
try
while a == x do
	-- code
end
Just tried that, and it didn't execute the coding at all, is it possible that "a" is getting cleared at the first "end" tag?
remiX #6
Posted 20 November 2012 - 10:39 AM
No, that means a is not equal to x.
Blueknight1758 #7
Posted 20 November 2012 - 10:48 AM
does x<=a mean x is greater than or equal to a? because that may solve that bit at least.
remiX #8
Posted 20 November 2012 - 10:51 AM
x <= a – x is smaller than or equal to a
x >= a – x is greater than or equal to a
x == a – x is equal to a
x ~= a – x it not equal to a
x > a – x is only greater than a
x < a – x is only smaller than a
Blueknight1758 #9
Posted 20 November 2012 - 10:57 AM
So using x>=a should solve it, but I have tried that and it turns right twice and moves forward once.
When a is three.
Because when I run the program it says how much?
So I type in three which I believe should register "a" as three. Am I right there? Or have I missed something with variables etc?
remiX #10
Posted 21 November 2012 - 02:02 AM
Can you post the current code you have now?

If a starts of as 3 and x is 1, obviously it's only going to go twice because everytime it does one loop x is increases.

What exactly are you trying to do?
Blueknight1758 #11
Posted 21 November 2012 - 05:45 AM
All of my code as of now:
http://pastebin.com/nr9SzPDN
what I am trying to achieve is you run the program and it asks how long you want the platform that is to be built to be.
Then the value you input is registered as "a"
then the turtle places a 3 block wide platform "a" times.
hen the turtle does a 180 and goes forward "a" times.
remiX #12
Posted 21 November 2012 - 06:12 AM
Oh so it does something like this?


-- If you enter 5, will it do this?

XXX -- start
XXX
XXX
XXX
XXX -- end
then return to start?

I didn't add any turtle.refuel, but you can do that :(/>/>
If so, then try this:
Spoiler
Args = {...}

if not Args[1] or not tonumber(Args[1]) then
    print("Usage: " .. fs.getName(shell.getRunningProgram()) .. " <length>")
    return
end

l = tonumber(Args[1])

for i = 1, l do
    turtle.forward()
    turtle.digDown()
    turtle.placeDown()
    turtle.turnRight()
    turtle.forward()
    turtle.digDown()
    turtle.placeDown()
    turtle.forward()
    turtle.digDown()
    turtle.placeDown()
    turtle.turnLeft()
    turtle.turnLeft()
    turtle.forward()
    turtle.forward()
    turtle.turnRight()
end
for i = 1, l-1 do turtle.back() end
Blueknight1758 #13
Posted 21 November 2012 - 06:27 AM
Oh so it does something like this?


-- If you enter 5, will it do this?

XXX -- start
XXX
XXX
XXX
XXX -- end
then return to start?

Yes that's exactly what i'm trying to achieve, i've just ran your code and it works perfectly thank you,

now for the part to actually get better at this.
"I'm assuming "args" means argument.
so your coding is saying:
Argument = {nothing}
if not nothing then execute code?

and the bit of coding that makes the turtle return to start, the

for i = 1, l-1 do turtle.forward() end
does that mean for a <length> of 1 go forward once and multiply by whatever value "i" is?
Lyqyd #14
Posted 21 November 2012 - 06:45 AM
The … isn't "nothing", it's how you catch variable arguments. Putting them into a table happens to be the handiest way of using them.
remiX #15
Posted 21 November 2012 - 06:54 AM
Oh so it does something like this?


-- If you enter 5, will it do this?

XXX -- start
XXX
XXX
XXX
XXX -- end
then return to start?

Yes that's exactly what i'm trying to achieve, i've just ran your code and it works perfectly thank you,

now for the part to actually get better at this.
"I'm assuming "args" means argument.
so your coding is saying:
Argument = {nothing}
if not nothing then execute code?

and the bit of coding that makes the turtle return to start, the

for i = 1, l-1 do turtle.forward() end
does that mean for a <length> of 1 go forward once and multiply by whatever value "i" is?
Oh so it does something like this?


-- If you enter 5, will it do this?

XXX -- start
XXX
XXX
XXX
XXX -- end
then return to start?

Yes that's exactly what i'm trying to achieve, i've just ran your code and it works perfectly thank you,

now for the part to actually get better at this.
"I'm assuming "args" means argument.
so your coding is saying:
Argument = {nothing}
if not nothing then execute code?

and the bit of coding that makes the turtle return to start, the

for i = 1, l-1 do turtle.forward() end
does that mean for a <length> of 1 go forward once and multiply by whatever value "i" is?

Yes args is arguments and it is used to catch arguments for when you run the program.

I edited my code before you quoted because I actually forgot about turtle.back() so you can actually remove the 4 turtle.turnLeft() near the end.

The loop basically means while i <= l-1 do turtle.forward() end. I actually am not sure how to explain it but it will just do turtle.forward l (length) - 1 times. - 1 because I made it move forward in the beginning.
Blueknight1758 #16
Posted 21 November 2012 - 07:47 AM
I think I understand it now, thanks a million, now to actually start building…
remiX #17
Posted 21 November 2012 - 09:04 AM
I think I understand it now, thanks a million, now to actually start building…

Haha enjoy :(/>/> Once you get use to LUA and turtle coding, you have some awesome times with that you can do with them :(/>/>