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

Crafty Turtle program: bios:337:[string "f"]:11:'do' expected

Started by RolandLundvall, 19 April 2014 - 07:28 AM
RolandLundvall #1
Posted 19 April 2014 - 09:28 AM
Greetings

I am new to LUA and rather inexperienced at programming in general. But since I did not find a suitable crafting program for my crafing turtle I had to try and make one myself.

Here is the code:


   
slotTable = {1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 2} --Tabell för vilka slots som skall ha innehåll och inte. Slutar när den kommer till slot 12 och får siffran 2

function fill()
local i = 1
    while slottable[i] ~= 2 do        

        if turtle.suckDown() == True then --så länge det finns material i kistan

                for i = 1, slotTable[i] = 2, 1 do -- Så länge som variabeln slotTable[tabellposition] skiljer sig från sluta så görs detta. Efter varje gång läggs ett till på i
                    if slotTable = 1 then --Om slotTable är 1
                        turtle.getSelectedSlot(i) --get slot
                            while turtle.getItemCount <64 do --så länge det är mindre än 64 st i sloten
                            turtle.suckDown --hämta material från kistan
                            end    --slut på for-satsen
                    else
                    i = i + 1 --Om slotTable skiljer sig från 1, lägg till ett på i
                    end
                end
        else
        print ("Material saknas!!")
        slottable =2

        end


function make()

    --kolla om det finns material i alla slots där det skall vara material
    for i = 1, slotTable = 2, i = i + 1 do
        if slotTable[i] = 1 then
            turtle.select(i)
                if turtle.getItemCount ~= 64 do --om itemCount skiljer sig från 64
                fill()
                end

        else
        i = i + 1
        end
    end
            
    

    while turtle.dropUp = true do --om det finns plats i kistan
        turtle.select(16) --välj slot 16
                
            if turtle.getItemCount(16) == 0 then --Om det inte finns något i sloten
                turtle.craft() --tillverka sakerna
                turtle.dropUp() --lägg sakerna i kistan
                print ("Tillverkade en stack")

            else
                if turtle.dropUp ~= false then --så läge som det går att lägga saker i kistan
                    turtle.dropUp() --om det fanns saker i slot 16 och det finns plats i kistan, töm sakerna till den
                    turtle.craft() --tillverka sakerna
                    turtle.dropUp() --lägg sakerna i kistan
                    print ("Tillverkade en stack")
                else
        
                print ("Målkista är full!!!!")
                
                end --slut på if-satsen


            end    --slut på if-satsen
            
                    
            
    end    --slut på while-satsen
        
    print ("Målkistan är full!")        

end --slut på funktionen

function wait()
    
    local x = 1
    while turtle.suckDown == false do
        print ("Väntar på material....." x )
    x = x + 1
    end
end

--Huvudprogrammet, typ

a = read() --ta in text från commandoraden

while a ~= "stop" do --så länge ingen skriver stop
    if turtle.dropUp == true &amp;&amp; turtle.suckDown == true do
    fill()    
    make()
    
    else

    wait()
end



For some reason I do not understand I get the message: bios:337:[string "f"]:11:'do' expected, where f is the name of the program as downloaded. Pastebin link here: http://pastebin.com/g0mKGuz8

Where do I fault in the programming? i am sry the comments are in swedish, but since the code itself is breaf and hopefully self explanatory, I hope it is ok.

Best regards
Roland
MKlegoman357 #2
Posted 19 April 2014 - 01:37 PM
Could you explain what this should do?


for i = 1, slotTable[i] = 2, 1 do --// Invalid 'for' loop syntax. What is 'slotTable[i] = 2' doing here?
  if slotTable = 1 then --Om slotTable är 1
    turtle.getSelectedSlot(i) --get slot
    while turtle.getItemCount <64 do --så länge det är mindre än 64 st i sloten --// Missing parenthesis
    turtle.suckDown --hämta material från kistan --// Missing parenthesis
  end    --slut på for-satsen --// No need for an 'end' here
  else
    i = i + 1 --Om slotTable skiljer sig från 1, lägg till ett på i
  end
end
RolandLundvall #3
Posted 19 April 2014 - 02:55 PM
Could you explain what this should do?


