←  Ask a Pro

ComputerCraft | Programmable Computers for Minecraft

»

Program error.

detsuo04's Photo detsuo04 13 Jan 2013

Ok so i started messing with computer craft today and this is my result so far. However Im getting an error. string miner :93: "=" expected, or something to that effect.
The first chunk just deals with its initial startup and communication. all that is tested and works.
i have gone over the code several times but im not sure whats wrong. If anyone could give it a look i would apreciate it.
Thanks for the help.

rednet.open("right")
rn = rednet.send
fx = turtle.getItemCount(16)
a = fx - 1
sleep(1)
rn (base , "Connection established")
sleep(1)
rn (base , "getting fuel level")
sleep(1)
turtle.getFuelLevel()
local f = turtle.getFuelLevel()
sleep(1)
rn (base , "Current fuel level is "..f.."" )
sleep(1)
if f < 100 then
  turtle.getItemCount(16)
  rn (base , "current coal stack is")
  turtle.select(16)
  turtle.refuel(a)
    else
    rn (base , "Fuel levels are acceptable")
    sleep(1)
  end
sleep(1)
rn (base , "getting coal count")
turtle.getItemCount(16)
sleep(1)
rn (base, "I have "..fx.." Coal")
sleep(1)
if fx < 64 then
  rn (base, "DANGER! Coal is low")
  os.reboot() 
  else
    rn (base, "Coal storage is full")
    sleep(2)
	  end
sleep(1)
rn (base , "comencing program")
--=======comence mining=======--
td = turtle.dig
tm = turtle.move
tc = turtle.detect
--===========fuel=============--
turtle.getFuelLevel()
  if f < z + 10 then
  rn (base , "Danger! fuel levels low.")
  turtle.select(16)
  turtle.refuel(a)
  turtle.getFuelLevel()
    if f < z + 10 then
    rn (base , "Insuficient Coal, Returning.")
    home()
    else
    end
  else
  end
--=======return code==========--
function home()
  tcUp(z + 2)
  rn (base , "Mining Operation finished")
  os.shutdown()
  end
--============value===========--
function value()
if turtle.detect() == false
then return false
end
  for i = 1,3 do
    turtle.select(i)
    if turtle.compare() then
    return false
    else
    return true
    end
  end
--============walls===========--
function walls()
  for j = 1,4 do
    if value() then
    td()
    rn (base, "ORE!")
	  else
	  turtle.turn("right")
	  end
end
--=============home===========--
function home()
tmUp(z)
rn (base , "Done, ready for pickup")
os.shutdown

--===========Dig Code=========--
function mine()
fuel()
  tcDown()
   if true then
   tdDown()
   tcDown()
    if true then
    rn (base , "I hit bedrock, Returning")
    home()
	  else
	  tmDown()
	  walls()
	  z = z + 1
	  else
	   tmDown()
	   walls()
	   z = z + 1
	   end
   end
   --===========start=========--
   while true do
   mine()
   end
  
Quote

crazyguymgd's Photo crazyguymgd 13 Jan 2013

you're missing ends to a lot of your functions. The one's I found were to value(), walls(), home(), and mine().
Also, turtle.turnRight() not turtle.turn("right")
In your home() function you use os.shutdown instead of os.shutdown()...
and maybe some other things. try going through your code and seeing if you can find any more problems :P debugging code is like a game, the more problems you find, the more points you get. so far, I'm winning.
Quote

detsuo04's Photo detsuo04 13 Jan 2013

HA ok ill fix the errors and get back to you.

actually im a little fuzzy on what denotes the need for end

im not sure how many to put
Quote

crazyguymgd's Photo crazyguymgd 13 Jan 2013

examples:

function foo()
  ... 
end

if condition then
   ...
end

if condition then
   if condition2 then
      ...
   end
else
  ...
end

while something do
  ...
end

for x = 1, n do
  ...
end

and so on
Quote

detsuo04's Photo detsuo04 13 Jan 2013

ok so i added the ends and fixed the errors you mentioned now im getting a eof error at 56? which i suspect has something to do with that else end pile there. but im not sure whats wrong with it
rednet.open("right")
rn = rednet.send
fx = turtle.getItemCount(16)
a = fx - 1
sleep(1)
rn (base , "Connection established")
sleep(1)
rn (base , "getting fuel level")
sleep(1)
turtle.getFuelLevel()
local f = turtle.getFuelLevel()
sleep(1)
rn (base , "Current fuel level is "..f.."" )
sleep(1)
if f < 100 then
  turtle.getItemCount(16)
  rn (base , "current coal stack is")
  turtle.select(16)
  turtle.refuel(a)
    else
    rn (base , "Fuel levels are acceptable")
    sleep(1)
  end
