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

Looking for some help with a Skystone digging script

Started by Chaz, 05 January 2016 - 05:00 AM
Chaz #1
Posted 05 January 2016 - 06:00 AM
Okay, so the basic idea I had in mind was to write a script specifically to dig down into the centre of a Skystone Meteor from Applied Energistics, until it detects the chest in the middle.

The idea I had in mind is that the turtle inspects the block below it, stores that data into two variables - firstBlock and checkBlock. As long as the IDs are the same between the two, it will dig down, move down, increment a counter by 1 and then check the block below it. As soon as that variable changes (Say if it hits the Skystone Chest or if it misses and hits Stone or Dirt), it'll stop, and then loop moving back up for as many steps as it went down.

Unfortunately I'm super rusty with Lua and I'm hitting a '"=" needed' error on line 14, not entirely sure where it's occurring, but I'm absolutely certain that's not the only thing I've done wrong.

Here's what I've got so far:


--[[This program checks the first
block it's standing on, then digs
down and checks the block below it
every time. As soon as the block
changes the loop ends, and the turtle
comes back up.
--]]

local firstBlock=turtle.inspectDown()
local checkBlock=turtle.inspectDown()
local downCounter=1
firstblock()
checkblock()
while checkBlock()=firstBlock() do
  turtle.digDown()
  turtle.down()
  downCounter=downCounter + 1
  checkBlock()
end()
for i=1,downCounter do
  turtle.up()
end()

As always, thank you for checking into this! Turtles are still not my strong point, but I'm gradually figuring things out.
Lignum #2
Posted 05 January 2016 - 12:09 PM
Here's how that code should look:

local firstBlock=turtle.inspectDown()
local checkBlock=turtle.inspectDown()
local downCounter=0 --# must be 0, we haven't moved down yet!

while firstBlock.name == checkBlock.name do --# need two = signs here because we're doing a comparison, we also need to compare the IDs, hence .name
  turtle.digDown()
  turtle.down()
  downCounter=downCounter + 1
  checkBlock = turtle.inspectDown() --# reinitialise checkBlock with the new block
end --# no () after end

for i=1,downCounter do
  turtle.up()
end --# ditto
KingofGamesYami #3
Posted 05 January 2016 - 01:29 PM
Lignum is incorrect. turtle.inspect returns two values - a boolean (true / false) and a table. I would assume you want the table, as you try to index the boolean later on.
Lignum #4
Posted 05 January 2016 - 03:07 PM
Lignum is incorrect. turtle.inspect returns two values - a boolean (true / false) and a table. I would assume you want the table, as you try to index the boolean later on.

Heh, I should probably test my code before posting it. I never knew that it returned two values, thanks for correcting me.
Chaz #5
Posted 05 January 2016 - 04:30 PM
Ahhh, okay. So what should I do if I want to compare the two tables to see if they match? That's the troublesome part for me, I haven't worked a whole lot with tables so I'm sort of entering unexplored territory there.
KingofGamesYami #6
Posted 05 January 2016 - 04:48 PM
You wouldn't compare the tables - you'd compare the contents of the tables. In this case, tbl.name is a string equal to the name of whatever you inspected.
Chaz #7
Posted 05 January 2016 - 05:19 PM
Ah! Alright, I think I understand it now, thank you again!

Now the only problem I'm having is that it keeps telling me a "do" is expected on Line 5, which I'm attempting to do, but it keeps autocompleting "do" into "dofile()" and I can't seem to stop it from doing that.

EDIT: Strange, even when I do fix it so it only says "do" on that part of the loop, it's still demanding a 'do' on Line 5. I must be missing something somewhere.

EDIT 2: Okay, looks like I'm hovering somewhere before a '"do" expected' and an 'attempt to call ? (a boolean value)' while attempting to get the game to read the Name entry in the table that turtle.inpsectDown() returns. Still not fully sure where I'm going wrong here.
Edited on 05 January 2016 - 06:07 PM
Chaz #8
Posted 05 January 2016 - 09:01 PM
ALright, proper update time, I've almost got the program working - it's not spewing errors about Booleans anymore but it's not quite doing what I want it to do. It's digging, but it keeps digging even after it should stop (Namely when the block beneath it reads different from the first block it detected)

Here's my updated script for looking over:


local success, fbn=turtle.inspectDown() --first block name
local success, cbn=turtle.inspectDown() --check block name
local downCounter=0

while fbn.name==cbn.name do
  turtle.digDown()
  turtle.down()
  downCounter=downCounter+1
  local success, cbn=turtle.inspectDown()
end

for i=1,downCounter do
  turtle.up()
end
Lyqyd #9
Posted 05 January 2016 - 09:31 PM
Remove the local from within the loop.
Chaz #10
Posted 05 January 2016 - 09:49 PM
Okay, I've removed the "local success," part from inside the loop so that it's now just cbn=turtle.inspectDown() (Which as Lignum commented above, is required to re-initialize the cbn value so it can see if it's changed). Now the machine digs down one block and then gives an "attempted to call a Boolean value" error at line 5. I've gotta say this one's been kinda tricky to puzzle out.
Edited on 05 January 2016 - 08:50 PM
KingofGamesYami #11
Posted 05 January 2016 - 09:57 PM
Leave the success. Only remove the local - or you're back to setting it to a boolean.

Edit: More on local variables here.
Edited on 05 January 2016 - 09:18 PM
Chaz #12
Posted 05 January 2016 - 10:01 PM
Aha! Turtle's working now. And here I was thinking that the "success" would only work if it had the "local" in front of it, haha.

That's fixed the problems and now the turtle behaves as intended. Thank you again for the help!
Lyqyd #13
Posted 05 January 2016 - 10:33 PM
The reason this change fixed the issue is because of scope. With the local keyword in the loop, you were creating new success and cbn variables within the scope of that loop iteration, which immediately evaporate, since that iteration of the loop ends and a new one begins. Whereas without the local keyword, you're updating the values in the success and cbn variables defined above, which is the cbn variable that the loop's condition is actually looking at.
Chaz #14
Posted 05 January 2016 - 10:46 PM
Ahhhh, okay! Thank you for explaining it to me :D/> I swear I'll get the hang of this scripting stuff eventually, but thank you all again for your assistance with this!