for i = 1, slotTable[i] = 2, 1 do --// Invalid 'for' loop syntax. What is 'slotTable[i] = 2' doing here?
  if slotTable = 1 then --Om slotTable är 1
	turtle.getSelectedSlot(i) --get slot
	while turtle.getItemCount <64 do --så länge det är mindre än 64 st i sloten --// Missing parenthesis
	turtle.suckDown --hämta material från kistan --// Missing parenthesis
  end	--slut på for-satsen --// No need for an 'end' here
  else
	i = i + 1 --Om slotTable skiljer sig från 1, lägg till ett på i
  end
end

Hi, thanx for replying.
I looked at the Lua for beginners and misinterpreted this section I think:

""How does the loop look like?

for variable = beginning, finish, step do
chunck
end

We do have:
  • a variable
  • a beginning
  • a finish
  • and a step to increment the variable ""
The way I intended it to work is:
The variable "i", which is set to 1 should, aslong as the table slotTable does not return "2", do increasements of 1.
If slotTable returns "1" the program checks if there are less than 64 peices in the slot. If there are it shall add more from a chest underneath.
If slottable does not return "1" "i" should increase (I get now that that is wrong, since it would mean that if there was an occation where slotTable did not return "1", as in the case of checking slot 4, it would jump slot 5 and start with slot 6 instead)

would this work better?

SpoilerslotTable = {1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 2} –table of the slots to be used and not in the turtles inventory. When it comes to "2" program part should terminate.

function fill() –fill inventory with contents

while slottable ~= 2 do –as long as slottable does not return 2

if turtle.suckDown() == True then –if there is any contents in the chest below



for i = 1, 12, 1 do –loop through the slots 1-12 (when it reaches slot 12 slotTable should return 2 and it would terminate anyway)

if slotTable = 1 then –if slotTable returns 1
turtle.getSelectedSlot(i) –get slot number "i"

while turtle.getItemCount() <64 do –check if there is less than 64 pcs in slot
turtle.suckDown() –if there was less than 64, get more from chest
end

else –if slotTable returns 0
print ("This slot is not used")
end –This end should still be here, right?
end


else
print ("Material saknas!!") –if turtle.suckDown() returns false
slotTable =2 –terminate program. Or should I reffer to the waiting function instead? as in: 'wait()' instead of 'slotTable = 2'

end
end
end


function make()

while turtle.dropUp() = true do –if there is space left in chest
turtle.select(16) –select slot 16

if turtle.getItemCount(16) == 0 then –if there are no things in slot 16
turtle.craft() –make things with the materials in the slots filled
turtle.dropUp() –put things in chest above
print ("Tillverkade en stack")

else
if turtle.dropUp ~= false then –if chest above has space left
turtle.dropUp() –empty slot 16 to chest
turtle.craft() –make the things
turtle.dropUp() –put things in chest above
print ("Tillverkade en stack")
else

print ("Målkista är full!!!!") –if chest is full it will be prompted. If the chest becomes full during work (as in not full before function starts), I assume both prints would be

done?

end


end



end

print ("Målkistan är full!") –if turtle.dropUp returns false shest is full and player will be prompted

end

function wait()

local x = 1
while turtle.suckDown() == false do –while there are no materials in chest
print ("Väntar på material….." x ) –print waiting for mats..
x = x + 1
end
end

–main program

print ("Welcome to CraftyTurtle. Write stop to terminate.")

a = read() –get user input
while a ~= "stop" do –as long as user does not write stop
if turtle.dropUp == true &amp;&amp; turtle.suckDown == true do –if there is space in chest above AND materials in chest below
fill()
make()

else

wait()
end

print ("Program terminated by user, emptying slots.")

for i = 1, 12, 1 do
turtle.select(i)
turtle.dropDown()
end

turtle.select(16)
turtle.dropUp()
print ("Turtle is empty. Thanx for using CraftyTurtle. See you soon.")


end


http://pastebin.com/g0mKGuz8
Dayrider10 #4
Posted 19 April 2014 - 03:32 PM
You put a do instead of a then here:


if turtle.getItemCount ~= 64 do --om itemCount skiljer sig från 64
			    fill()
end

It should be:


if turtle.getItemCount ~= 64 then --om itemCount skiljer sig från 64
			    fill()
