0

i am currently playing ftb on minecraft and have gotten into the mod called computer craft, that uses lua as its programing language. i have written a basic script so far for clearing out rooms with 3 directions and any size. the directions are Right Left and Center. the center is for if the person wants the door to be the center of the room. but i am having trouble performing a function i made along wit a bit of code one in another function once.

this is the function i want to perform once in another function

function hwd()
    for m = 1,w*0.5 do
       turtle.dig()
       echest()
       turtle.forward()
    end
end  

this is the main function

function cntr()
    if d == "c" then
            for p = 1,w do
                length()
                turtle.turnRight()
                turtle.dig()
                echest()
                turtle.forward()
                turtle.turnLeft()
            end
    end
end

the part i need to perform once in on the first loop will look like this

turtle.turnLeft()
hwd()
turtle.turnRight()

after "for p = 1,w do" and before "length()"

i have the script working without the center part but need to ad this part. any help would be greatly appreciated.

for the working script head to: http://pastebin.com/Uf5Li1Cy

for the script with the added center part head to: http://pastebin.com/AqZHQrFb

4

1 回答 1

1

循环是否总是至少执行一次?然后只需将该代码放在循环之前。

  -- do the special thing
  for p = 1,w do

如果没有,你可以把它放在由布尔值保护的循环中:

  local didTheSpecialThing = false
  for p = 1,w do
     if not didTheSpecialThing then
        -- do the special thing
        didTheSpecialThing = true
    end
    ...

或者你可以把它放在循环之前,检查它是否需要执行:

  if w > 0 then
    -- do the special thing
  end
  for p = 1,w do
于 2013-10-31T22:37:54.090 回答