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

Multiple Variable with read() Issue

Started by mikey53591, 30 December 2012 - 03:45 PM
mikey53591 #1
Posted 30 December 2012 - 04:45 PM
Hi there. I seem to be having an issue assigning multiple variables using the read() function. For some reason, I seem to get the error "progtunnel:140: attempt to concatenate sting and nil". I'd really appreaciate if someone could check out my code and help me figure out what i did wrong. And if you're wondering, I'm making a 3x3 tunnel bore with user input for the depth that it digs and I'm slowly figuring out how to auto-check the inventory for full stacks, make it return to the chest it placed, and return to it's last dig cycle and continue digging. So far I'm only about 30% through all that… Oh well. Anyways, the help would be much appreciated, so here's the code I've got so far. I marked the issue lines and have sectioned off all the function defining and chunks and such what.


--Clearing the Screen--
term.clear()
term.setCursorPos(1,1)
print[["3x3 Tunnel Turtle v1.0"
=======================================
]]
--Initial Coordinates--
rednet.open("right")
xcor,ycor,zcor=gps.locate(5)
if xcor~=nil then
  print("Starting Coords:")
  print("x = "..xcor)
  print("y = "..ycor)
  print("z = "..zcor)
  print(" ")
else
  print("GPS System Offline")
  error()
end
--Defining checkInv() Function--
local function checkInv()
  x=0
  for i=1,16 do
	turtle.select(i)
  x=turtle.getItemCount(i)
	  if x==64 then
		print("Full Slot. Returning to Chest")
		--Space for Return Program--
		break
	  else
	end
  end
end
--User Input for Tunnel Length--
term.write("Length of Tunnel? ")
l,lr=read()  --Line that's supposed to define lr to find Distance Traveled--
print(" ")
print("Digging for "..l.. " Cycles.")
print(" ")
--Placment of Chest for Depositing--
turtle.turnLeft()
if turtle.detect()==true then
  turtle.select(2)
  turtle.dig()
  turtle.select(1)
	if turtle.getItemCount(1)==1 then
	  print("Placing Chest.")
	  turtle.place()
	else
	  print("No Storage Found. Returning.")
	  error()
	end
else
	if turtle.getItemCount(1)==1 then
	  print("Placing Chest.")
	  turtle.place()
	else
	  print("No Storage Found. Returning.")
	  error()
	end
end
turtle.turnRight()
--Digging Loop and Distance Counter for Returning--
for i=1,l do
  if turtle.detect()==true then
	turtle.dig()
	turtle.forward()
  else
	turtle.forward()
  end

  turtle.turnLeft()
  
	for x=1,2 do
	  if turtle.detect()==true then
		turtle.dig()
		turtle.turnLeft()
		sleep(1)
		turtle.turnLeft()
	  else
		turtle.turnLeft()
		sleep(1)
		turtle.turnLeft()
	  end
	
	  if turtle.detect()==true then
		turtle.dig()
		turtle.turnRight()
		sleep(1)
		turtle.turnRight()
	  else
		turtle.turnRight()
		sleep(1)
		turtle.turnRight()
	  end
  
	  if turtle.detectUp()==true then
		turtle.digUp()
		turtle.up()
	  else
		turtle.up()
	  end
	end
	
	  if turtle.detect()==true then
		turtle.dig()
		turtle.turnLeft()
		sleep(1)
		turtle.turnLeft()
	  else
		turtle.turnLeft()
		sleep(1)
		turtle.turnLeft()
	  end
	
	  if turtle.detect()==true then
		turtle.dig()
		turtle.turnLeft()
	  else
		turtle.turnLeft()
	  end
  
  turtle.down()
  sleep(1)
  turtle.down()
  checkInv()
  turtle.select(1)
  print("Distance Traveled: "..lr) --Line with the Issue--
end
snoble2022 #2
Posted 30 December 2012 - 05:01 PM
You can't assign multiple vars to read(). In your case just put lr = read() and put l = lr after the read function
mikey53591 #3
Posted 30 December 2012 - 05:04 PM
Ok thanks! I'll give it a try and see how it goes.
remiX #4
Posted 30 December 2012 - 09:52 PM
I see you eventually the variable 'l' in a for loop, so you will need to convert it into a number to achieve this successfully,
tonumber(l)

But yeah, the easiest way to set multiple variables as one read() is set the first value to another, etc.
mikey53591 #5
Posted 31 December 2012 - 03:10 AM
I fixed the code :)/> Here's what i've got so far. Now i'm having an issue with dropping the items off into a chest… It seems to not wanna detect the inventory and gives a turtle error.
Spoiler

--Clearing the Screen--
term.clear()
term.setCursorPos(1,1)
print[["3x3 Tunnel Turtle v1.0"
=======================================
]]
--Initial Coordinates--
rednet.open("right")
xcor,ycor,zcor=gps.locate(5)
if xcor~=nil then
  print("Starting Coords:")
  print("x = "..xcor)
  print("y = "..ycor)
  print("z = "..zcor)
  print(" ")
else
  print("GPS System Offline")
  error()
end
--User Input for Tunnel Length--
term.write("Length of Tunnel? ")
local l=read()
local dt=0
local nd=0
local dr=0
print(" ")
print("Digging for "..l.. " Cycles.")
print(" ")
--Table for Variables in the Whole Program--
local Vars={dt, nd, dr, l}
--dt=Distance Traveled; Used in: digLoop()--
--nd=New Distance; Used in: contDigLoop()--
--dr=Distance Remaning; Used in: contDigLoop()--
--l=Length of Tunnel; Used in: digLoop()--
--Placment of Chest for Depositing--
turtle.turnLeft()
if turtle.detect()==true then
  turtle.select(2)
  turtle.dig()
  turtle.select(1)
	if turtle.getItemCount(1)==1 then
	  print("Placing Chest.")
	  turtle.place()
	else
	 print("No Storage Found. Returning.")
	 error()
	 end
