-1

i've got a query about my code. I currently cant test it so i thought i'd ask. (for context i'm very new to lua) Hopefully i'm not asking a duplicate question, or anything similar.

here's my code currently

write("Column: ")
local column = tonumber( read() )
write("Row: ")
local row = tonumber( read() )
local x = 0
local y = 0

function digforward(str)
    repeat
        turtle.dig()
        turtle.forward()
        x = x+1
    until x == column

So after the repeat until loop ends, what would i do to set the variable x back to 0? I'm aware that x = 0 would normally do it, but i want to make it so when you run the function it goes till x = column and then set x to 0.

Additionally just so you know this is code for a computer in a game. (from the computercraft mod for minecraft)

xXMAZEEXx
  • 13
  • 5
  • 1
    I don't exactly understand what you mean by 'I'm aware that x = 0 would normally do it ...'. Why don't you do that? Just place that after the repeat-loop and you're fine? – pschulz Jan 20 '21 at 18:27
  • @pschulz because i assume when i call the function it won't run that aswell. – xXMAZEEXx Jan 20 '21 at 19:10
  • You do `x = 0`..... – user253751 Jan 20 '21 at 19:11
  • Have you learned that the computer does things in the order you write them, not all at once? It won't do `x=0` until it gets to the `x=0` part. – user253751 Jan 20 '21 at 19:12
  • @user253751 yes i've learned that, but i'm unsure of if it would work to put x = 0 after that loop. Are you guys saying to put it in the function after the loop, or outside? – xXMAZEEXx Jan 20 '21 at 19:33
  • 1
    You put it at the part of the program when you want it to happen – user253751 Jan 20 '21 at 20:55

1 Answers1

2

In that case, it'd be better to just use a numeric for-loop:

for x = 1, column do
   turtle.dig()
   turtle.forward()
end

And as a small extra:

for x = 1, column do
   while not turtle.forward() do
      turtle.dig()
   end
end

This makes sure to re-try until the turtle actually manages to move, like when a sand block falls down immediately after digging. You can also throw in an attack for good measure for cases when an enemy is blocking the movement.

user253751
  • 57,427
  • 7
  • 48
  • 90
DarkWiiPlayer
  • 6,871
  • 3
  • 23
  • 38