sleep(1)
rn (base , "getting coal count")
turtle.getItemCount(16)
sleep(1)
rn (base, "I have "..fx.." Coal")
sleep(1)
if fx < 64 then
  rn (base, "DANGER! Coal is low")
  os.reboot() 
  else
    rn (base, "Coal storage is full")
    sleep(2)
	  end
sleep(1)
rn (base , "comencing program")
--=======comence mining=======--
td = turtle.dig
tm = turtle.move
tc = turtle.detect
--===========fuel=============--
turtle.getFuelLevel()
  if f < z + 10 then
  rn (base , "Danger! fuel levels low.")
  turtle.select(16)
  turtle.refuel(a)
  turtle.getFuelLevel()
    if f < z + 10 then
    rn (base , "Insuficient Coal, Returning.")
    home()
    end
    else
    end
  else
end
--=======return code==========--
function home()
  tcUp(z + 2)
  rn (base , "Mining Operation finished")
  os.shutdown()
end
--============value===========--
function value()
if turtle.detect() == false
then return false
end
  for i = 1,3 do
    turtle.select(i)
    if turtle.compare() then
    return false
    end
    else
    return true
    end
end
--============walls===========--
function walls()
  for j = 1,4 do
    if value() then
    td()
    rn (base, "ORE!")
    end
	  else
	  turtle.turnright()
	  end
  end
end
--=============home===========--
function home()
tmUp(z)
rn (base , "Done, ready for pickup")
os.shutdown()
end

--===========Dig Code=========--
function mine()
fuel()
  tcDown()
   if true then
   tdDown()
   tcDown()
    if true then
    rn (base , "I hit bedrock, Returning")
    home()
    end
	  else
	  tmDown()
	  walls()
	  z == z + 1
	  end
	  else
	   tmDown()
	   walls()
	   z == z + 1
	   end
   end
   --===========start=========--
   while true do
   mine()
   end
  
Quote

crazyguymgd's Photo crazyguymgd 13 Jan 2013

well the extra else's aren't necessary so did you try just getting rid of them? also, there are a couple of places where you have
if condition then
   ...
end
else
  ...
end

that first end isn't necessary. so you can find those and get rid of them too.
Quote

detsuo04's Photo detsuo04 13 Jan 2013

just to clerify

if condition then
if condition then
....
end
else
end

right?
Quote

crazyguymgd's Photo crazyguymgd 13 Jan 2013

so nested ifs?

if condition then
  if condition then
    -- do something
  end
else
    -- do something else
end

but you only need that else if you're actually going to do something else. otherwise just an end.
if that's not clear i'm sure there's a wikipedia page that explains ifs and elses better than I can. or someone else on here.
Quote

detsuo04's Photo detsuo04 13 Jan 2013

awsome you are mega helpful. ok so i have finished the edits and the program returns no errors. however it ends without doing anything. kindof confused on this since its giving me no feed back. right now im looking over my dig function for a reason
Quote

crazyguymgd's Photo crazyguymgd 14 Jan 2013

it often helps to put some debugging print statements in this situation. also, make sure you're actually calling the main function after all these edits. sometimes that could get missed.
Quote

theoriginalbit's Photo theoriginalbit 14 Jan 2013

Conditionals are definitely a very important part of programming that everyone needs to know... that being said here goes...

Explaination of if's
Spoiler

Off the top of my head I think I have covered everything, I hope I'm not forgetting anything and that it all makes sense, it made sense in my head ;)

EDIT: Oops just remembered something... editing


Pro tip:
Spoiler
ok now I think thats it ;)
Edited by TheOriginalBIT, 14 January 2013 - 12:07 AM.
Quote

detsuo04's Photo detsuo04 14 Jan 2013

frustration is hitting an all time high. i copy the program from my computer to a disk. then run it to the turtle and copy from disk to turtle No such file.
so i go back and try the copy again. file already exists. i have gone back and forth ten times with different disks trying to preform this simple operation that i have done 100 times already. BLARG coding is killer

Edit ok i got the disk to work again. not sure what happened there

code still doing nothing though. i am going to spend some time and go over it again.

