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

Problem with (if then) or Turtle(api)

Started by BaneIronhand, 20 December 2012 - 05:43 PM
BaneIronhand #1
Posted 20 December 2012 - 06:43 PM
Trying to write a simple mining program to tunnel down a set distance then move over and come back then dump etc. (my second program ever)

Wrote this function to handle refueling.

function juice()

	  turtle.select(1)

	  if turtle.getitemCount(1) < 1 then
		 print ("Feed Me")
		 repeat
		   sleep (5)
		 until turtle.getitemCount(1) > 10
	  end

	  if turtle.getFuelLevel() < 5 then
		 repeat
			print ("Fueling Standby")	
			turtle.select(1)
			turtle.refuel(1)
		 until turtle.getFuelLevel() > 50
	  end

end

I am getting an "attemp to call nil" error on the first "if" statement. (should be coloured red) My problem is that I wrote the bottom "If" statement (coloured blue) for my first program and it works just fine. My question is do i have a problem with the lua syntax or the Turtle (api) or something else entirely.

I've put the whole program below for anyone who would like to look and see. Please keep in mind till a week ago i had no idea lua or computercraft existed. The blue text is my notes to myself while trying to learn this stuff. I've also never got this program to run past the error reprorted above so bugs, random capital letters, and other assorted screwups are still to be found therein.

P.S. If you know how to keep the formatting for text pasted into a post you could tack that on to your answer as well.. :huh:/>
Spoiler
--variable thingy   I have very little idea how this part works I "borrowed" this bit from somewhere. i'm pretty sure it sets a variable from a number inputed after program command.

turtle.select(1)
local tArgs = { ... }
local togo = tonumber(tArgs[1]) --sets the tArgs variable string to a number usable by the program...I think
togo = togo or 1 -- No idea why this is line here

--fuel function. Pause if out of fuel (put fuel in slot 1)

function juice()
  
turtle.select(1)

   if turtle.getitemCount(1) < 1 then
	  print ("Feed Me")
	  repeat
		 sleep (5)
	  until turtle.getitemCount(1) > 10
   end

   if turtle.getFuelLevel() < 5 then
	repeat
	   print ("Fueling Standby")
	   turtle.select(1)
	   turtle.refuel(1)
	until turtle.getFuelLevel() > 50
end
end

--Tunnel functions

function clearud()

   if turtle.detectUp() then
	  turtle.digUP()
   end

   if turtle.detectDown() then
	 turtle.digDown()
   end
end

function dig()

   juice()

   if turtle.detect() then

	   repeat
			turtle.dig()
			sleep(0.5)

	   until turtle.detect() == false

		   turtle.forward()
		   clearud()

   else

	   turtle.forward()
	   clearud()

   end
end

--turn arount at far end function

function gohome()

   turtle.turnRight()

   for i = 1, 3 do
	  dig()
   end

   turtle.turnRight()

end

--Dump and turn at end Function

function dumpturn()

   print ("Unloading standby...")

   local x=1

   while x<16 do   --unload slots 2-16
	   x=x+1
	   turtle.select(x)
	   turtle.drop()
   end

   turtle.turnLeft()

   for i = 1, 3 do
	 dig()
   end

   turtle.turnLeft()

end

---Main program

for i = 1, 10 do

   for i = 1, togo do
	  dig()
   end

   gohome()

   for i = 1, togo do
	  dig()
   end

   dumpturn()
end
Edited by
theoriginalbit #2
Posted 20 December 2012 - 06:48 PM
firstly please use code tags around code:

without the spaces of course

secondly Lua, like most languages is case sensitive. so when you are calling turtle.getitemCount you should actually be calling turtle.getItemCount notice the capital I :)/>

EDIT: Everything that dan200 and the other devs created uses camel case, so the first word is lower case, the rest of the words have the front letter capital.
NabsterHax #3
Posted 20 December 2012 - 07:43 PM
Edit: whoops, I'm too tired for this.
BaneIronhand #4
Posted 20 December 2012 - 08:51 PM
Thanks guys. and yea too tired is right. Stared at that line for over an hour. Must have compared it to the turtle(api) chart a least 10 times and made sure i capitalized the C. :rolleyes:/>

Thanks for the explanation on posting code I spent some time looking around the forum help section never found that bit anywhere. But then we've established how observant i am already. I'll have to look it over again later.

Note to self…No more posting after bedtime.