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
-
What is the error that you are currently getting?hugomg– hugomg2013年10月31日 20:27:51 +00:00Commented Oct 31, 2013 at 20:27
-
1Is the question here how to only call hwd() during the first iteration of the loop?Etan Reisner– Etan Reisner2013年10月31日 20:41:15 +00:00Commented Oct 31, 2013 at 20:41
-
i am getting no error since the code isnt finished for me to execute it but yes and no Etan i want it to call the hole function but want hwd() to be called on the first iterationSubTox1c– SubTox1c2013年11月01日 16:59:17 +00:00Commented Nov 1, 2013 at 16:59
1 Answer 1
Does the loop always execute at least once? Then simply put that code right before the loop.
-- do the special thing
for p = 1,w do
If not, you can put it the loop guarded by a boolean:
local didTheSpecialThing = false
for p = 1,w do
if not didTheSpecialThing then
-- do the special thing
didTheSpecialThing = true
end
...
Or you could put it before the loop, with a check to see if it needs to be executed:
if w > 0 then
-- do the special thing
end
for p = 1,w do