Here it is as it stands incase somebody catches something i missed.. Once agfain thanks for all your help guys
rednet.open("right")
rn = rednet.send
fx = turtle.getItemCount(16)
a = fx - 1
sleep(1)
rn (base , "Connection established")
sleep(1)
rn (base , "getting fuel level")
sleep(1)
turtle.getFuelLevel()
local f = turtle.getFuelLevel()
sleep(1)
rn (base , "Current fuel level is "..f.."" )
sleep(1)
if f < 100 then
  turtle.getItemCount(16)
  rn (base , "current coal stack is")
  turtle.select(16)
  turtle.refuel(a)
    else
    rn (base , "Fuel levels are acceptable")
    sleep(1)
  end
sleep(1)
rn (base , "getting coal count")
turtle.getItemCount(16)
sleep(1)
rn (base, "I have "..fx.." Coal")
sleep(1)
if fx < 64 then
  rn (base, "DANGER! Coal is low")
  os.reboot() 
  else
    rn (base, "Coal storage is full")
    sleep(2)
	  end
sleep(1)
rn (base , "comencing program")
--=======comence mining=======--
td = turtle.dig
tm = turtle.move
tc = turtle.detect
z = 0
--===========fuel=============--
function fuel()
print("1")
turtle.getFuelLevel()
  if f < z + 10 then
  rn (base , "Danger! fuel levels low.")
  turtle.select(16)
  turtle.refuel(a)
  turtle.getFuelLevel()
    if f < z + 10 then
    rn (base , "Insuficient Coal, Returning.")
    home()
	 else
	 end
end
end
--=======return code==========--
function home()
  tcUp(z + 2)
  rn (base , "Mining Operation finished")
  os.shutdown()
end
--============value===========--
function value()
if turtle.detect() == false
then return false
end
  for i = 1,3 do
    turtle.select(i)
    if turtle.compare() then
    return false
    else
    return true
    end
end
--============walls===========--
function walls()
  for j = 1,4 do
    if value() then
    td()
    rn (base, "ORE!")
	  else
	  turtle.turnright()
	  end
  end
end
--=============home===========--
function home()
tmUp(z)
rn (base , "Done, ready for pickup")
os.shutdown()
end

--===========Dig Code=========--
function mine()
print("1")
fuel()
print("2")
  tcDown()
   if true then
   tdDown()
   tcDown()
   print("3")
    if true then
    rn (base , "I hit bedrock, Returning")
    home()
	  else
	  tmDown()
	  print("4")
	  walls()
	  print("5")
	  z = z + 1
	  end
	  else
	   tmDown()
	   walls()
	   z = z + 1
	   end
   end
   --===========start=========--
   while true do
   mine()
   end
end  
Quote

theoriginalbit's Photo theoriginalbit 14 Jan 2013

are you typing in the copy correct? i.e from the disk to the root of the turtle...
Quote

crazyguymgd's Photo crazyguymgd 14 Jan 2013

your while true do mine() end needs to be outside of the mine() function.
Quote

theoriginalbit's Photo theoriginalbit 14 Jan 2013

View Postcrazyguymgd, on 14 January 2013 - 12:23 AM, said:

your while true do mine() end needs to be outside of the mine() function.
As a side note here, you should never call a function recursively unless you have some way for it to return back down the function tree... or else you will get a stack overflow error as your program has run out of space...
Quote

detsuo04's Photo detsuo04 14 Jan 2013

i moved some ends around to acomodate that now its

end
end
end
start
while true do mine()
end

though still dosnt move or return errors

not sure what you mean.
you mean i shouldnt loop the function mine() ?
Quote

theoriginalbit's Photo theoriginalbit 14 Jan 2013

if true then else will NEVER do the else... true can never be false...
Quote

detsuo04's Photo detsuo04 14 Jan 2013

huh? where did i do that?
Quote

theoriginalbit's Photo theoriginalbit 14 Jan 2013

ok I've gone and commented your code with all the mistakes that you have made ( hope i didn't miss any )

http://pastebin.com/eCHabxgj
Quote

crazyguymgd's Photo crazyguymgd 14 Jan 2013

View PostTheOriginalBIT, on 14 January 2013 - 12:41 AM, said:

ok I've gone and commented your code with all the mistakes that you have made ( hope i didn't miss any )

http://pastebin.com/eCHabxgj

I was just getting into doing this as well...
Quote