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

Error And How To Repeate Commands

Started by sleawnis, 28 September 2012 - 02:33 PM
sleawnis #1
Posted 28 September 2012 - 04:33 PM
i have this code for my mining turtle:

http://i.imgur.com/x4eiU.jpg
http://imgur.com/x4eiU,DMkD0,wyfvA (I used link to make sure it was exalcy how i wrote it)

i get the http://i.imgur.com/TD4m8.jpg error

1. how do i fix the error?
2. how do i make the programs repeate like 100 times?
Thanks :P/>/>
OmegaVest #2
Posted 28 September 2012 - 04:53 PM
Okay, a few things:

For problem 1:
1) Is this on a server? If not, go into your mincraft folder, the save for whatever world this is (again, if it is not multiplayer), find the computer this is written on and copy the code to here.
2) Format. Everytime you have an if-block, put three spaces on the next line. For every end, take three spaces away.


while true do
   --stuff
   if stuff then
	  --stuff
   elseif other stuff then
	  --more stuff
   else
	  --even more stuff
   end
end

Because the error you are getting is that you have one too many ends, most likely, and formatting is a very easy way to figure out where it is.

For the second problem.
You can use a for loop, if you want. That repeats a set of actions a definite number of times.

for i = 1,  -- This sets up the start position, which can be useful for traversing a table, figuring out where a turtle is, etc.
endVar,   -- This is your end position, usually a number higher than the initiator variable.
1  -- This is your incrementer. 1 is default, so if you are going up by one, you don't need this. For any other numbers (such as going downward) you will need it.
do  --  Tells the computer where the for call ends

   --Stuff goes here

end  --  Make sure you cap everything. Just no too many things.

So, for what you want (say, 100 times), you would do this:


for i = 1, 100 do
   --  Code
end


There are other ways, but this is the simplest route. Also. Go read the Lua reference document. Google Lua 5.1 and you'll find it.








EDIT: The guy beneath me brings up a loop style called recursive looping, which is a terrible idea for an infinite loop. Finite loops, fine, but not infinite. Any more than about two hundred iterations of it causes the program to stop due to reaching the sandbox's memory limit. A better infinite loop would be a while loop like so:


while true do
  --Code
end

Which doesn't cause all sorts of nasty problems.
ChaddJackson12 #3
Posted 29 September 2012 - 12:32 AM
You can make it repeat infinite times a few ways: (Just for your convenience :P/>/>)
1. Naming the file startup, and then putting os.reboot() at the end of the code.

2. Making 2 functions of the same material (Your current code), and they would look like this.


local function code()
   (PUT YOUR CODE HERE FOR THE TURTLE)
   repeatcode()
end

local function repeatcode()
   code()
end

Hope that this helps, and for the error, from my understanding "<eof> expected" means End of file expected, and so you have an end somewhere that it shouldn't be.