end
RolandLundvall #5
Posted 20 April 2014 - 12:29 PM
Since I couldn't wait for the moderated approval I rewrote the program. Now it works, but not really as I intended. Or, well, it does as long as there is enough mats in the chest below. Ill get back with the changes I wan't to accomplish when I can post without having to wait for the approval…

I am still interested in feedback regarding the first script though. I am having problems getting the for-loops and tha table working.
RolandLundvall #6
Posted 21 April 2014 - 07:33 PM
hi again. Now I rewrote the code. But I still get some strange errors. Right now I get the: bios:337:[string "xxx"] :22: ',' expected.

I got the syntax from an example here: http://www.computerc...post__p__173119
Maybe the fault is elsewhere?

Spoiler

--CraftyTurtle




function fill()		 --fill inventory with contents

print("starting function fill")

print ("Creating table.")

slotTable = {"y","y","y","n","n","y","n","n","y","y","y","c"}		 --create table of the slots to be used and not in the turtles inventory. When it comes to "c" (close) program part should terminate.

print ("Table created")

local x = 1

while x == 1 do

	while turtle.suckDown == true do								--as long as there is materials in chest below
	
		for i = turtle.select(1, 12) do		--go through slots 1 to 12

			if turtle.getItemCount(slotTable[turtle.getSelectedSlot()]) == y then		--I want it to look in the tablearray that was made in the beginning to see weather it should fill the slot or go		

													--to next. Will [i] be the number for the turtle.select, as in when i = 1 it is slot one and since i == 1 it will		

													--check slotTable[1] which returns y?

			turtle.suckDown()								--fill the slot
		
				if turtle.getItemCount(turtle.getSelectedSlot()) == 64 then			--if the selected slot has 64 items in it

				print ("Slot is full, moving to next.")

				else

				print ("Slot is not full, going to function wait()")				--otherwise go to function wait, since there was not enough materials in chest below
				a = 2
				wait ()
	
				end

			elseif turtle.getItemCount(slotTable[turtle.getSelectedSlot()]) == n then		--if this returns n it means slot should stay empty

			print ("This slot is not in use.")

			elseif turtle.getItemCount(slotTable[turtle.getSelectedSlot()]) == c then		--if this returns c the function shall close
			
			print("All slots full!")
			x = x+1											--this closes the function

			else

			end
		
		
		end
	end
end
			
			






function make()
print("starting function make")

local x = 1

while x == 1 do

	while turtle.dropUp() == true do		 --if there is space left in chest
		turtle.select(16)			 --select slot 16
				
			if turtle.getItemCount(turtle.getSelectedSlot()) == 0 then		 --if slot 16 in empty
				turtle.craft()							 --make things with the materials in the slots filled
				turtle.dropUp()						 --put things in chest above
				print ("Tillverkade en stack")

			elseif turtle.dropUp ~= false then					 --if chest above has space left
					turtle.dropUp()					 --empty slot 16 to chest
					turtle.craft()						 --make the things
					turtle.dropUp()					 --put things in chest above
					print ("Tillverkade en stack")
					x = x+1							--terminates the function
			else
		
				print ("Målkista är full!!!!")		 --if chest is full it will be prompted. If the chest becomes full during work (as in not full before function starts), I assume both prints would be done (this and the one below)?

				a = 3
				
			end


				
			
					
			
	end	
		
	print ("Målkistan är full!")					--if turtle.dropUp returns false chest is full and player will be prompted this
	x = x+1	

end



function wait()

print("starting wait")

local x = 1

while x == 1 do

	if a == 2 then

		while not turtle.suckDown() do			 --while there are no materials in chest
		print ("Väntar på material.....")		 --print waiting for mats..
		
	
		end

	elseif a == 3 then

		while not turtle.dropUp() do			--I assume this means that as long as chest above is full 'turtle.dropUp' will return false
	
		print ("Chest full. Waiting for user to empty it.")

		end

	else

	print ("a was neither 2 nor 3...")


	end

--main program

print ("Welcome to CraftyTurtle. Press a key to terminate.")

global a = 1

local x = 1

