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

asked Oct 31, 2013 at 20:13
3
  • What is the error that you are currently getting? Commented Oct 31, 2013 at 20:27
  • 1
    Is the question here how to only call hwd() during the first iteration of the loop? Commented 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 iteration Commented Nov 1, 2013 at 16:59

1 Answer 1

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
answered Oct 31, 2013 at 22:37
Sign up to request clarification or add additional context in comments.

1 Comment

Mud i thank you greatly. The last 1 worked perfectly now all i need to do is add in a mob/gravel detection bit and maybe block lite detect for torches. now i can do the finishing touches to my turtle script. i cant thank you enough.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.