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

Error I can't figure out

Started by Zachaq, 04 March 2012 - 02:03 AM
Zachaq #1
Posted 04 March 2012 - 03:03 AM
I've been playing with CC a few days, I haven't gotten into programming before, so I'm learning as I go. I'm trying to use an if statement, but it keeps returning an '=' expected error. I know what this means, and I've tried fixing it, but nothing I do to it helps. I even tried it alone, without the function it's in, and it seemed to work okay. Hopefully it isn't something too obvious, but here it is:


function placetorch() --puts a torch (slot 4) above the turtle
turtle.turnRight()
turtle.forward()
turtle.up()
turtle.turnRight()
turtle.turnRight()
turtle.forward
if turtle.detect() == true then  --Error on this line. Not sure why.
   turtle.back()
   turtle.select(4)
   turtle.place()
   turtle.down()
   turtle.forward()
   turtle.turnRight()
else
   turtle.down()
   turtle.turnRight()
end
end


Also, if anyone would like to read over the rest of the code, I would appreciate the insight. It's a program that digs a 2x2 tunnel, places Industrial Craft miners with a chest every ninth block, puts down chunkloaders every so often, and also puts torches on the wall, if there is a wall. Just a note, I know I should use local variables vs globals, but I can't make them work either, some tips on that would be welcome.


function check() -- checks for a block and digs it. Works with sand and gravel
while turtle.detect() == true do
   sleep(1)
   turtle.dig(1)
   sleep(1)
end
end
function dig() -- tunnels a 2x2x2 cube forward
check()
turtle.forward(1)
turtle.turnRight(1)
check()
turtle.digUp(1)
turtle.forward(1)
turtle.digUp(1)
turtle.turnLeft(1)
check()
turtle.forward(1)
turtle.digUp(1)
turtle.turnLeft(1)
check()
turtle.forward(1)
turtle.digUp(1)
turtle.turnRight(1)
end
function checkminer() --are the miners there and in the right slot?
if turtle.getItemCount(1) > 0 and turtle.getItemCount(2) > 0 then
  hasminer = true
  else
  hasminer = false
end
end
function placetorch() --puts a torch (slot 4) above the turtle
turtle.turnRight()
turtle.forward()
turtle.up()
turtle.turnRight()
turtle.turnRight()
turtle.forward
if turtle.detect() == true then  --Error on this line. Not sure why.
   turtle.back()
   turtle.select(4)
   turtle.place()
   turtle.down()
   turtle.forward()
   turtle.turnRight()
else
   turtle.down()
   turtle.turnRight()
end
end
function cycle() -- digs 9 length tunnel and places one set of miners, and a torch on the wall
dig()
dig()
placetorch()
dig()
dig()
dig()
turtle.turnLeft()
turtle.turnLeft()
turtle.forward()
turtle.forward()
turtle.forward()
turtle.forward()
turtle.forward()
turtle.forward()
turtle.forward()
turtle.forward()
turtle.select(1)
turtle.place(1)
turtle.back()
turtle.select(2)
turtle.place(1)
turtle.turnLeft(1)
turtle.turnLeft(1)
turtle.forward()
turtle.forward()
turtle.forward()
turtle.forward()
turtle.forward()
turtle.forward()
end
function chunk() -- drops a chunkloader (slot 3) behind it
turtle.turnLeft(1)
turtle.turnLeft(1)
turtle.select(3)
turtle.place(1)
turtle.turnLeft(1)
turtle.turnLeft(1)
end
function checkinv() -- dumps inventory if it is full
if turtle.getItemCount(5) >= 32 then
  turtle.select(5)
  turtle.drop(5)
end
if turtle.getItemCount(6) >= 32 then
  turtle.select(6)
  turtle.drop(6)
end
if turtle.getItemCount(7) >= 32 then
  turtle.select(7)
  turtle.drop(7)
end
if turtle.getItemCount(8) >= 32 then
  turtle.select(8)
  turtle.drop(8)
end
if turtle.getItemCount(9) >= 32 then
  turtle.select(9)
  turtle.drop(9)