while not io.read() do

	while turtle.suckDown() do				 --Would this be the same as 'while turtle.suckDown == true do'?
		
	print("inside first while loop")							
		
		while turtle.dropUp() do		 --while there is materials in chest below And if there is space in chest on top
		
		print ("ready to work.")
		
		fill()	
		make()

	end
	
	

wait()

end
	

--print ("Program terminated by user, emptying slots.")

	--for i = 1, 12, 1 do
		--turtle.select(i)
		--turtle.dropDown()
	--end

--turtle.select(16)
--turtle.dropUp()
--print ("Turtle is empty. Thanx for using CraftyTurtle. See you soon.")


--end

thanx in advance
Edited on 21 April 2014 - 05:58 PM
CometWolf #7
Posted 21 April 2014 - 07:55 PM
Please use CODE TAGS, and why is every line seperate by blank a line?

You still don't understand how for loop work.

for i = turtle.select(1, 12) do --go through slots 1 to 12
It's throwing the error because the for loop expects atleast 2 values, one starting number, and one ending number. You can also add an optional third value, which is the step amount, by default it's 1.
What the for loop actually does, is save the first number to the variable name(i), then run the loop, and when it's done, increase i by whatever the step is. This process will repeat until i is greater than the second value.

If we want the turtle to check the slots 1-12 we would do the follwing

for i=1,12 do
  if turtle.getItemCount(i) == 0 then
	--empty slot
  end
end

Also, this loop will never run

while turtle.suckDown == true do --as long as there is materials in chest below
since turtle.suckDown is a function, and thus not equal to true. Remember to call the function.

This won't work either

while x == 1 do
while turtle.dropUp() == true do --if there is space left in chest

elseif turtle.getItemCount(slotTable[turtle.getSelectedSlot()]) == c then --if this returns c the function shall close
print("All slots full!")
x = x+1 --this closes the function
else
end
Since the second loop will still run regardless of what x is. Meaning that you can either add the check to the second loop aswell, or use a break statement, which ends whatever loop is currently running. I also don't see the variable c defined anywhere.
Edited on 21 April 2014 - 06:01 PM
RolandLundvall #8
Posted 21 April 2014 - 10:01 PM
Sry, I missed the code tag… fixed now. And there is a blank line between to make it easier to read.

I looked at this example and thought it was valid code since no one said anything about it:


if turtle.getItemCount(turtle.getSelectedSlot()) &amp;lt; 0 then
print("Not Enough Items!")
for i = turtle.select(1, 16) do
turtle.dropUp()
end
else
craft()
end

My mistake of assuming it was right. I was confused, since I thought it was a strange for-loop. But since noone else complained I thought it was right. There, now we can leave it…

I want it to go through slots 1 to 12. But I do not want it just to go through the slots and see if there are items in them. I want it to compare the current slot number with the table to see weather it should place any items in it or not. I have put in some prints to see where it stops. Right now it stops in the function fill() at line 40. Attempts to call nil:


if slotTable[i] == y then		--look in tablearray

I have changed it between the above and


if slotTable[turtle.getSelectedSlot()] == y then

But I get the same result, even with the y inside "y".
what am I doing wrong? Is it turtle.select(i) that is wrong? When I try to get it to print it with:


print(turtle.getSelectedSlot())

But it seems that it does not return the number for the selected slot, as it should do.
I could always write out every slot manually, but that would create a whole lot of code…

the full code:
[spolier]

--CraftyTurtle

function empty()

print ("Program terminated, emptying slots.")

	for i = 1, 12, 1 do
		turtle.select(i)
		turtle.dropDown()
	end --for end

turtle.select(16)
turtle.dropUp()
print ("Turtle is empty. Thanx for using CraftyTurtle. See you soon.")
b = b+1

end --function end


function fill()		 --fill inventory with contents

print("starting function fill")

print ("Creating table.")

slotTable = {"y","y","y","n","n","y","n","n","y","y","y","c"}		 --create table of the slots to be used and not in the turtles inventory. When it comes to "c" (close) program part should terminate.

print ("Table created")
print("Testing table")

for i=1, 12, 1 do
print(slotTable[i])
print("This was slotTable")
end