else
  if turtle.getItemCount(1)==1 then
	print("Placing Chest.")
	turtle.place()
  else
	print("No Storage Found. Returning.")
	error()
  end
end
turtle.turnRight()
--Defining returnSystem() Function--
local function dropOff()
  Vars[1]=dt
  Vars[4]=l
  go=dt+1
  turtle.turnLeft()
  sleep(0.5)
  turtle.turnLeft()
  for i=1,go do
	turtle.forward()
  end

  turtle.turnRight()
  if turtle.detect()==true then
	for x=1,16 do
	  turtle.select(i)
	  turtle.drop()
	end
  else
	print("No Chest Detected. Error.")
	error()
  end

  turtle.turnRight()
  for i=1,go do
	turtle.forward()
	sleep(0.5)
  end
  contDigLoop()
end
local function checkInv()
  for i=1,16 do
	turtle.select(i)
	x=turtle.getItemCount(i)
	if x==64 then
	  turtle.select(1)
   print("Full Slot. Returning to Chest")
	  return true
	else
end
  end
turtle.select(1)
end
local function returnSystem()
  if checkInv()==true then
	dropOff()
  end
end
--Digging Loop Function and Distance Counter for dropOff()--
local function digLoop()
  Vars[4]=l
  for i=1,l do
	if turtle.detect()==true then
	  turtle.dig()
	  turtle.forward()
	else
	  turtle.forward()
	end

  turtle.turnLeft()
  
	for x=1,2 do
	  if turtle.detect()==true then
		turtle.dig()
		turtle.turnLeft()
		sleep(0.5)
		turtle.turnLeft()
	  else
		turtle.turnLeft()
		sleep(0.5)
		turtle.turnLeft()
		end
		
	  if turtle.detect()==true then
		turtle.dig()
		turtle.turnRight()
		sleep(0.5)
		turtle.turnRight()
	  else
		turtle.turnRight()
		sleep(0.5)
		turtle.turnRight()
		end
  
	  if turtle.detectUp()==true then
		turtle.digUp()
		turtle.up()
	  else
		turtle.up()
		end
	  end
		
	if turtle.detect()==true then
		turtle.dig()
		turtle.turnLeft()
		sleep(0.5)
		turtle.turnLeft()
	else
		turtle.turnLeft()
		sleep(0.5)
		turtle.turnLeft()
	  end
		
	if turtle.detect()==true then
	  turtle.dig()
	  turtle.turnLeft()
	else
	  turtle.turnLeft()
	end
  
	turtle.down()
	sleep(0.5)
	turtle.down()
	returnSystem()
	turtle.select(1)
Vars[1]=dt
dt=(l/l)+dt
	print("Distance Traveled: "..dt)
if table.insert(Vars, 1, dt)~=true then
  table.remove(Vars, 1, dt)
  table.insert(Vars, 1, dt)
else
  table.insert(Vars, 1, dt)
end
  end
end
--Defining contDigLoop()--
local function contDigLoop()
  Vars[1]=dt
  Vars[4]=l
  nd=l-dt
  if table.insert(Vars, 2, nd)~=true then
table.remove(Vars, 2, nd)
table.insert(Vars, 2, nd)
  else
table.insert(Vars, 2, nd)
  end
  for i=1,nd do
	if turtle.detect()==true then
	  turtle.dig()
	  turtle.forward()
	else
	  turtle.forward()
	end

  turtle.turnLeft()
  
	for x=1,2 do
	  if turtle.detect()==true then
		turtle.dig()
		turtle.turnLeft()
		sleep(0.5)
		turtle.turnLeft()
	  else
		turtle.turnLeft()
		sleep(0.5)
		turtle.turnLeft()
		end
		
	  if turtle.detect()==true then
		turtle.dig()
		turtle.turnRight()
		sleep(0.5)
		turtle.turnRight()
	  else
		turtle.turnRight()
		sleep(0.5)
		turtle.turnRight()
		end
  
	  if turtle.detectUp()==true then
		turtle.digUp()
		turtle.up()
	  else
		turtle.up()
		end
	  end
		
	if turtle.detect()==true then
		turtle.dig()
		turtle.turnLeft()
		sleep(0.5)
		turtle.turnLeft()
	else
		turtle.turnLeft()
		sleep(0.5)
		turtle.turnLeft()
	  end
		
	if turtle.detect()==true then
	  turtle.dig()
	  turtle.turnLeft()
	else
	  turtle.turnLeft()
	end
  
	turtle.down()
	sleep(0.5)
	turtle.down()
	returnSystem()
	turtle.select(1)
	Vars[3]=dr
Vars[1]=dt
Vars[2]=nd
dr=((nd/nd)+1)+dt
print("Distance Traveled: "..dr)
if table.insert(Vars, 3, dr) ~=true then
  table.remove(Vars, 3, dr)
  table.insert(Vars, 3, dr)
else
  table.insert(Vars, 3, dr)
end
  end
end
digLoop()
remiX #6
Posted 31 December 2012 - 05:28 AM
Can you tell us what the error is exactly, with the line number too. Also put on pastebin so we do not have to count the lines to get to the right line