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

loop in a loop, priorities

Started by Langvad, 11 August 2015 - 01:00 PM
Langvad #1
Posted 11 August 2015 - 03:00 PM
So i just picked op scripting with the mining turtle, and i think its really awesome!

I am trying to create a script that use user input for X and Z, to send turtle into placing landmarks for quarry, filler etc.

it looks something like this:



if turtle.getFuelLevel() < 100
then turtle.select(16)
turtle.refuel()
end


print("Enter the X distance")
xDist =read()

print("Enter the Z distance")
zDist = read()

x=0
z=0

// in order for the turtle to place the first landmark i manually typed the lua turtle movement:

turtle.forward()
turtle.turnLeft()
turtle.select(15)
turtle.dig()
turtle.place()
turtle.turnRight()

// then for the user input X movement

while x<xDist-1
do turtle.forward()
while turtle.detect()==true
do turtle.dig()
sleep(1)
end
x=x+1
end


the rest is just this repeated, so i have a loop in a loop, the idea is to make the script able to kill of gravel/sand without loosing the userinput distance, like 5 x 5

im not sure what i do wrong here :)/> because it keeps going one less forward with this, so i get a 4x5 instead of 5x5 with user input.

ive tried to add turtle.forward() in the loop like this:

while x<xDist-1
do turtle.forward()
while turtle.detect()==true
do turtle.dig()
turtle.forward()
sleep(1)
end
x=x+1
end


but then with more gravel or sand it moves too long, so never get the correct result with obstacles in the way.


- Langvad
KingofGamesYami #2
Posted 11 August 2015 - 03:43 PM
Welcome to the forums! First off, there are special tags for code, [code]print( "Hello World!" )

print( "Hello World!" )

Second, declare x as 1 instead of 0. 0-5 is 6 movements, because there are 6 numbers (0, 1, 2, 3, 4, 5).

Third, maybe you'd like a for loop:

for x = 1, xDist do
  --#stuff
end
Bomb Bloke #3
Posted 11 August 2015 - 03:43 PM
while x<xDist-1 do  -- On each iteration of this loop,
	turtle.forward()   -- we try to go forward once (which may or may not work),
	
	while turtle.detect()==true do
		turtle.dig()
		turtle.forward()  -- then we keep trying to go forward until we move to a position without a block in front,
		sleep(1)
	end
	
	x=x+1  -- then after all that we increment x once!
end

Try it like this:

while x<xDist-1 do  -- On each iteration of this loop,
	while not turtle.forward() do  -- we try to go forward until we get a single success,
		turtle.dig()
		turtle.attack()
	end
	
	x=x+1  -- therefore incrementing x exactly once per movement.
end
Edited on 11 August 2015 - 01:46 PM
Langvad #4
Posted 11 August 2015 - 05:30 PM
hi :)/> it worked perfectly!

i am really amazed by this, but i dont understand the logic in it, when u tell it do something when its Not turtle.forward() does that autmaticly mean that its gonna try to achieve a succes with turtle.forward() command, and therefore going forward?
KingofGamesYami #5
Posted 11 August 2015 - 05:42 PM
turtle.forward returns true if it went forward, false if it didn't. not inverts whatever was returned, for example true would be false.

The loop will run as long as the turtle didn't go forward: when it has, the loop stops running.
Langvad #6
Posted 11 August 2015 - 07:51 PM
that makes sense :)/>
i instantly went over to a 3d version of the landmarks, it looks like this:

if turtle.getFuelLevel() < 300
then turtle.select(16) turtle.refuel()
end

x=0
y=0
z=0
fw = turtle.forward
bk = turtle.back
dig = turtle.dig
digup = turtle.digUp
digdw = turtle.digDown
att = turtle.attack
attup = turtle.attackUp
attdw = turtle attackDown
tr = turtle.turnRight
tl = turtle.turnLeft
sel = turtle.select
pd = turtle.placeDown
pf = turtle.place
up = turtle.up
down = turtle.down
Print ("Please enter the X value")
xDist = read()
Print ("Please enter the Z value")
zDist = read()
Print ("Please enter the Y value")
yDist = read()
xDist=xDist2
zDist=zDist2
yDist=yDist2
dig()
fw()
tl()
dig()
fw()
sel(1)
pd()
bk()
sel(15)
pf()

while y<yDist-1
do
   while not up()
   do  digup() attup()
  end
  y=y+1
end
dig()
att()
fw()
sel(1)
pd()
bk()
sel(15)
pf()
tr()
while x<xDist-1
  do
   while not fw()
   do dig() att()
   end
   x=x+1
end
tl()
dig()
att()
fw()
sel(1)
pd()
bk()
sel(15)
pf()
while y>0
  do
   while not down()
    do digdw() attdw()
    end
  y=y-1
end
	
dig()
att()
fw()
sel(1)
pd()
bk()
sel(15)
pw()
tr()
tr()
while z<zdist-3
  do
   while not fw()
   do dig() att()
   end
z=z+1
end
dig()
att()
fw()
sel(1)
pd()
bk()
sel(15)
pf()
while y<yDist2-1
  do
   while not up()
   do digup() attup()
   end
  y=y+1
end
dig()
att()
fw()
sel(1)
pd()
bk()
sel(15)
pf()
tr()
while x>0
   do
    while not fw()
    do dig() att()
    end
  x=x-1
end
tl()
dig()
att()
fw()
sel(1)
pd()
bk()
sel(15)
pf()
while y>0
  do
   while not down()
    do digdw() attdw()
   end
y=y-1
end
dig()
att()
fw()
sel(1)
pd()
bk()
sel(15)
pf()
tr()
tr()
while z>1
  do
   while not fw()
   do dig() att()
   end
z=z-1
end
tr()
bk()
tl()
fw()
tr()  

it gives me an error when i run it saying: bios: 339 ,[string. "filler" ]: 18: . ' =' . expected

ive looked through it quite abit but cant seem to find the solution, what could cause this ?
Dog #7
Posted 11 August 2015 - 08:15 PM
I don't see what's causing the error you're having, but I do see another problem you'll want to fix:

You set xDist, yDist, and zDist via read then you immediately set them to nil by making them equal to undeclared variables xDist2, yDist2, and zDist2. What you want to do is reverse those so you set xDist2 = xDist, yDist2 = yDist, and zDist2 = zDist. In fact, you don't even need to use xDist2, yDist2, and zDist2 - you can just xDist, yDist, and zDist for your comparisons (which you actually do, but for some reason you use yDist2 for one loop).
Edited on 11 August 2015 - 06:30 PM
Langvad #8
Posted 11 August 2015 - 09:21 PM
i missed a . at turtle attackdown, damn i guess im gonna encounter these things alot :D/> gonna start at computer sience in a month :P/>

i changed it aroung with Dist2 as u said, it made more sense :)/>

code works perfectly btw ;)/>