local x = 1
print("after local x")
	while x == 1 do
	print("Inside while x == 1")
		while turtle.suckDown do								--as long as there is materials in chest below
		print("Inside while suckDown")
			for i = 1, 12, 1 do		--go through slots 1 to 12
			print(i)
			turtle.select(i)
			print("turtle.getSelectedSlot =")
			print(turtle.getSelectedSlot())
		   	
				if slotTable[i] == y then		--look in tablearray
				turtle.suckDown()								--fill the slot

					if turtle.getItemCount(turtle.getSelectedSlot()) == 64 then			--if the selected slot has 64 items in it

					print ("Slot is full, moving to next.")

					else

					print ("Slot is not full, going to function wait()")		--otherwise go to function wait
					wait ()								--or this? To end it I mean...

					end --if end

				elseif slotTable[turtle.getSelectedSlot()] == n then		--if this returns n it means slot should stay empty
				print ("This slot is not in use.")

				elseif slotTable[turtle.getSelectedSlot()] == c then		--if this returns c the function shall close
				print("All slots full!")
				x = x+1											--this closes the function
				else
				end  --if-elseif-end


			end --for-loop end
		end --while end
	end  --while end

end --function end








function make()
print("starting function make")

local x = 1

	while x == 1 do

		while turtle.dropUp() == true do		 --if there is space left in chest
			turtle.select(16)			 --select slot 16

				if turtle.getItemCount(turtle.getSelectedSlot()) == 0 then		 --if slot 16 in empty
					turtle.craft()							 --make things with the materials in the slots filled
					turtle.dropUp()						 --put things in chest above
					print ("Tillverkade en stack")

				elseif turtle.dropUp ~= false then					 --if chest above has space left
					turtle.dropUp()					 --empty slot 16 to chest
					turtle.craft()						 --make the things
					turtle.dropUp()					 --put things in chest above
					print ("Tillverkade en stack")
					x = x+1							--terminates the function
				else

					print ("Målkista är full!!!!")		 --if chest is full it will be prompted. If the chest becomes full during work (as in not full before function starts), I assume both prints would be done (this and the one below)?
					a = 3

				end --if-elseif end
		end   --while end	

		print ("Målkistan är full!")	--if turtle.dropUp returns false chest is full and player will be prompted this
		x = x+1	

	end --while end
end --function end



function wait()

print("starting wait")

local x = 1

	while x == 1 do

		if a == 2 then

			while not turtle.suckDown() do			 --while there are no materials in chest
			print ("Väntar på material.....")		 --print waiting for mats..


			end  --while end

		elseif a == 3 then

			while not turtle.dropUp() do	--I assume this means that as long as chest above is full 'turtle.dropUp' will return false?
			print ("Chest full. Waiting for user to empty it.")

			end --while end

		else
		print ("a was neither 2 nor 3...")
		end --if-elseif end
	end --while end
end --function end

--main program

print ("Welcome to CraftyTurtle.")
print("Place a chest under and above the turtle.")
print("This program will terminate when chest below is empty.")

a = 1
b = 1
d= 1



while b == 1 do
	while d == 1 do
		while turtle.suckDown() do				 --Would this be the same as 'while turtle.suckDown == true do'?
			print("inside first while loop")							
				while turtle.dropUp() do		 --while there is materials in chest below And if there is space in chest on top
				print ("ready to work.")
					fill()	
					make()
				end --while end
			print ("Top chest full. Terminating")			
		end --while end
		d = d +1
	end --while d end
empty()
end --while b end

Edited on 21 April 2014 - 08:06 PM
Lyqyd #9
Posted 21 April 2014 - 10:05 PM
What version of ComputerCraft are you using? The turtle.getSelectedSlot function is not available prior to 1.6.
RolandLundvall #10
Posted 21 April 2014 - 10:18 PM
oh, ofc…. I am using 1.53 which is in the Feed the Beast Unleashed pack… How can I come around this problem then?
CometWolf #11
Posted 21 April 2014 - 10:22 PM
Your usage of it dosen't make any sense either way to be honest, since the turtle dosen't need to select the slot to check it. Just use slotTable
RolandLundvall #12
Posted 21 April 2014 - 11:03 PM
thanx, I will look over the code again tomorrow. Thanx alot for the help and the input.
RolandLundvall #13
Posted 24 April 2014 - 02:17 PM
Just discovered the ME mod. I do not think I will bother to make a turtle for it. Thanx for bothering anyway.