end
end
term.clear()
term.setCursorPos(1,1)
sleep(1)
print("Mining Sequence initiated.")
sleep(1)
print("Please check that the Bot is exactly 1 block behind where you want the first miner to go, and that it is facing the direction you want it to mine")
sleep(1)
word = nil
while word == nil do --surely there is a better way to do this bit
print("Enter 'confirm', without the quotes, to continue")
local word = io.read()
if word == ("confirm") then
   term.clear()
   term.setCursorPos(1,1)
   print("Now make sure that there are miners in the top left slot, chests in the top middle slot, chunkloaders in the top right slot, and torches in the middle left slot.")
   sleep(1)
else
   term.clear()
   term.setCursorPos(1,1)
end
end
word2 = nil
while word2 == nil do
print("Enter 'confirm', without the quotes, to continue")
local word2 = io.read()
if word2 == ("confirm") then
   term.clear()
   term.setCursorPos(1,1)
   print("Confirmation successful. Stand back, and let the Bot do its thing. Be sure to report any unusual behavior.")
   sleep(5)
else
   term.clear()
   term.setCursorPos(1,1)
end
end
cyclenumber = 0 --controls chunk drops
checkminer()
if hasminer == false then
print("Out of Miners, stopping.")
end
while hasminer == true do -- the looper to keep this going
cycle()
checkminer()
if hasminer == false then
   term.clear()
   term.setCursorPos(1,1)
   print("Out of Miners, stopping.")
end
checkinv()
cyclenumber = cyclenumber + 1
if cyclenumber == 2 then
  chunk()
  cyclenumber = 0
end
end

I tried to reindent it after copying, but I promise the orignal was formatted better
Whoaaaaa #2
Posted 04 March 2012 - 01:10 PM
Your code :
-- Your code :
function placetorch() --puts a torch (slot 4) above the turtle
turtle.turnRight()
turtle.forward()
turtle.up()
turtle.turnRight()
turtle.turnRight()
turtle.forward
if turtle.detect() == true then  --Error on this line. Not sure why.
 turtle.back()
turtle.select(4)
turtle.place()
turtle.down()
turtle.forward()
turtle.turnRight()
else
   turtle.down()
   turtle.turnRight()
end
end
The correct code :

function placetorch() puts a torch (slot 4) above the turtle
turtle
.turnRight()
turtle.forward()
turtle.up()
turtle.turnRight()
turtle.turnRight()
turtle.forward
while turtle.detect() do There is Ifs, But the API Lua for Turtles are a bit different. So instead of if turtle.detect() == true, it's while turtle.detect() do
turtle.back()
turtle.select(4)
turtle.place()
turtle.down()
turtle.forward()
turtle.turnRight()
end
while not turtle.detect() do
turtle.down()
turtle.turnRight()
end
MysticT #3
Posted 04 March 2012 - 01:43 PM
Well, the problem is that you forgot the parentheses after the turtle.forward call, it should be:

...
turtle.forward()
if ...
That's it, the if is ok :unsure:/>/>
Zachaq #4
Posted 04 March 2012 - 08:05 PM
Well, the problem is that you forgot the parentheses after the turtle.forward call, it should be:

...
turtle.forward()
if ...
That's it, the if is ok :unsure:/>/>

*checks code*
Sure enough…… Why was that making the if line error though?

Another thing I was wondering about, my original idea for this was just to have it tunnel as far as you want it to, similiar to the Tunnel program. I had it put down chunkloaders (from Additional pipes for Build Craft) every so often, so it could keep going even when no one was around. However I found that even with those chunkloaders, the turtle would restart when the player leaves the area. This is on SMP. Is that intended behavior?
Casper7526 #5
Posted 04 March 2012 - 08:16 PM
I've seen the same thing, I never messed with it too much, but they don't seem to keep the chunk loaded the way it should be. I didn't try placing a startup program to see if it's just be cause the chunk unloads for 1 frame causing the turtle to reboot though.
Zachaq #6
Posted 04 March 2012 - 08:31 PM
Hmm, didn't think of that, would be tricky to have that with user input though. Do global varibles persist when it reboots?
Liraal #7
Posted 04 March 2012 - 09:19 PM
nope, they don't. you have to save it to a file.
Zachaq #8
Posted 04 March 2012 - 09:35 PM
Ah, that's what I thought. Should be fun to work that out. Thanks guys!