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

Trying to make a woodchopper program but Im very new to programming

Started by TilionDC, 16 February 2014 - 07:11 PM
TilionDC #1
Posted 16 February 2014 - 08:11 PM
Okey So i tried making a woodchopping program but i think im in over my head.
This is a bit too complicated for starting just today…
I have a syntax error close to the "=" in the if statement on line 23 only i cant figure out what is wrong with it :/

Anyways here is the code so far. And I apologize if its messy this is my very first program and i tried to comment it but it seems im not too good with that either.

Spoiler–[[This is my feller program for computercraft. Enjoy! Also Its my very first program so don't hate please…]]


function refuel() –[[This is for refueling the turtle. If false there is a message that the turtle needs more logs]]
local fuelLevel = turtle.getFuelLevel()
if fuelLevel < 30 then
local invCountRefuel = turtle.getItemCount(1)
turtle.select(1)
if invCountRefuel > 2 then
turtle.refuel(2)
return(true)
else
return(false)
end
end


function drop() –[[This function drops the items and returns true when finished]]
local invCounter = 1 –[[This one is make sure that the Item drop for loop was successful]]

turtle.turnLeft()
for i = 0, 15 do –[[After the turtle have turned left it checks if its first slot one, then it drops all logs except for 2 logs. 1 for fuel and 1 to make sure that there always is the right log in slot one]]
local invCount(i) = turtle.getItemCount(i+1)
if i == 0 then
if invCount(0) > 3 then
turtle.select(i+1)
turtle.drop(invcount(0)-3)
end
else
turtle.select(i+1)
turtle.drop(invcount)
end
if itemCount(i) == 0 then
invCounter = invCounter+1
end
if invCounter == 14 then –[[This is for the future, It counts all the slots and makes sure only one has 3 items in it, this should be slot 1]]
turtle.turnRight()
return(true)
else
return(false)
end
end
end
Here is the pastebin link(the program will be updated there though when i figure out more. so the spoiler might actually be more useful for the post.)
http://pastebin.com/NpGfzVyj
CometWolf #2
Posted 17 February 2014 - 03:21 AM
The line 23 "x-1" dosen't mean anything. You calculate a number but don't do anything with it. So it's expecting a = sign. Im guessing what you want is to decrease x by 1. Which would be done like this

x = x-1
You'll probably get the same errors on the other lines where you're doing it similarily.
TilionDC #3
Posted 17 February 2014 - 09:29 AM
The line 23 "x-1" dosen't mean anything. You calculate a number but don't do anything with it. So it's expecting a = sign. Im guessing what you want is to decrease x by 1. Which would be done like this

x = x-1
You'll probably get the same errors on the other lines where you're doing it similarily.
Okey thanks i totally forgot that. I also got to fix some other problems i had now but the parser still found some misstakes

Spoiler

--[[This is my feller program for computercraft. Enjoy! Also Its my very first program so don't hate please...]]
local x, z, y = 0, 0, 0
function refuel() --[[This is for refueling the turtle. If false there is a message that the turtle needs more logs]]
local fuelLevel = turtle.getFuelLevel()
if fuelLevel < 30 then
  local invCountRefuel = turtle.getItemCount(1)
  turtle.select(1)
  if invCountRefuel > 2 then
   turtle.refuel(2)
   return(true)
  else
   return(false)
  end
end
end
function checkLeft()
turtle.turnLeft()
turtle.compare()
if turtle.compare() then
  turtle.dig()
  turtle.forward()
  x = x-1
  return (true)
else
  return (false)
end
end
function checkBack()
turtle.turnLeft()
turtle.compare()
if turtle.compare() then
  turtle.dig()
  turtle.forward()
  z = z-1
  return (true)
else
  return (false)
end
end
function checkRight()
turtle.turnLeft()
turtle.compare()
if turtle.compare() then
  turtle.dig()
  turtle.forward()
  x = x+1
  return (true)
else
  return (false)
end
end
function checkForward()
turtle.turnLeft()
turtle.compare()
if turtle.compare() then
  turtle.dig()
  turtle.forward()
  z = z+1
  return (true)
else
  return (false)
end
end
function check()
turtle.select(1)
checkLeft()
checkBack()
checkRight()
checkForward()
while checkLeft() or checkBack() or checkRight() or checkForward() do
  check()
end
return true
end
function chop() --[[This is to chop down the tree, it will first look for the tree and compare it to the log in invslot 1 then it will chop 1 log down, look on the sides if there are more logs, if there are none it will go up one block and repeat]]
while turtle.compare() do
  turtle.dig()
  turtle.forward()
  while turtle.compareUp() do
   check()
   if check() then
    if x > 0 then
	 turtle.turnLeft()
	 while x > 0 do
	  turtle.forward()
	  x = x-1
	 end
	 if z > 0 then
	  turtle.turnLeft()
	  while z > 0 do
	   turtle.forward()
	   z = z-1
	  end
	 end
	 if z < 0 then
	  turtle.turnRight()
	  while z < 0 do
	   turtle.forward()
	   z = z+1
	  end
	 end
    end
    elseif x < 0 then
	 turtle.turnRight()
	 while x < 0 do
	  turtle.forward()
	  x = x+1
	 end
	 if z < 0 then
	  turtle.turnLeft()
	  while z < 0 do
	   turtle.forward()
	   x = z+1
	  end
	 end
	 if z > 0 then
	  turtle.turnRight()
	  while z > 0 do
	   turtle.forward()
	   z = z-1
	  end
	 end
    end
    elseif z > 0 then
	 turtle.turnLeft()
	 turtle.turnLeft()
	 while z > 0 do
	  turtle.forward()
	  z = z-1
	 end
    end
    elseif z < 0 then
	 while z < 0 do
	  turtle.forward()
	  z = z+1
	 end
    end
    elseif z and x == 0 then
	 turtle.digUp()
	 turtle.up()
    end
   end
  end
end
end
function drop() --[[This function drops the items and returns true when finished]]
local invCounter = 1 --[[This one is make sure that the Item drop for loop was successful]]
turtle.turnLeft()
for i = 0, 15 do --[[After the turtle have turned left it checks if its first slot one, then it drops all logs except for 2 logs. 1 for fuel and 1 to make sure that there always is the right log in slot one]]
  local invCount(i) = turtle.getItemCount(i+1)
  if i == 0 then
   if invCount(0) > 3 then
    turtle.select(i+1)
    turtle.drop(invcount(0)-3)
   end
  else
   turtle.select(i+1)
   turtle.drop(invcount[i])
  end
  if itemCount(i) == 0 then
   invCounter = invCounter+1
  end
  if invCounter == 14 then --[[This is for the future, It counts all the slots and makes sure only one has 3 items in it, this should be slot 1]]
   turtle.turnRight()
   return(true)
  else
   return(false)
  end
end
end
So i got a problem with missing an end in line 127 after the while. only I cant find any missing end and i dubblechecked. What could else be wrong?

Also i tried parsing the drop function at line 150 but it says there is a syntax error with = at line 155. What could be wrong cause i cant see any problem.
CometWolf #4
Posted 17 February 2014 - 11:07 AM
Assuming your indentation is correct, this is non-sensical

							    end
							    elseif x < 0 then
You don't need to close an if followed by an elseif. this begins at line 107 by the way.

Line 150 is way off base. You can't pass an argument to a variable, let alone one you try to define at the same time.

			    local invCount(i) = turtle.getItemCount(i+1)
TilionDC #5
Posted 17 February 2014 - 04:35 PM
so instead of doing



for i = 0, 10 do
    local invCount(i) = turtle.getItemCount(i+1)
end

I could do


for i = 0, 10 do
    local j = i
    local invCount(j) = turtle.getItemCount(j +1)
end

Or would I need to make an array of invCount and then get the turtle.getItemCount the same way i did before? Do you have any examples if im still incorrect?
surferpup #6
Posted 17 February 2014 - 05:49 PM

local invCount = {}
for i = 1,11 do
  invCount[i] = turtle.getItemCount(i)
end

This will store the number of items in slots 1-11 in your turtle inventory into a table named invCount, such that:

invCount[1] = count of items in slot 1
invCount[2] = count of items in slot 2
… and so on.

If a slot is empty, it will return a 0 for the count.
Bomb Bloke #7
Posted 17 February 2014 - 09:32 PM
To be clear, "invCount()" (using rounded brackets) assumes "invCount" is a function and tries to call that function. "invCount(i)" would try to call that function and pass it "i" as a parameter.

"invCount" (using square brackets) treats "invCount" as a table, and refers to the index in that table with the key name/number of "i".

A variable must already point to a function before you can attempt to call it, or to a table if you wish to index it. That first line in surferpup's example sets "itemCount" to point to a new, empty table.
surferpup #8
Posted 17 February 2014 - 10:04 PM
[member='Bomb Bloke'] is correct. No matter how it is done, attempting to use invCount(i) = someVariable will never work, as you are attempting to assign the outcome of a function ( a no-no). My example accomplished what I saw as your intent – which was to use a table.
Edited on 17 February 2014 - 09:04 PM
TilionDC #9
Posted 21 February 2014 - 08:52 AM
Okey so i redid my woodchopping program from scratch because it was crap. Im still having problems with the arrays though.

So this is my chest dropping function. Its not working very well

function chest()
turtle.turnLeft()
turtle.turnLeft()
local dropCount = {}
for i = 1, 13 do
  if i == 1 then
   turtle.select(1)
   local getCount = turtle.getItemCount()
   local keepOne = getCount - 1
   turtle.drop(keepOne)
  else
   dropCount[i] = turtle.select(i)
   turtle.drop()
  end
end
turtle.turnLeft()
turtle.turnLeft()
turtle.select(1)
end
For some reason its expecting a number.

What im trying to do is create an array that will check every slot from 1 - 13 and drop all items except for 1 item in slot 1.
CometWolf #10
Posted 21 February 2014 - 09:09 AM
Not entirely sure what dropCount is supposed to be, but turtle.select() dosen't return any values.

   dropCount[i] = turtle.select(i)

Or well, it returns true… but it's not like it could ever fail.

Anyways, since you didn't point out what line expects a number, im gonna take a guess and say it's this one

   local getCount = turtle.getItemCount()
Because getItemCount does indeed expect a number…
Edited on 21 February 2014 - 08:26 AM
TilionDC #11
Posted 21 February 2014 - 09:30 AM
Oh i figured it out. I forgot to put a number in the turtle.getItemCount() variable.
Now it works. I still have one problem though i cant figure out what bugging it.

http://pastebin.com/Eeir6xdA

If you run the program with no sapling in its inventory at the start and you have saplings in a chest under it. and the tree is already grown in front of it. it will say "Turtle blocked".

It runs the detect function but it doesnt go foward in the "if compare == true then" section. Do you guys know what could be wrong? is it maybe that the block doesn't update?=

Not entirely sure what dropCount is supposed to be, but turtle.select() dosen't return any values.

   dropCount[i] = turtle.select(i)

Or well, it returns true… but it's not like it could ever fail.

Anyways, since you didn't point out what line expects a number, im gonna take a guess and say it's this one

   local getCount = turtle.getItemCount()
Because getItemCount does indeed expect a number…

Oh i guess you beat me to it.. Thanks anyways. Do you think you know what could be wrong?
CometWolf #12
Posted 21 February 2014 - 11:47 AM
Missing fuel? idk…
Post a picture, please.